3.17.2 • Published 8 days ago

fast-check v3.17.2

Weekly downloads
58,777
License
MIT
Repository
github
Last release
8 days ago

Getting started

Hands-on tutorial and definition of Property Based Testing: 🏁 see tutorial. Or directly try it online on our pre-configured CodeSandbox.

Property based testing frameworks check the truthfulness of properties. A property is a statement like: for all (x, y, ...) such that precondition(x, y, ...) holds predicate(x, y, ...) is true.

Install the module with: yarn add fast-check --dev or npm install fast-check --save-dev

Example of integration in mocha:

import fc from 'fast-check';

// Code under test
const contains = (text, pattern) => text.indexOf(pattern) >= 0;

// Properties
describe('properties', () => {
  // string text always contains itself
  it('should always contain itself', () => {
    fc.assert(fc.property(fc.string(), (text) => contains(text, text)));
  });
  // string a + b + c always contains b, whatever the values of a, b and c
  it('should always contain its substrings', () => {
    fc.assert(
      fc.property(fc.string(), fc.string(), fc.string(), (a, b, c) => {
        // Alternatively: no return statement and direct usage of expect or assert
        return contains(a + b + c, b);
      }),
    );
  });
});

In case of failure, the test raises a red flag. Its output should help you to diagnose what went wrong in your implementation. Example with a failing implementation of contain:

1) should always contain its substrings
    Error: Property failed after 1 tests (seed: 1527422598337, path: 0:0): ["","",""]
    Shrunk 1 time(s)
    Got error: Property failed by returning false

    Hint: Enable verbose mode in order to have the list of all failing values encountered during the run

Integration with other test frameworks: ava, jasmine, jest, mocha and tape.

More examples: simple examples, fuzzing and against various algorithms.

Useful documentations:

Why should I migrate to fast-check?

fast-check has initially been designed in an attempt to cope with limitations I encountered while using other property based testing frameworks designed for JavaScript:

  • Types: strong and up-to-date types - thanks to TypeScript
  • Extendable: easy map method to derive existing arbitraries while keeping shrink [more] - some frameworks ask the user to provide both a->b and b->a mappings in order to keep a shrinker
  • Extendable: kind of flatMap-operation called chain [more] - able to bind the output of an arbitrary as input of another one while keeping the shrink working
  • Extendable: precondition checks with fc.pre(...) [more] - filtering invalid entries can be done directly inside the check function if needed
  • Extendable: easily switch from fake data in tests to property based with fc.gen() [more] - generate random values within your predicates
  • Smart: ability to shrink on fc.oneof [more] - surprisingly some frameworks don't
  • Smart: biased by default - by default it generates both small and large values, making it easier to dig into counterexamples without having to tweak a size parameter manually
  • Debug: verbose mode [more][tutorial] - easier troubleshooting with verbose mode enabled
  • Debug: replay directly on the minimal counterexample [tutorial] - no need to replay the whole sequence, you get directly the counterexample
  • Debug: custom examples in addition of generated ones [more] - no need to duplicate the code to play the property on custom examples
  • Debug: logger per predicate run [more] - simplify your troubleshoot with fc.context and its logging feature
  • Unique: model based approach [more][article] - use the power of property based testing to test UI, APIs or state machines
  • Unique: detect race conditions in your code [more][tutorial] - shuffle the way your promises and async calls resolve using the power of property based testing to detect races
  • Unique: simplify user definable corner cases [more] - simplify bug resolution by asking fast-check if it can find an even simpler corner case

For more details, refer to the documentation in the links above.

Trusted

fast-check has been trusted for years by big projects like: jest, jasmine, fp-ts, io-ts, ramda, js-yaml, query-string...

Powerful

It also proved useful in finding bugs among major open source projects such as jest, query-string... and many others.

Compatibility

Here are the minimal requirements to use fast-check properly without any polyfills:

fast-checknodeECMAScript versionTypeScript (optional)
3.x≥8(1)ES2017≥4.1(2)
2.x≥8(1)ES2017≥3.2(3)
1.x≥0.12(1)ES3≥3.0(3)
  1. Except for features that cannot be polyfilled - such as bigint-related ones - all the capabilities of fast-check should be usable given you use at least the minimal recommended version of node associated to your major of fast-check.
  2. Require either lib or target ≥ ES2020 or @types/node to be installed.
  3. Require either lib or target ≥ ES2015 or @types/node to be installed.

ReScript bindings

Bindings to use fast-check in ReScript are available in package rescript-fast-check. They are maintained by @TheSpyder as an external project.

Contributors ✨

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome! Become one of them

Sponsors 💸

Many individuals and companies offer their financial support to the project, a huge thanks to all of them too 💓

You can also become one of them by contributing via GitHub Sponsors or OpenCollective.

cucumber-check@infinitebrahmanuniverse/nolb-fast-@everything-registry/sub-chunk-1648qudratic-uiquadratic-sdkrescript-fast-checksyncpackthe-fuzzeffect-schema-compilersfarms-clifuzz-checkfast-speclcap-pc-templatemdast-util-arbitrarymock-data-genjson-schema-fast-checkjazz-toolspixiuswap-libs-sdkpixiu-swap-corepineappletranslucent-cardano@hubbleprotocol/farms-sdk@hubbleprotocol/rarefish-sdk@danny.andrews/asymptotic-analyzer@fp-ts/schema@fp4ts/schema-laws@fp4ts/schema-test-kit@fp4ts/mtl-laws@fp4ts/mtl-test-kit@fp4ts/optics-laws@fp4ts/stream-test-kit@fp4ts/cats-kernel-laws@fp4ts/cats-laws@fp4ts/effect-laws@fp4ts/effect-test-kit@fp4ts/cats-profunctor-laws@fp4ts/cats-test-kit@fp4ts/collections-test-kit@fp4ts/cats-mtl-laws@boostercloud/framework-corecardano-translucent@itweeno/app-sdk@deskpro/app-sdkauditless.jsullamdoloribusasymptotic-analyzer@bity/orianne@principia/laws@principia/model@principia/schema@proti-iac/core@proti-iac/plugins-demo@proti-iac/pulumi-packages-schema@proti-iac/spec@proti-iac/test-runner@micahkendall/translucent@mihalex/farms-sdk-tests@matechs/aio@matechs/fancy@matechs/test@mondrian-framework/model@nirin100/quadratic-protocol@nirin100/quadratic-sdk@lcap/mobile-template@lxsmnsyc/dendro@ephox/mcagar@ephox/agar@effect-ts-app/boilerplate-infra@ssephox/agar@truffle/abi-utils@ethaks/fvtt-quench@effect-app/infra@shopfabrik/shopify-auth@shopfabrik/shopify-data@fast-check/ava@fast-check/jest@fast-check/vitest@fast-check/worker@steepleinc/fp@timeswap-labs/timeswap-v1-convenience@typed/async-data@vlcn.io/nodeno-tests@zalastax/nolb-fast-@waku/relay@0x706b/mongodb-ts@0x706b/morphic
3.17.2

8 days ago

3.17.1

1 month ago

3.17.0

1 month ago

3.16.0

2 months ago

3.15.1

3 months ago

3.15.0

4 months ago

3.13.2

6 months ago

3.14.0

5 months ago

3.13.1

7 months ago

3.12.1

8 months ago

3.13.0

8 months ago

3.12.0

9 months ago

3.11.0

10 months ago

3.9.0

11 months ago

3.10.0

11 months ago

3.8.3

12 months ago

3.8.2

12 months ago

3.8.1

1 year ago

3.8.0

1 year ago

3.7.1

1 year ago

3.7.0

1 year ago

3.6.2

1 year ago

3.6.1

1 year ago

3.6.0

1 year ago

3.6.3

1 year ago

3.5.1

1 year ago

3.2.0

2 years ago

3.5.0

1 year ago

3.4.0

1 year ago

3.3.0

2 years ago

3.1.3

2 years ago

3.1.2

2 years ago

3.1.4

2 years ago

3.1.1

2 years ago

3.1.0

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

2.25.0

2 years ago

2.24.0

2 years ago

2.23.2

2 years ago

2.23.1

2 years ago

2.23.0

2 years ago

2.22.0

2 years ago

2.21.0

2 years ago

2.20.0

2 years ago

2.19.0

3 years ago

2.15.1

3 years ago

2.18.1

3 years ago

2.17.1

3 years ago

2.16.1

3 years ago

2.18.0

3 years ago

2.17.0

3 years ago

2.15.0

3 years ago

2.16.0

3 years ago

2.14.0

3 years ago

2.11.1

3 years ago

2.8.1

3 years ago

2.9.1

3 years ago

2.10.1

3 years ago

2.7.1

3 years ago

2.13.0

3 years ago

2.12.2

3 years ago

2.12.1

3 years ago

2.12.0

3 years ago

2.11.0

3 years ago

2.10.0

3 years ago

2.9.0

3 years ago

2.8.0

3 years ago

2.7.0

3 years ago

2.2.2

3 years ago

2.3.1

3 years ago

2.4.1

3 years ago

2.5.1

3 years ago

2.6.1

3 years ago

2.6.0

4 years ago

2.5.0

4 years ago

2.4.0

4 years ago

2.3.0

4 years ago

2.2.1

4 years ago

2.2.0

4 years ago

2.1.0

4 years ago

2.0.0

4 years ago

1.26.0

4 years ago

1.25.1

4 years ago

1.25.0

4 years ago

1.24.2

4 years ago

1.24.1

4 years ago

1.24.0

4 years ago

1.23.0

4 years ago

1.22.2

4 years ago

1.22.1

4 years ago

1.22.0

4 years ago

1.21.0

4 years ago

1.20.1

4 years ago

1.20.0

4 years ago

1.19.0

4 years ago

1.18.1

5 years ago

1.18.0

5 years ago

1.17.0

5 years ago

1.16.3

5 years ago

1.15.4

5 years ago

1.16.2

5 years ago

1.15.3

5 years ago

1.14.2

5 years ago

1.7.2

5 years ago

1.6.3

5 years ago

1.4.1

5 years ago

1.5.1

5 years ago

1.8.3

5 years ago

1.13.1

5 years ago

1.12.2

5 years ago

1.11.1

5 years ago

1.10.2

5 years ago

1.9.4

5 years ago

1.14.1

5 years ago

1.15.2

5 years ago

1.16.1

5 years ago

1.16.0

5 years ago

1.15.1

5 years ago

1.15.0

5 years ago

1.14.0

5 years ago

1.13.0

5 years ago

1.12.1

5 years ago

1.12.0

5 years ago

1.11.0

5 years ago

1.10.1

5 years ago

1.9.3

5 years ago

1.10.0

5 years ago

1.9.2

5 years ago

1.9.1

5 years ago

1.9.0

5 years ago

1.8.2

5 years ago

1.8.1

5 years ago

1.8.0

5 years ago

1.7.1

6 years ago

1.7.0

6 years ago

1.6.2

6 years ago

1.6.1

6 years ago

1.6.0

6 years ago

1.5.0

6 years ago

1.4.0

6 years ago

1.3.0

6 years ago

1.2.3

6 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

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.2

6 years ago

0.0.1

6 years ago