Skip to content
Logo

Quick start

Fabricator is most useful for test fixtures that exercise your code with inputs you didn't anticipate. That makes it a form of fuzzing.

import {  } from "@ghostry/fabricator";
 
const { ,  } = ();
 
const  = .({
  : ..({ : { : 1, : 25 } }),
  : ..({ : 1, : 500 }),
  : .,
  : ..,
  : .(..({ : { : 15 } })).({
    : { : 5 },
  }),
});
 
const  = new ();
 
const  = .();
// { name: "...", price: ..., inStock: ..., createdAt: ..., tags: [...] }

That's the whole loop:

  1. initialize(...) gives you T, a set of type builders, and Fabricator, a constructor that turns a Schema into something that produces values.
  2. T.object({ ... }) builds a Schema. A Schema is plain data that describes a shape, and building one draws no randomness.
  3. new Fabricator(schema) builds a Fabricator, which has a .fabricate() method.
  4. .fabricate() draws the values.

See Mental model for why there are three layers.

initialize() needs no configuration. It records the time of the call as its clock. That one number makes every run different, and it also replays any run. When something fails, log context.clock, then pass it back as clock to get that run's data again. See Reproducibility.

Overriding specific fields

To keep everything random except the fields you care about, pass those fields to .fabricate():

const item = Product.fabricate({ name: "Widget", inStock: true });

Every other field still draws from the same Fabricator. This override applies to one call and doesn't create a new schema. To bake overrides into a schema instead, use .override(...). See Composition.