Property based testing framework for JavaScript (like QuickCheck)
Configuration options for test execution, runtime context access, and utility functions.
Configure global parameters applied to all property tests.
/**
* Define global parameters applied to all property runs
* @param parameters - Global configuration parameters
*/
function configureGlobal(parameters: GlobalParameters): void;
/**
* Read current global configuration parameters
* @returns Current global parameters
*/
function readConfigureGlobal(): GlobalParameters;
/**
* Reset global parameters to defaults
*/
function resetConfigureGlobal(): void;
type GlobalParameters = {
numRuns?: number;
maxSkipsPerRun?: number;
timeout?: number;
path?: string;
unbiased?: boolean;
verbose?: boolean | VerbosityLevel;
seed?: number;
randomType?: RandomType;
reporter?: <T>(runDetails: RunDetails<T>) => void;
asyncReporter?: <T>(runDetails: RunDetails<T>) => Promise<void>;
beforeEach?: GlobalPropertyHookFunction;
afterEach?: GlobalPropertyHookFunction;
asyncBeforeEach?: GlobalAsyncPropertyHookFunction;
asyncAfterEach?: GlobalAsyncPropertyHookFunction;
endOnFailure?: boolean;
skipAllAfterTimeLimit?: number;
interruptAfterTimeLimit?: number;
markInterruptAsFailure?: boolean;
skipEqualValues?: boolean;
baseSize?: Size;
defaultSizeToMaxWhenMaxSpecified?: boolean;
};
type GlobalPropertyHookFunction = () => void;
type GlobalAsyncPropertyHookFunction = (() => Promise<unknown>) | (() => void);
type RandomType = 'mersenne' | 'xorshift128plus' | 'xoroshiro128plus' | 'congruential' | 'congruential32';
enum VerbosityLevel {
None = 0,
Verbose = 1,
VeryVerbose = 2
}Usage Example:
import { configureGlobal, readConfigureGlobal, resetConfigureGlobal, VerbosityLevel } from 'fast-check';
// Configure all tests to run with 1000 runs and verbose output
configureGlobal({
numRuns: 1000,
verbose: VerbosityLevel.Verbose,
seed: 42, // Reproducible tests
});
// Read current configuration
const config = readConfigureGlobal();
console.log(`Running with seed: ${config.seed}`);
// Reset to defaults
resetConfigureGlobal();Configure individual property runs:
interface Parameters<T> {
/** Random seed for reproducible tests */
seed?: number;
/** Type of random number generator */
randomType?: RandomType;
/** Number of test runs to execute */
numRuns?: number;
/** Maximum number of skipped tests before failing */
maxSkipsPerRun?: number;
/** Timeout per test run in milliseconds */
timeout?: number;
/** Path for replaying a specific counterexample */
path?: string;
/** Disable bias towards edge cases */
unbiased?: boolean;
/** Verbosity level for output */
verbose?: boolean | VerbosityLevel;
/** Custom examples to test before generated values */
examples?: T[];
/** Stop on first failure */
endOnFailure?: boolean;
/** Custom reporter function for run details */
reporter?: (runDetails: RunDetails<T>) => void;
/** Async custom reporter function */
asyncReporter?: (runDetails: RunDetails<T>) => Promise<void>;
/** Mark test as failed if interrupted */
markInterruptAsFailure?: boolean;
/** Interrupt test after time limit (ms) */
interruptAfterTimeLimit?: number;
/** Skip all remaining tests after time limit (ms) */
skipAllAfterTimeLimit?: number;
/** Skip duplicate values during testing */
skipEqualValues?: boolean;
}Usage Example:
import { assert, property, integer } from 'fast-check';
// Override global config for specific test
assert(
property(integer(), integer(), (a, b) => {
return a + b === b + a;
}),
{
seed: 12345,
numRuns: 10000,
verbose: true,
examples: [[0, 0], [1, -1], [Number.MAX_SAFE_INTEGER, 1]],
}
);Control and share depth across recursive arbitraries.
/**
* Create a new unique depth identifier for sharing depth across arbitraries
* @returns Unique depth identifier
*/
function createDepthIdentifier(): DepthIdentifier;
/**
* Get the depth context associated with an identifier
* @param contextMeta - Depth context, identifier, or string
* @returns Depth context object
*/
function getDepthContextFor(contextMeta: DepthContext | DepthIdentifier | string | undefined): DepthContext;
type DepthIdentifier = { readonly name: string };
type DepthContext = { depth: number };Usage Example:
import { createDepthIdentifier, array, record, string, integer } from 'fast-check';
// Share depth limit across multiple recursive structures
const depthId = createDepthIdentifier();
const treeArb = record({
value: integer(),
children: array(
record({
value: string(),
}),
{ depthIdentifier: depthId }
),
}, { depthIdentifier: depthId });Access runtime context for logging and debugging during property execution.
/**
* Generate execution context for logging during property execution
* @returns Arbitrary generating context values
*/
function context(): Arbitrary<ContextValue>;
interface ContextValue {
/**
* Log a message that will be included in failure reports
* @param label - Log message
*/
log(label: string): void;
/**
* Get size hint for current execution
*/
size(): number;
/**
* Get maximum number of logs allowed
*/
maxLogs(): number;
}Usage Example:
import { assert, property, context, array, integer } from 'fast-check';
assert(
property(context(), array(integer()), (ctx, arr) => {
ctx.log(`Testing array of length ${arr.length}`);
const sorted = [...arr].sort((a, b) => a - b);
ctx.log(`Sorted: [${sorted.slice(0, 5).join(', ')}...]`);
// Property to test
for (let i = 1; i < sorted.length; i++) {
if (sorted[i] < sorted[i - 1]) {
ctx.log(`Found violation at index ${i}`);
return false;
}
}
return true;
})
);Generate values imperatively within test execution (advanced usage).
/**
* Generate values within test execution itself
* @returns Arbitrary generating generator functions
*/
function gen(): Arbitrary<GeneratorValue>;
type GeneratorValue = <T>(arb: Arbitrary<T>) => T;Usage Example:
import { assert, property, gen, integer, array } from 'fast-check';
assert(
property(gen(), (g) => {
// Generate values imperatively
const length = g(integer({ min: 0, max: 10 }));
const arr = g(array(integer(), { minLength: length, maxLength: length }));
return arr.length === length;
})
);/**
* Error thrown when precondition fails
*/
class PreconditionFailure extends Error {
readonly interruptExecution: true;
readonly footprint: string;
}/** Type of module (commonjs or module) */
const __type: string;
/** Version of fast-check package */
const __version: string;
/** Commit hash of the current code */
const __commitHash: string;Usage Example:
import { __version, __commitHash } from 'fast-check';
console.log(`fast-check version: ${__version}`);
console.log(`Built from commit: ${__commitHash}`);Install with Tessl CLI
npx tessl i tessl/npm-fast-check