Seed utilities for controlling randomness in tests. These can be overridden in tests to provide predictable results.
Methods
(inner) random() → {Number}
Example
seed.random()
=> 0.42480169934972056
Returns:
a random number between 0 and 1.0. This can be overridden in tests to control for randomness.
- Type
- Number
(inner) restore()
Example
seed.restore()
(inner) sequence(…values)
- Description:
Replaces
seed.randomwith a function that cycles through a fixed sequence of values, looping back to the start when exhausted. Useful in tests when a single constant isn't enough to drive realistic variation.
- Source:
Examples
seed.sequence(0.1, 0.5, 0.9);
seed.random(); // => 0.1
seed.random(); // => 0.5
seed.random(); // => 0.9
seed.random(); // => 0.1 (loops)
Example usage in a test
beforeEach(() => {
seed.sequence(0.1, 0.5, 0.9); // cycles: 0.1 → 0.5 → 0.9 → 0.1 → ...
});
afterEach(() => {
seed.restore();
});
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
values |
Number |
<repeatable> |
One or more numbers between 0 and 1.0. |
(inner) timestamp() → {Number}
Example
seed.timestamp()
=> 1571246424383
Returns:
milliseconds since the epoch. This can be overridden in tests to control for time.
- Type
- Number