TestCheck.js
TestCheck.js is a JavaScript library for generative property testing, drawing inspiration from QuickCheck. It empowers developers to define program properties or assumptions that should consistently hold true, subsequently testing these properties against a large number of randomly generated input cases. A significant feature of TestCheck.js is its ability to automatically shrink failing test cases, identifying and reporting the smallest possible input that causes a property to fail, which greatly aids in debugging. The package is currently at version 1.0.0-rc.2, indicating active development towards a stable 1.0.0 release. While functioning as a testing utility rather than a complete test runner, it offers seamless integration with popular JavaScript test frameworks such as AVA, Jasmine/Jest, Mocha, and Tape through dedicated plugins. It also includes comprehensive TypeScript and Flow type definitions, supporting modern, type-safe development workflows.
Common errors
-
TypeError: (0 , testcheck__WEBPACK_IMPORTED_MODULE_0__.check) is not a function
cause This typically occurs when mixing CommonJS `require` with ES Modules `import` syntax, or when a bundler incorrectly transpiles the module, or if `check` is attempted to be used as a default import when it's a named export.fixEnsure consistent import style: use `import { check, gen, property } from 'testcheck';` for ES Modules/TypeScript environments, or `const { check, gen, property } = require('testcheck');` for CommonJS environments. -
ReferenceError: check is not defined
cause The `check` function (or `gen`, `property`) was not imported or required correctly into the current JavaScript scope before being used.fixAdd the necessary import statement at the top of your file: `import { check, gen, property } from 'testcheck';` (for ESM/TS) or `const { check, gen, property } = require('testcheck');` (for CommonJS). -
Property function did not return true
cause This is TestCheck.js reporting a test failure. The property function provided returned `false` (or threw an error) for one or more generated test cases. This indicates either a bug in the code under test or an incorrect/flawed property definition.fixExamine the `result.shrunk.smallest` output from the `check` function to identify the minimal failing input. Debug your property function and/or the application code it tests to resolve the discrepancy.
Warnings
- breaking The `1.0.0-rc.0` release introduced significant changes and new features, implying potential breaking API changes for users upgrading from pre-1.0 (e.g., 0.x) versions. A full report was promised for the final `1.0.0` release.
- gotcha TestCheck.js is a property testing *utility* and does not include a test runner. It must be integrated with an existing test framework (like AVA, Jasmine/Jest, Mocha, or Tape) or its `check` function called explicitly. It will not automatically discover and run tests.
- gotcha As a release candidate (`1.0.0-rc.2`), the API is generally stable but may still undergo minor adjustments or refinements before the final `1.0.0` release. Users should be aware of potential, albeit small, changes.
Install
-
npm install testcheck -
yarn add testcheck -
pnpm add testcheck
Imports
- check
const check = require('testcheck');import { check } from 'testcheck'; - gen
import * as gen from 'testcheck/gen';
import { gen } from 'testcheck'; - property
import Property from 'testcheck';
import { property } from 'testcheck'; - { check, gen, property } (CommonJS)
import { check, gen, property } from 'testcheck';const { check, gen, property } = require('testcheck');
Quickstart
import { check, gen, property } from 'testcheck';
// Define a property: any integer subtracted from itself should be 0.
// TestCheck will generate various integer inputs (x) and check this property.
const subtractionProperty = property(
gen.int, // Generator for an integer input
(x: number) => x - x === 0
);
// Run the property check with default settings
const result = check(subtractionProperty);
// Log the outcome. In a real test suite (e.g., Jest, Mocha), you'd use
// framework-specific assertions based on `result.pass`.
if (result.pass) {
console.log('Property holds true for all generated cases after ' + result.numTests + ' tests.');
} else {
console.error(`Property failed after ${result.numTests} tests.`);
console.error(`Failing input (minimal): ${JSON.stringify(result.shrunk.smallest)}`);
// Access original failing input: result.fail
}
// Example with multiple generators and a more complex property
const additionCommutativity = property(
gen.int, gen.int,
(a: number, b: number) => a + b === b + a
);
const addResult = check(additionCommutativity);
if (!addResult.pass) {
console.error(`Addition commutativity failed for: ${JSON.stringify(addResult.shrunk.smallest)}`);
}