Literals
Literal types — 'asc', 42, true — represent fixed value sets. Forger rolls a random member of the set, which makes literal unions a natural way to forge constrained values.
Literal unions
Every string, numeric, and boolean literal in a union is a candidate, and each call rolls one:
Literal unions are a natural companion to enums: both roll a random member per call, and literal unions can be declared inline — see Enums for the comparison.
Literal properties
A property whose type is a literal union is forged the same way:
When a test is specifically about one member, pin it:
Standalone literals
A single non-union literal is a degenerate case: Forger does not resolve it, and the result is null.
There is nothing to generate from a type with exactly one inhabitant anyway — if a constant is needed, declare it directly or pin it with createWith. See Caveats.
Literal unions vs enums
Literal union | Enum (numeric/string) | |
|---|---|---|
Random member per call | yes | yes |
Falsy members ( | reachable | reachable |
Enum API ( | no | yes |
Needs a declaration statement | no (inline allowed) | yes |
Prefer literal unions inside test doubles; keep enums where production code benefits from them.