jest-chance v0.2.5
Jest-Chance
A small library to help javascript projects deal with reproducible randomised test data.
Tested with:
Usage
Installing
yarn add -D jest-chance
npm install -D jest-chance
Add to Jest
To have a random seed for each test execution, we need to tell Jest to use this library.
In your package.json
, add the following:
"jest": {
"globalSetup": "jest-chance"
}
Replace chance in your tests
Within your tests, you might have something like this:
Old Chance usage, don't copy this
import Chance from 'chance'; const chance = new Chance();
This will be replaced by our new package's offering:
import { chance } from 'jest-chance';
This will do 2 things: 1. it will acquire a seed to use 2. will return you a chance object primed with said seed
### Using with a fixed seed
Sometimes you would need deterministic generators.
For that reason, you can use the method: getChance(seed?)
import { getChance } from 'jest-chance';
const deterministicChance = getChance('a-fixed-seed');
Watch your logs
When you run your tests, jest will tell you something like this:
Using Chance Seed: 534a873a618e4e317060f9bc29f9115ad156168b
This is the piece of information you need to replay the tests with the same values.
Replaying the tests
Set the CHANCE_SEED
environment variable to the seed you got in the console previously
$ CHANCE_SEED=534a873a618e4e317060f9bc29f9115ad156168b jest
Disclaimer
There's more to follow, this is a WIP project. Feel free to contribute with pull requests.
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago