@fast-check/jest

2.1.1last stable release 4 weeks ago
Complexity Score
High
Open Issues
N/A
Dependent Projects
36
Weekly Downloadsglobal
60,422

License

  • MIT
    • Yesattribution
    • Permissivelinking
    • Permissivedistribution
    • Permissivemodification
    • Nopatent grant
    • Yesprivate use
    • Permissivesublicensing
    • Notrademark grant

Downloads

Loading Weekly Download Data

Readme

Property based testing framework for JavaScript/TypeScript

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: pnpm add -D fast-check or 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:

  • 🏁 Introduction to Property Based & Hands On
  • 🐣 Built-in arbitraries
  • πŸ”§ Custom arbitraries
  • πŸƒβ€β™‚οΈ Property based runners
  • πŸ’₯ Tips
  • πŸ”Œ API Reference
  • ⭐ Awesome fast-check

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-check node ECMAScript version TypeScript (optional) 4.x β‰₯12.17.0(1) ES2020 β‰₯5.0 3.x β‰₯8(2) ES2017 β‰₯4.1(3) 2.x β‰₯8(2) ES2017 β‰₯3.2(4) 1.x β‰₯0.12(2) ES3 β‰₯3.0(4) More details...
  1. Even if version 12.x should support most of the ES2020 features that will be leveraged by the version 4, we recommend relying at least on version 14.x of Node as it supports all the targeted specification. In addition, we highly encourage switching to still supported LTS versions of Node and not sticking to unsupported versions for too long.
  2. 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.
  3. Require either lib or target β‰₯ ES2020 or @types/node to be installed.
  4. Require either lib or target β‰₯ ES2015 or @types/node to be installed.

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Nicolas DUBIEN
πŸ’» πŸ“– ⚠️ πŸš‡ 🎨 🚧
Aaron Elligsen
πŸ’» πŸ“– ⚠️
Will Heslam
πŸ“–
kazchimo
πŸ’» πŸ“–
Brandon Chinn
πŸ’» πŸ“–
Irakli Safareli
πŸ“–
Andrew Herron
πŸ“– πŸ”Œ
Eric Crosson
πŸ“– πŸ’»
burrscurr
πŸ“–
JC (Jonathan Chen)
πŸ“–
Larry Botha
πŸ“– πŸ’» ⚠️
Roman Gusev
πŸ“–
Tim Wisniewski
πŸ“–
Brais PiΓ±eiro
πŸ’» ⚠️
Renaud-Pierre Bordes
🎨
Jemma Nelson
πŸ“–
John Haugeland
πŸ“–
Trey Davis
🎨
Leon Si
πŸ“–
Gorgi Kosev
πŸš‡
mayconsacht
πŸ’»
Simon Friis Vindum
πŸ’» ⚠️
Richard Gibson
πŸ“–
Alan Harper
πŸ“–
Makien Osman
πŸ’»
David Sommerich
πŸ’» ⚠️
Diego Pedro
πŸ’» ⚠️
Borui Gu
πŸ“–
Brian Donovan
πŸ“–
volrk
πŸ’» πŸ“– ⚠️
tinydylan
πŸ’» ⚠️
Caleb Jasik
πŸ“–
Rulai Hu
πŸ“–
Afonso Jorge Ramos
πŸ“–
Tom Jenkinson
πŸ“–
phormio
πŸ“–
Giovanni Gonzaga
πŸ’» ⚠️
Tomas Carnecky
πŸ’»
Kirill Romanov
πŸ’» πŸ“– ⚠️
Giovanny GonzΓ‘lez
πŸ“–
Mark Kulube
πŸš‡
Peter Hamilton
πŸ’»
Chinedu Ozodi
πŸ“–
Gunar Gessner
πŸ“–
Christian Batchelor
⚠️
Tomer Aberbach
πŸ’» πŸ“– ⚠️
0xflotus
πŸ“–
Ryan Leonard
πŸ’» πŸ“– ⚠️
Jason Dreyzehner
πŸ’» ⚠️
Matin Zadeh Dolatabad
πŸ’»
Juan JuliΓ‘n Merelo GuervΓ³s
πŸ“–
Simen Bekkhus
πŸ“–
Tarjei Skjærset
πŸ“–
Denis Gorbachev
πŸ“–
Trevor McCauley
πŸ“–
Grant Kiely
πŸ“–
Attila Večerek
πŸ’» πŸ“– ⚠️
Zach Bjornson
πŸ’» πŸ“–
Bennett Perkins
πŸ“–
Alexandre Oger
πŸ“–
ej shafran
πŸ“–
Niklas Gruhn
πŸ’» πŸ’¬
Patrick Roza
πŸ’»
Cindy Wu
πŸ“–
Noah
πŸ“–
James Vaughan
πŸ“–
Alex Errant
πŸ’»
andrew jarrett
πŸ’»

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.

Dependencies

Loading dependencies...

CVE IssuesActive
0
Scorecards Score
8.20
Test Coverage
No Data
Follows Semver
Yes
Github Stars
4,479
Dependenciestotal
13
DependenciesOutdated
1
DependenciesDeprecated
0
Threat Modelling
No Data
Repo Audits
No Data

Learn how to distribute @fast-check/jest in your own private NPM registry

npm config set registry  https://npm.cloudsmith.com/owner/repo
Processing...
Done
npm install @fast-check/jest
Processing...
Done

32 Releases

NPM on Cloudsmith

Getting started with NPM on Cloudsmith is fast and easy.