Property-Based Testing for JavaScript/TypeScript
fast-check is a property-based testing framework for JavaScript and TypeScript, inspired by Haskell's QuickCheck. It enables developers to define properties about their code and automatically generates diverse inputs to test these properties, identifying edge cases that traditional example-based testing might miss. The current stable version is 4.7.0. The project maintains an active release cadence, with continuous development across its core library and various test runner integrations. Key differentiators include its robust shrinking capabilities, which minimize counterexamples to help pinpoint bugs, an extensive set of built-in arbitrary data generators, and comprehensive TypeScript support. It integrates seamlessly with popular testing frameworks like Mocha, Jest, Vitest, and Ava via dedicated adapter packages, though users should be aware of recent changes in module support for these adapters.
Common errors
-
Error: Property failed by returning false
cause A property's predicate function returned `false` for a generated input, indicating a bug in the code under test or the property definition.fixReview the counterexample provided in the test output (the shrunken inputs) to debug your implementation or refine your property's preconditions. -
SyntaxError: Cannot use import statement outside a module
cause Attempting to use ES module `import` syntax in a CommonJS context, especially common when using ESM-only adapter packages like `@fast-check/vitest`.fixEnsure your file is treated as an ES module (e.g., by using `.mjs` extension, or setting `"type": "module"` in `package.json`) or migrate to using `require()` if the package still supports CJS and you must use CJS.
Warnings
- breaking Several adapter packages (e.g., `@fast-check/worker`, `@fast-check/vitest`, `@fast-check/poisoning`, `@fast-check/packaged`, `@fast-check/ava`) have dropped CommonJS support and are now ESM-only. If you are using these integrations, you must update your project to use ES modules (`import`/`export`).
- deprecated The methods `Random::next(n)` and `Random::nextInt()` have been deprecated. Users should migrate to newer alternatives for random number generation.
- gotcha While the core `fast-check` package in the 4.x series still supports CommonJS (`require()`), many of its ecosystem adapter packages (e.g., for Vitest, Ava, Workers) have moved to ESM-only. This can lead to module resolution issues if mixing CommonJS for `fast-check` and ESM-only adapters.
Install
-
npm install fast-check -
yarn add fast-check -
pnpm add fast-check
Imports
- fc
const fc = require('fast-check').default;import fc from 'fast-check';
- assert
import fc, { assert, property } from 'fast-check';import { assert, property, string } from 'fast-check'; - test
const { test } = require('@fast-check/vitest');import { test } from '@fast-check/vitest';
Quickstart
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);
}),
);
});
});