Cookbook
Short recipes for common test-data needs.
An entity with a known id
const user = Forger.createWith<User>()
.with(u => u.id = 42)
.result()!;
service.register(user);
expect(service.findById(42)).toEqual(user);
A generated value as the expected constant
const expectedName = Forger.create<string>()!;
const dto = Forger.createWith<UserDto>()
.with(d => d.name = expectedName)
.result()!;
expect(mapper(dto).displayName).toBe(expectedName);
An array of exactly N items
const top = Forger.create<Player[]>({ arrayLength: 10 })!;
expect(top).toHaveLength(10);
A date within the last week
const weekAgo = Date.now() - 7 * 24 * 3600 * 1000;
const event = Forger.create<Event>({
dateMin: new Date(weekAgo),
dateMax: new Date(),
})!;
A small positive integer (qty, page size, age)
const qty = Forger.create<number>({ numberMin: 1, numberMax: 9 })!;
A human-readable code (no special characters)
const code = Forger.create<string>({
stringLength: 8,
stringSpecial: false,
})!;
// 'kW9pLm3t' — letters and digits only
One arm of a discriminated union
type Result = { ok: true; value: string } | { ok: false; error: string };
const failure = Forger.createWith<Result>()
.with(r => r.ok = false)
.with(r => r.error = 'NETWORK')
.result()!;
A real Map / Date / function where behavior matters
const cache = new Map<string, number>();
const repo = Forger.createWith<UserRepository>()
.with(r => r.cache = cache)
.result()!;
repo.save(user); // writes through to the real map
expect(cache.get(user.id)).toBeDefined();
A mixed spy + forged dependency
const api = Forger.createWith<AuthApi>()
.with(a => a.login = jest.fn().mockResolvedValue({ token: 't' }))
.result()!;
A tree with a known shape
const leaf = Forger.create<TreeNode>()!;
const root = Forger.createWith<TreeNode>()
.with(t => t.children = [leaf, leaf])
.result()!;
expect(countNodes(root)).toBe(3);
A full object graph with bounded numbers everywhere
interface Meter { value: number; history: number[] }
const meter = Forger.create<Meter>({ numberMin: 0, numberMax: 100, arrayLength: 5 })!;
// value in [0, 100]; history: 5 numbers, each in [0, 100]
08 September 2026