ci / tests (macos-latest, python 3.11) (push) Canceled after 0s
ci / tests (macos-latest, python 3.13) (push) Canceled after 0s
ci / tests (macos-latest, python 3.9) (push) Canceled after 0s
ci / tests (ubuntu-latest, python 3.11) (push) Canceled after 0s
ci / tests (ubuntu-latest, python 3.13) (push) Canceled after 0s
ci / tests (ubuntu-latest, python 3.9) (push) Canceled after 0s
Contracts with a ladder of checks, an end-of-turn gate, retrospectives and shared lessons, time-to-delivery measurement, a library of 140 verification techniques and 20 recipes, personal data kept outside the clone, and releases with rollback. Standard library only. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
68 lines
2.5 KiB
Markdown
68 lines
2.5 KiB
Markdown
---
|
||
id: fast-check
|
||
title: Property testing in TypeScript and JavaScript with fast-check
|
||
answers: Does this JavaScript or TypeScript function hold its law for generated inputs, and what is the smallest counterexample?
|
||
rung: 2
|
||
section: many-inputs
|
||
languages: typescript, javascript
|
||
kinds: general, parsing, money, state-machine
|
||
cost: low
|
||
detect: node --version
|
||
ran_here: yes
|
||
verified: ran
|
||
---
|
||
|
||
## Use when
|
||
|
||
- A function or module has a law (round trip, idempotence, order, agreement with a simple version) and you use Node, Jest, Vitest or the built-in `node:test`.
|
||
- You want shrinking and a replayable seed.
|
||
- You want to test a sequence of operations against a model (see `stateful-model-test`).
|
||
|
||
## Do not use when
|
||
|
||
- The code is browser-only and cannot run under Node; run the same property in the browser test runner instead.
|
||
- You need coverage-guided search. Use `jazzer`.
|
||
|
||
## Set up
|
||
|
||
npm install --save-dev fast-check
|
||
|
||
fast-check runs inside any runner. The example uses `node --test`, which needs
|
||
no extra install.
|
||
|
||
## Minimal example (`props.test.mjs`)
|
||
|
||
import test from 'node:test';
|
||
import fc from 'fast-check';
|
||
|
||
const encode = (b) => Buffer.from(b).toString('hex');
|
||
const decode = (s) => Uint8Array.from(Buffer.from(s, 'hex'));
|
||
|
||
test('round trip', () => {
|
||
fc.assert(
|
||
fc.property(fc.uint8Array({ maxLength: 256 }), (bytes) =>
|
||
Buffer.compare(Buffer.from(decode(encode(bytes))), Buffer.from(bytes)) === 0),
|
||
{ numRuns: 1000 },
|
||
);
|
||
});
|
||
|
||
## Run
|
||
|
||
node --test props.test.mjs
|
||
|
||
Passing looks like: `ℹ pass 1` and `ℹ fail 0`, exit 0.
|
||
Failure shows `Counterexample: [...]`, `Shrunk N time(s)` and a line
|
||
`{ seed: -2075209501, path: "..." }`, exit non-zero. Replay it by passing the
|
||
same values: `fc.assert(prop, { seed: -2075209501, path: "8:4:..." })`. Pin
|
||
important cases permanently with `{ examples: [[...]] }`.
|
||
|
||
## As a contract check
|
||
|
||
{"id": "prop-round-trip-js", "claim": "decode(encode(x)) equals x for generated byte arrays", "kind": "parsing", "rung": 2, "cmd": "node --test props.test.mjs", "expect": {"exit": 0}, "tier": "deep", "timeout_s": 300}
|
||
|
||
## What a pass does not prove
|
||
|
||
- It samples (100 runs by default; `numRuns` raises it). The arbitrary decides what is ever tried, so check its bounds against real inputs.
|
||
- A property that returns `true` for any input is vacuous. Test the test: break the code on purpose, confirm a counterexample appears, restore it.
|
||
- Number generators include `NaN`, `-0` and huge values only if you ask; `fc.integer()` is not `fc.double()`.
|