TestCheck.js

1.0.0-rc.2 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define and run a basic property test using `gen.int` and `check`, illustrating both success and failure reporting.

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)}`);
}

view raw JSON →