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()
.with(expr)assigns one property inside the lambda — one assignment per call; chain severalwith()calls for several properties..result()must be called after allwith()calls; it returns the forgedT(T | undefined, same ascreate).
Everything unpinned stays generated
Properties you do not touch are forged as usual — including nested objects:
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.
To pin a nested value, reach into it directly:
Forging inside with()
The right side of an assignment may itself use Forger:
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,Datewith exact time, functions) that should behave, not just type-check — see Caveats.
API details: CreateWithModel.