Forger Help

Pinning Values with createWith

Forger.createWith<T>() generates a full fake and then lets you pin specific properties to concrete values — the two API halves complement each other: create for filler, createWith for the data a test is actually about.

The chain: with() … result()

interface Order { id: number; status: 'new' | 'paid' | 'shipped'; total: number; } const order = Forger.createWith<Order>() .with(o => o.status = 'paid') .result()!; // { id: 417, status: 'paid', total: 295 }
  • .with(expr) assigns one property inside the lambda — one assignment per call; chain several with() calls for several properties.

  • .result() must be called after all with() calls; it returns the forged T (T | undefined, same as create).

const student = Forger.createWith<Student>() .with(s => s.name = 'John Doe') .with(s => s.age = 42) .result()!;

Everything unpinned stays generated

Properties you do not touch are forged as usual — including nested objects:

interface Inner { prop: string } interface Test { prop: string; num: number; inner: Inner } const test = Forger.createWith<Test>() .with(t => t.prop = 'FIXED') .result()!; // { prop: 'FIXED', num: 818, inner: { prop: '>fw0JsyvK1' } }

Pinning applies to the root type only

The pinned property is excluded from generation for the root instance — nothing else:

  • the same property name in nested objects of another type is still generated;

  • nested instances of the same type are generated from scratch too.

interface Inner { prop: string } interface Test { prop: string; inner: Inner; sibling?: Test } const test = Forger.createWith<Test>() .with(t => t.prop = 'ROOT') .result()!; // { // prop: 'ROOT', // inner: { prop: 'generated' }, // same name, other type — generated // sibling: { prop: 'generated' }, // same type, nested — generated // }

To pin a nested value, reach into it directly:

const test = Forger.createWith<Test>() .with(t => t.prop = 'ROOT') .with(t => t.inner.prop = 'INNER') .result()!;

Forging inside with()

The right side of an assignment may itself use Forger:

const expected = Forger.create<string>()!; const box = Forger.createWith<{ value: string }>() .with(b => b.value = expected) .result()!; expect(box.value).toBe(expected); // generated once, reused as the expectation

Typical uses

  • Pin a discriminator (status, kind) to test one arm of a union.

  • Pin an identifier that the test looks up later.

  • Pin built-in structures (Map, Date with exact time, functions) that should behave, not just type-check — see Caveats.

API details: CreateWithModel.

08 September 2026