0.2.5 • Published 2 years ago

jest-chance v0.2.5

Weekly downloads
2,015
License
MIT
Repository
github
Last release
2 years ago

Jest-Chance npm

A small library to help javascript projects deal with reproducible randomised test data.

install size npm downloads Snyk Vulnerabilities for npm package NPM

Tested with:

npm dependency version npm peer dependency version

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.


Twitter Follow

0.2.1

2 years ago

0.2.0

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.2.5

2 years ago

0.2.4

2 years ago

0.1.10

3 years ago

0.1.11

3 years ago

0.1.9

3 years ago

0.1.8

5 years ago

0.1.7

5 years ago

0.1.6

5 years ago

0.1.5

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.34

5 years ago

0.0.33

5 years ago

0.0.32

6 years ago

0.0.31

6 years ago

0.0.30

6 years ago

0.0.29

6 years ago

0.0.27

6 years ago

0.0.19

6 years ago

0.0.18

6 years ago

0.0.17

6 years ago

0.0.15

6 years ago

0.0.14

6 years ago

0.0.13

6 years ago

0.0.12

6 years ago

0.0.11

6 years ago

0.0.10

6 years ago

0.0.9

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.1

6 years ago

0.0.0

6 years ago