or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

advanced

combinators.mdconcurrency-testing.mdmodel-based-testing.md
configuration.mdindex.md
tile.json

primitive.mddocs/arbitraries/

Primitive Arbitraries

Primitive arbitraries generate basic JavaScript values including booleans, numbers, strings, dates, and other fundamental types.

Capabilities

Boolean Values

Generate boolean values (true or false).

/**
 * Generate boolean values
 * @returns Arbitrary generating true or false
 */
function boolean(): Arbitrary<boolean>;

Usage Example:

import { boolean, property, assert } from 'fast-check';

assert(
  property(boolean(), boolean(), (a, b) => {
    return (a && b) === (b && a); // AND is commutative
  })
);

Integer Values

Generate integer values with configurable range constraints.

/**
 * Generate integers between min and max (inclusive)
 * @param constraints - Min/max bounds (default: -0x80000000 to 0x7fffffff)
 * @returns Arbitrary generating integers
 */
function integer(constraints?: IntegerConstraints): Arbitrary<number>;

/**
 * Generate natural numbers (non-negative integers)
 * @param constraints - Min/max bounds (default: 0 to 0x7fffffff)
 * @returns Arbitrary generating natural numbers
 */
function nat(constraints?: NatConstraints): Arbitrary<number>;

/**
 * Generate integers between Number.MIN_SAFE_INTEGER and Number.MAX_SAFE_INTEGER
 * @returns Arbitrary generating safe integers
 */
function maxSafeInteger(): Arbitrary<number>;

/**
 * Generate natural numbers between 0 and Number.MAX_SAFE_INTEGER
 * @returns Arbitrary generating safe natural numbers
 */
function maxSafeNat(): Arbitrary<number>;

interface IntegerConstraints {
  min?: number;
  max?: number;
}

interface NatConstraints {
  max?: number;
}

Usage Examples:

import { integer, nat, maxSafeInteger, property, assert } from 'fast-check';

// Generate integers in custom range
assert(
  property(integer({ min: 0, max: 100 }), (n) => {
    return n >= 0 && n <= 100;
  })
);

// Natural numbers
assert(
  property(nat(), (n) => {
    return n >= 0;
  })
);

// Safe integers for precise arithmetic
assert(
  property(maxSafeInteger(), maxSafeInteger(), (a, b) => {
    return Number.isSafeInteger(a + b) || Math.abs(a + b) > Number.MAX_SAFE_INTEGER;
  })
);

Floating Point Values

Generate 32-bit or 64-bit floating point numbers with fine-grained control.

/**
 * Generate 32-bit floating point numbers
 * @param constraints - Min/max bounds and special value control
 * @returns Arbitrary generating float32 values
 */
function float(constraints?: FloatConstraints): Arbitrary<number>;

/**
 * Generate 64-bit floating point numbers
 * @param constraints - Min/max bounds and special value control
 * @returns Arbitrary generating float64 values
 */
function double(constraints?: DoubleConstraints): Arbitrary<number>;

interface FloatConstraints {
  min?: number;
  max?: number;
  minExcluded?: boolean;
  maxExcluded?: boolean;
  noDefaultInfinity?: boolean;
  noNaN?: boolean;
  noInteger?: boolean;
}

interface DoubleConstraints {
  min?: number;
  max?: number;
  minExcluded?: boolean;
  maxExcluded?: boolean;
  noDefaultInfinity?: boolean;
  noNaN?: boolean;
  noInteger?: boolean;
}

Usage Examples:

import { float, double, property, assert } from 'fast-check';

// Float in range without special values
assert(
  property(
    float({ min: 0, max: 1, noNaN: true, noDefaultInfinity: true }),
    (f) => {
      return f >= 0 && f <= 1 && !isNaN(f) && isFinite(f);
    }
  )
);

// Double with NaN and infinity
assert(
  property(double(), (d) => {
    return typeof d === 'number';
  })
);

BigInt Values

Generate arbitrary-precision integer values.

/**
 * Generate bigint values between min and max (inclusive)
 * @param constraints - Min/max bounds
 * @returns Arbitrary generating bigint values
 */
function bigInt(constraints?: BigIntConstraints): Arbitrary<bigint>;

interface BigIntConstraints {
  min?: bigint;
  max?: bigint;
}

Usage Example:

import { bigInt, property, assert } from 'fast-check';

// Large integer arithmetic
assert(
  property(
    bigInt({ min: 0n, max: 10n ** 100n }),
    bigInt({ min: 0n, max: 10n ** 100n }),
    (a, b) => {
      return (a + b) - b === a;
    }
  )
);

String Values

Generate strings with configurable character sets and lengths.

