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

objects.mddocs/arbitraries/

Object and Record Arbitraries

Object and record arbitraries generate JavaScript objects, records following schemas, dictionaries, and JSON data structures.

Capabilities

Record Generation

Generate objects following a typed schema with optional and required keys.

/**
 * Generate objects following a schema with optional/required keys
 * @param model - Schema mapping keys to arbitraries
 * @param constraints - Which keys are required
 * @returns Arbitrary generating record objects
 */
function record<T, K extends keyof T = keyof T>(
  model: { [K in keyof T]: Arbitrary<T[K]> },
  constraints?: RecordConstraints<K>
): Arbitrary<RecordValue<T, K>>;

interface RecordConstraints<K> {
  requiredKeys?: K[];
}

type RecordValue<T, K extends keyof T> = { [P in K]: T[P] } & Partial<T>;

Usage Example:

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

// User record with required and optional fields
const userArb = record(
  {
    id: string(),
    name: string(),
    age: integer({ min: 0, max: 120 }),
    email: string(),
    active: boolean(),
  },
  { requiredKeys: ['id', 'name'] }
);

assert(
  property(userArb, (user) => {
    return 'id' in user && 'name' in user;
  })
);

Object Generation

Generate arbitrary nested objects with configurable depth and complexity.

/**
 * Generate arbitrary nested objects with configurable depth and keys
 * @param constraints - Depth, key type, and size constraints
 * @returns Arbitrary generating arbitrary objects
 */
function object(constraints?: ObjectConstraints): Arbitrary<Record<string, unknown>>;

interface ObjectConstraints {
  key?: Arbitrary<string>;
  maxDepth?: number;
  maxKeys?: number;
  size?: SizeForArbitrary;
  depthIdentifier?: DepthIdentifier | string;
  values?: Arbitrary<unknown>[];
  withBigInt?: boolean;
  withBoxedValues?: boolean;
  withDate?: boolean;
  withMap?: boolean;
  withNullPrototype?: boolean;
  withObjectString?: boolean;
  withSet?: boolean;
  withTypedArray?: boolean;
}

Usage Example:

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

// Generate arbitrary objects
assert(
  property(object({ maxDepth: 2, maxKeys: 5 }), (obj) => {
    return typeof obj === 'object' && obj !== null;
  })
);

Dictionary Generation

Generate dictionary objects mapping string keys to arbitrary values.

/**
 * Generate dictionary objects (string keys to arbitrary values)
 * @param keyArb - Arbitrary for dictionary keys
 * @param valueArb - Arbitrary for dictionary values
 * @param constraints - Size and length constraints
 * @returns Arbitrary generating dictionary objects
 */
function dictionary<T>(
  keyArb: Arbitrary<string>,
  valueArb: Arbitrary<T>,
  constraints?: DictionaryConstraints
): Arbitrary<Record<string, T>>;

interface DictionaryConstraints {
  minKeys?: number;
  maxKeys?: number;
  size?: SizeForArbitrary;
}

Usage Example:

import { dictionary, string, integer, property, assert } from 'fast-check';

// String to number mapping
assert(
  property(dictionary(string(), integer(), { maxKeys: 10 }), (dict) => {
    return Object.keys(dict).length <= 10;
  })
);

Anything Generation

Generate any JavaScript value including primitives, objects, arrays, and special values.

/**
 * Generate any JavaScript value (primitives, objects, arrays, etc)
 * @param constraints - Same as object constraints
 * @returns Arbitrary generating any value
 */
function anything(constraints?: ObjectConstraints): Arbitrary<unknown>;

Usage Example:

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

// Test JSON.stringify doesn't crash
assert(
  property(anything(), (value) => {
    try {
      JSON.stringify(value);
      return true;
    } catch {
      return true; // Some values like circular refs can't be stringified
    }
  })
);

JSON Generation

Generate valid JSON strings and JSON-compatible values.

/**
 * Generate valid JSON strings
 * @param constraints - Depth and size constraints
 * @returns Arbitrary generating JSON strings
 */
function json(constraints?: JsonSharedConstraints): Arbitrary<string>;

/**
 * Generate JSON-compatible values (objects, arrays, primitives)
 * @param constraints - Depth and size constraints
 * @returns Arbitrary generating JSON values
 */
function jsonValue(constraints?: JsonSharedConstraints): Arbitrary<JsonValue>;

interface JsonSharedConstraints {
  maxDepth?: number;
  depthIdentifier?: DepthIdentifier | string;
}

type JsonValue =
  | null
  | boolean
  | number
  | string
  | JsonValue[]
  | { [key: string]: JsonValue };

Usage Examples:

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

// JSON strings are parseable
assert(
  property(json(), (jsonStr) => {
    const parsed = JSON.parse(jsonStr);
    return JSON.stringify(parsed) !== undefined;
  })
);

// JSON values can be stringified
assert(
  property(jsonValue({ maxDepth: 3 }), (value) => {
    const stringified = JSON.stringify(value);
    return JSON.parse(stringified) !== undefined;
  })
);

Type Definitions

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

type DepthIdentifier = { readonly name: string };