New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

jest-chance

Package Overview
Dependencies
Maintainers
1
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-chance

Random seed generator for chancejs with jest

0.2.5
next
latest
Source
npm
Version published
Maintainers
1
Created
Source

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:

  • it will acquire a seed to use
  • 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

FAQs

Package last updated on 26 Oct 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts