npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

jest-chance

v0.2.6

Published

Random seed generator for chancejs with jest

Readme

Jest-Chance npm

install size npm downloads Snyk Vulnerabilities for npm package NPM

npm dependency version

Why?

When working with larger teams with varied skill levels, it can be difficult to ensure that tests are written in a way that is consistent and reproducible. This can lead to tests that are flaky and hard to debug.

It's very easy to make tests pass if the test is only testing for one single known input, expecting a known output.

If this is your test:

test('should return world when saying hello', () => {
  expect(add(1, 1)).toBe(2);
});

Then it's very easy to make this test pass by changing the implementation to:

export const add = (a, b) => 2;

In traditional unit testing, we would apply a technique called triangulation to ensure that our tests are not brittle. It means adding at least 3 examples of known inputs and outputs.

While this is a good start, it introduces a new problem: a large amount of code needs to be written for even the simplest tests.

In comes randomised data and Monkey Testing to the rescue!

When done correctly, randomised data can help you with two things:

  1. Over time, you will run the code through thousands of different inputs and outputs, and you will find edge cases that you may not have thought of.
  2. It can help you communicate instances where the input is irrelevant for a given test.

When NOT to use randomised data

In the example above, we are testing a simple function that adds two numbers together. Writing random tests for this function is not very useful, as you would need to re-implement adding of 2 numbers in your test just to get the expected output.

A good rule of thumb is that when you find yourself reimplementing logic to get to an expected output, you shouldn't be using random data.

When to use randomised data

  • Asserting that a value you passed in, gets passed on to a mock
  • When the contents of the data is irrelevant to the test
  • When you can assert on the shape of the data, but not the contents
  • When you want to test that your code can handle a large variety of inputs

Repeatable Tests

When using randomised data, it's important to ensure that the tests are repeatable.

The most common pitfall of randomised testing is that you can end up with a lot of tests that are hard to debug.

Builds break on CI systems and when you want to replay them locally, you can't because the random seed is not saved.

Unless you’re building regulated gambling solutions or highly specialised solutions, most of the randomizers you’ll end up working with will be pseudo-random generators.

The main characteristic of such a thing is that the values they produce are completely determined by an initial value, called a seed. This means that if you ask for 5 random numbers in a sequence, the sequence will give you the exact same numbers every time if you use the same seed.

Fortunately for us, we’re completely ok with not being securely random, and we can use it to our advantage.

What does Jest-Chance do?

As we’ve learned, given the same seed value, we’ll always get the same random values generated.

This means that if we can find a way to expose and control our test seed, we could use that to reproduce our failing tests anywhere and any time we’d like.

And that's exactly what Jest-Chance does.

It's a wrapper around the Chance library, which is a great library for generating randomised data.

It takes the seed managament under control and helps you to write randomized tests that are repeatable and reproducible.

Usage

Installing

pnpm add -D jest-chance
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 jest config, add the following:

  ...
  "globalSetup": "jest-chance"
  ...

Add to Vitest

Create a file called vitest.setup.ts in the root of your project and add the following:

import chanceSetup from 'jest-chance';

export const setup = () => {
  chanceSetup();
};

Then in your vitest.config.ts add the following:

  ...
  "globalSetup": './vitest.setup.ts',
  ...

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');
 

Repeating failed tests

When you run your tests, jest will tell you something like this in your execution logs:

Using Chance Seed: 534a873a618e4e317060f9bc29f9115ad156168b

This is the piece of information you need to replay the tests with the same values.

Rerunning the tests

Set the CHANCE_SEED environment variable to the seed you got in the console previously

$ CHANCE_SEED=534a873a618e4e317060f9bc29f9115ad156168b jest

Due to the way pseudo-random generators work, you will have to run the exact command that your failing system runs.

Repeating a subset of tests with the same seed will not result in the same values being generated.

You must run the exact same command that failed.


Twitter Follow