Guide

Mock Data

Generate valid, type-shaped fake data for tests and fixtures.

Make some fake data

// packages/examples/src/guide/mocking-basics.ts
// createMockType -> a function that invents a fresh, valid User every call.
const mockUser = createMockType<User>();

const a = mockUser(); // {id: 91, name: 'qZ...', roles: ['user'], active: true}
const b = mockUser(); // a different one each time

// Whatever it produces passes the validator for the same type — by construction.
const isUser = createValidate<User>();
isUser(mockUser()); // true

Call the generator as many times as you like. You get fresh, valid data every time. Change the type and the mocks follow along, with no fixture to update.

createMockType needs the Vite plugin running to read your type. It throws a clear error if the plugin isn't active. It's a build-time tool, so reach for it in tests and seed scripts, not shipped runtime code.

Steer it with options

Pass a mock options bag to nudge the generated values: number ranges, string lengths, how often optionals show up, and more. Options merge in order: built-in defaults, then factory options, then per-call options.

// packages/examples/src/guide/mocking-options.ts
// Options can be set at the factory (apply to every call) or per call.
// They merge: defaults < factory < call.
const mockAccount = createMockType<Account>({
  mock: {
    minNumber: 0,
    maxNumber: 1000, // numbers land in [0, 1000]
    stringLength: 8, // every generated string is 8 chars
    optionalProbability: 1, // always include optional props like `tags`
  },
});

const rich = mockAccount({mock: {minNumber: 1_000_000}}); // override just for this call

A few of the knobs you'll reach for most:

OptionWhat it does
minNumber / maxNumberBounds for generated numbers and bigints.
stringLength / maxRandomStringLengthForce or cap generated string length.
arrayLength / maxRandomItemsLengthForce or cap array, Map and Set sizes.
optionalProbabilityChance from 0 to 1 that an optional property is included.
unionIndex / enumIndexPin a union member or enum branch instead of picking at random.

Format-aware mocks

If your type uses type formats, the mock respects them. An email field gets a real-looking address, and a uuidv4 field gets a real-looking UUID, not just random characters.

// packages/examples/src/guide/mocking-formats.ts
const mockContact = createMockType<Contact>();

const fake = mockContact();
// id is a real-looking UUID, email is a real-looking address —
// not just random strings. The mock is format-aware.
// {id: '3f2504e0-4f89-...', email: 'name@example.com', name: '...'}

This means a mocked value passes not just the structural validator but the format checks too. The full list of formats and how each one mocks lives in Type Formats.

Copyright © 2026