Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dubzzz/fast-check
Property based testing framework for JavaScript (like QuickCheck) written in TypeScript
https://github.com/dubzzz/fast-check
faker fuzzing generative-testing property-based-testing quickcheck tdd testing typescript unit-testing
Last synced: 5 days ago
JSON representation
Property based testing framework for JavaScript (like QuickCheck) written in TypeScript
- Host: GitHub
- URL: https://github.com/dubzzz/fast-check
- Owner: dubzzz
- License: mit
- Created: 2017-10-30T23:41:11.000Z (about 7 years ago)
- Default Branch: main
- Last Pushed: 2024-10-29T08:33:49.000Z (3 months ago)
- Last Synced: 2024-10-29T09:23:42.852Z (3 months ago)
- Topics: faker, fuzzing, generative-testing, property-based-testing, quickcheck, tdd, testing, typescript, unit-testing
- Language: TypeScript
- Homepage: https://fast-check.dev/
- Size: 40 MB
- Stars: 4,327
- Watchers: 19
- Forks: 180
- Open Issues: 69
-
Metadata Files:
- Readme: .github/README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
- Security: SECURITY.md
Awesome Lists containing this project
- awesome-list - fast-check
- awesome-javascript - @fast-check/monorepo
- awesome-javascript - @fast-check/monorepo
README
Property based testing framework for JavaScript/TypeScript
## Getting started
Hands-on tutorial and definition of Property Based Testing: [π see tutorial](https://fast-check.dev/docs/tutorials/quick-start/). Or directly try it online on our pre-configured [CodeSandbox](https://codesandbox.io/s/github/dubzzz/fast-check/tree/main/examples?previewwindow=tests).
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](http://mochajs.org/):
```js
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 falseHint: Enable verbose mode in order to have the list of all failing values encountered during the run
```Integration with other test frameworks: [ava](https://github.com/dubzzz/fast-check-examples/blob/main/test-ava/example.spec.js), [jasmine](https://github.com/dubzzz/fast-check-examples/blob/main/test-jasmine/example.spec.js), [jest](https://github.com/dubzzz/fast-check-examples/blob/main/test-jest/example.spec.js), [mocha](https://github.com/dubzzz/fast-check-examples/blob/main/test/longest%20common%20substr/test.js) and [tape](https://github.com/dubzzz/fast-check-examples/blob/main/test-tape/example.spec.js).
More examples: [simple examples](https://github.com/dubzzz/fast-check/tree/main/examples), [fuzzing](https://github.com/dubzzz/fuzz-rest-api) and [against various algorithms](https://github.com/dubzzz/fast-check-examples).
Useful documentations:
- [π Introduction to Property Based & Hands On](https://fast-check.dev/docs/tutorials/quick-start/)
- [π£ Built-in arbitraries](https://fast-check.dev/docs/core-blocks/arbitraries/)
- [π§ Custom arbitraries](https://fast-check.dev/docs/core-blocks/arbitraries/combiners/)
- [πββοΈ Property based runners](https://fast-check.dev/docs/core-blocks/runners/)
- [π₯ Tips](https://fast-check.dev/docs/configuration/)
- [π API Reference](https://fast-check.dev/api-reference/index.html)
- [β Awesome fast-check](https://fast-check.dev/docs/ecosystem/)## 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](https://fast-check.dev/docs/core-blocks/arbitraries/combiners/any/#map)\] - _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](https://fast-check.dev/docs/core-blocks/arbitraries/combiners/any/#chain)\] - _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](https://fast-check.dev/docs/core-blocks/properties/#example)\] - _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](https://fast-check.dev/docs/core-blocks/arbitraries/others/#gen)\] - _generate random values within your predicates_
- **Smart:** ability to shrink on `fc.oneof` \[[more](https://fast-check.dev/docs/core-blocks/arbitraries/combiners/any/#oneof)\] - _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](https://fast-check.dev/docs/configuration/custom-reports/#verbosity)\]\[[tutorial](https://fast-check.dev/docs/tutorials/quick-start/read-test-reports/#how-to-increase-verbosity)\] - _easier troubleshooting with verbose mode enabled_
- **Debug:** replay directly on the minimal counterexample \[[tutorial](https://fast-check.dev/docs/tutorials/quick-start/read-test-reports/#how-to-re-run)\] - _no need to replay the whole sequence, you get directly the counterexample_
- **Debug:** custom examples in addition of generated ones \[[more](https://fast-check.dev/docs/configuration/user-definable-values/#run-against-custom-values)\] - _no need to duplicate the code to play the property on custom examples_
- **Debug:** logger per predicate run \[[more](https://fast-check.dev/docs/core-blocks/arbitraries/others/#context)\] - _simplify your troubleshoot with fc.context and its logging feature_
- **Unique:** model based approach \[[more](https://fast-check.dev/docs/advanced/model-based-testing/)\]\[[article](https://medium.com/criteo-labs/detecting-the-unexpected-in-web-ui-fuzzing-1f3822c8a3a5)\] - _use the power of property based testing to test UI, APIs or state machines_
- **Unique:** detect race conditions in your code \[[more](https://fast-check.dev/docs/advanced/race-conditions/)\]\[[tutorial](https://fast-check.dev/docs/tutorials/detect-race-conditions/)\] - _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](https://fast-check.dev/docs/configuration/user-definable-values/#shrink-custom-values)\] - _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](https://github.com/jestjs/jest), [jasmine](https://github.com/jasmine/jasmine), [fp-ts](https://github.com/gcanti/fp-ts), [io-ts](https://github.com/gcanti/io-ts), [ramda](https://github.com/ramda/ramda), [js-yaml](https://github.com/nodeca/js-yaml), [query-string](https://github.com/sindresorhus/query-string)...
### Powerful
It also proved useful in finding bugs among major open source projects such as [jest](https://github.com/jestjs/jest), [query-string](https://github.com/sindresorhus/query-string)... and [many others](https://fast-check.dev/docs/introduction/track-record/).
## Compatibility
Here are the minimal requirements to use fast-check properly without any polyfills:
| fast-check | node | ECMAScript version | _TypeScript (optional)_ |
| ---------- | ------------------- | ------------------ | ----------------------- |
| **4.x** | β₯10.5.0 | ES2020 | β₯5.0 |
| **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) |More details...
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](https://rescript-lang.org) are available in package [rescript-fast-check](https://www.npmjs.com/rescript-fast-check). They are maintained by [@TheSpyder](https://github.com/TheSpyder) as an external project.
## Contributors β¨
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/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
π
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome! [Become one of them](CONTRIBUTING.md)
## 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](https://github.com/sponsors/dubzzz) or [OpenCollective](https://opencollective.com/fast-check/contribute).