jest-chance
TypeScript icon, indicating that this package has built-in type declarations

0.2.5 • Public • Published

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

Readme

Keywords

none

Package Sidebar

Install

npm i jest-chance

Weekly Downloads

1,424

Version

0.2.5

License

MIT

Unpacked Size

52.4 kB

Total Files

11

Last publish

Collaborators

  • meza