Skip to content

DynamicFactory

Creates complete items from a function that builds default values.

Use this factory when defaults should be generated fresh for each item, such as when values need to be unique, random, or based on the requested overrides. The default maker receives the same overrides passed to make, then the overrides are applied to the generated defaults.

interface User {
id: string;
name: string;
active: boolean;
}
const userFactory = new DynamicFactory<User>(() => ({
id: crypto.randomUUID(),
name: "Test User",
active: true,
}));
const defaultUser = userFactory.make();
// { id: "...", name: "Test User", active: true }
const inactiveUser = userFactory.make({ active: false });
// { id: "...", name: "Test User", active: false }

Source file: src/dynamic/dynamic-factory.ts