1 | # Jest-Chance ![npm](https://img.shields.io/npm/v/jest-chance.svg)
|
2 | A small library to help javascript projects deal with reproducible randomised test data.
|
3 |
|
4 | ![CircleCI token](https://img.shields.io/circleci/token/6a2612980deeac020acd1c4401785f06e91a52af/project/github/meza/jest-chance/master.svg?label=circleci)
|
5 | [![codecov](https://codecov.io/gh/meza/jest-chance/branch/master/graph/badge.svg)](https://codecov.io/gh/meza/jest-chance)
|
6 | ![Dependencies](https://david-dm.org/meza/jest-chance.svg)
|
7 | [![install size](https://packagephobia.now.sh/badge?p=jest-chance)](https://packagephobia.now.sh/result?p=jest-chance)
|
8 | [![npm downloads](https://img.shields.io/npm/dm/jest-chance.svg?style=flat-square)](http://npm-stat.com/charts.html?package=jest-chance)
|
9 | ![Snyk Vulnerabilities for npm package](https://img.shields.io/snyk/vulnerabilities/npm/jest-chance.svg)
|
10 | ![NPM](https://img.shields.io/npm/l/jest-chance.svg)
|
11 |
|
12 | >Tested with:
|
13 | >
|
14 | >![npm dependency version](https://img.shields.io/npm/dependency-version/jest-chance/chance.svg)
|
15 | >![npm peer dependency version](https://img.shields.io/npm/dependency-version/jest-chance/dev/jest.svg)
|
16 |
|
17 | ## Usage
|
18 |
|
19 | ### Installing
|
20 | ```
|
21 | yarn add -D jest-chance
|
22 | ```
|
23 |
|
24 | ```
|
25 | npm install -D jest-chance
|
26 | ```
|
27 |
|
28 | ### Add to Jest
|
29 | To have a random seed for each test execution, we need to tell Jest to use this library.
|
30 |
|
31 | In your `package.json`, add the following:
|
32 | ```json
|
33 | "jest": {
|
34 | "globalSetup": "jest-chance"
|
35 | }
|
36 | ```
|
37 |
|
38 | ### Replace chance in your tests
|
39 | Within your tests, you might have something like this:
|
40 | >**Old Chance usage, don't copy this**
|
41 | >```js
|
42 | >import Chance from 'chance';
|
43 | >const chance = new Chance();
|
44 | >```
|
45 |
|
46 | This will be replaced by our new package's offering:
|
47 |
|
48 | ```js
|
49 | import { chance } from 'jest-chance';
|
50 | ```
|
51 |
|
52 | This will do 2 things:
|
53 | 1. it will acquire a seed to use
|
54 | 2. will return you a chance object primed with said seed
|
55 |
|
56 | ### Watch your logs
|
57 | When you run your tests, jest will tell you something like this:
|
58 | ```
|
59 | Using Chance Seed: 534a873a618e4e317060f9bc29f9115ad156168b
|
60 | ```
|
61 |
|
62 | This is the piece of information you need to replay the tests with the same values.
|
63 |
|
64 | ### Replaying the tests
|
65 | Set the `CHANCE_SEED` environment variable to the seed you got in the console previously
|
66 | ```
|
67 | $ CHANCE_SEED=534a873a618e4e317060f9bc29f9115ad156168b jest
|
68 | ```
|
69 |
|
70 | ## Disclaimer:
|
71 | There's more to follow, this is a WIP project. Feel free to contribute with pull requests.
|
72 |
|
73 | ---
|
74 | ![Twitter Follow](https://img.shields.io/twitter/follow/vsbmeza.svg?style=social)
|