/**
 * Generate strings with configurable unit type and length
 * @param constraints - Length, size, and unit type constraints
 * @returns Arbitrary generating strings
 */
function string(constraints?: StringConstraints): Arbitrary<string>;

/**
 * Generate valid base64 encoded strings
 * @param constraints - Length and size constraints
 * @returns Arbitrary generating base64 strings
 */
function base64String(constraints?: StringSharedConstraints): Arbitrary<string>;

/**
 * Generate strings matching a regular expression
 * @param constraints - Regular expression pattern
 * @returns Arbitrary generating matching strings
 */
function stringMatching(constraints: StringMatchingConstraints): Arbitrary<string>;

/**
 * Generate lorem ipsum style text
 * @param constraints - Word and sentence count constraints
 * @returns Arbitrary generating lorem text
 */
function lorem(constraints?: LoremConstraints): Arbitrary<string>;

/**
 * Transform a string arbitrary to toggle letter casing randomly
 * @param stringArb - Source string arbitrary
 * @param constraints - Toggle chance and case preservation
 * @returns Arbitrary generating mixed case strings
 */
function mixedCase(
  stringArb: Arbitrary<string>,
  constraints?: MixedCaseConstraints
): Arbitrary<string>;

interface StringConstraints {
  minLength?: number;
  maxLength?: number;
  size?: SizeForArbitrary;
  unit?: 'grapheme' | 'grapheme-composite' | 'grapheme-ascii' | 'binary' | 'binary-ascii';
}

interface StringSharedConstraints {
  minLength?: number;
  maxLength?: number;
  size?: SizeForArbitrary;
}

interface StringMatchingConstraints {
  regex: RegExp | string;
  size?: SizeForArbitrary;
}

interface LoremConstraints {
  maxCount?: number;
  mode?: 'words' | 'sentences';
  size?: SizeForArbitrary;
}

interface MixedCaseConstraints {
  toggleCase?: (rawChar: string) => string;
  untoggleAll?: (toggledString: string) => string;
}

Usage Examples:

import { string, base64String, stringMatching, lorem, mixedCase, property, assert } from 'fast-check';

// Strings with length constraints
assert(
  property(string({ minLength: 5, maxLength: 10 }), (s) => {
    return s.length >= 5 && s.length <= 10;
  })
);

// Base64 strings
assert(
  property(base64String(), (s) => {
    // Should decode without error
    const decoded = Buffer.from(s, 'base64').toString('base64');
    return true;
  })
);

// Pattern matching
assert(
  property(stringMatching({ regex: /^[a-z]{3,8}$/ }), (s) => {
    return /^[a-z]{3,8}$/.test(s);
  })
);

// Lorem ipsum
assert(
  property(lorem({ maxCount: 3, mode: 'sentences' }), (text) => {
    return text.split('.').length <= 4; // maxCount + 1
  })
);

// Mixed case
assert(
  property(mixedCase(string({ unit: 'grapheme-ascii' })), (s) => {
    return typeof s === 'string';
  })
);

Falsy Values

Generate JavaScript falsy values.

/**
 * Generate falsy JavaScript values
 * @param constraints - Which falsy values to include
 * @returns Arbitrary generating falsy values (false, null, undefined, 0, '', NaN)
 */
function falsy(constraints?: FalsyContraints): Arbitrary<FalsyValue>;

interface FalsyContraints {
  withBigInt?: boolean;
}

type FalsyValue = false | null | undefined | 0 | '' | typeof NaN | 0n;

Usage Example:

import { falsy, property, assert } from 'fast-check';

// Test truthy/falsy behavior
assert(
  property(falsy(), (value) => {
    return !value; // All falsy values should be falsy
  })
);

Date Values

Generate Date instances within a specified range.

/**
 * Generate Date instances within a specified range
 * @param constraints - Min/max date and validity constraints
 * @returns Arbitrary generating Date objects
 */
function date(constraints?: DateConstraints): Arbitrary<Date>;

interface DateConstraints {
  min?: Date;
  max?: Date;
  noInvalidDate?: boolean;
}

Usage Examples:

import { date, property, assert } from 'fast-check';

// Dates in specific range
assert(
  property(
    date({ min: new Date('2020-01-01'), max: new Date('2025-12-31') }),
    (d) => {
      return d >= new Date('2020-01-01') && d <= new Date('2025-12-31');
    }
  )
);

// Valid dates only
assert(
  property(date({ noInvalidDate: true }), (d) => {
    return !isNaN(d.getTime());
  })
);

Type Definitions

type SizeForArbitrary =
  | 'xsmall'
  | 'small'
  | 'medium'
  | 'large'
  | 'xlarge'
  | '='
  | number
  | { min?: number; max?: number };