Skip to content
Logo

Compared to other libraries

Similar libraries can be categorized as schema-derived mocks, factory libraries, and property-based testing. They differ in where the shape is defined and whether a run can be repeated.

Factory libraries are the closest, and the only group where a feature-by-feature comparison is useful:

fabricatorrosiefactory-girlfisherycooky-cutter@factory-js/factory@travelperksl/fabricator
Where the shape livesschemafactory definitionfactory definitionfactory definitionfactory definitionfactory definitionfactory definition
Result typederived from the schema; .satisfies<T>() checks a supplied typesupplied T, not enforcedsupplied T, not enforcedsupplied T, enforced in fullsupplied T, enforced in fullinferred from the definitionsupplied T, not enforced
Unpinned fields varyevery buildonly what you writeonly what you writeonly what you writeonly what you writeonly what you writeonly what you write
Stated bounds, distributionyes——————
Replay a runclock (+ optional salt)——————
Counter stateper-instance; per-test with the harnessglobal, resetAll()named, factory.sequence()per-factory, rewindSequence()per-factory, resetSequence()per-factory, seq()sequence()
Enumerate the output spacecoverage / combinatorial——————
Params kept out of the output—optionbuildOptionstransient—vars—
Async persistence hook——create + ORM adapterscreate / onCreate—create + Prisma—

The dashes in fabricator's column mean what they mean everywhere else. Fabricator can't pass a parameter that steers generation without it appearing in the output, and it doesn't persist what it builds.

Schema-derived mocks

@anatine/zod-mock generates data from a Zod schema you already have. If your application already validates with Zod, that is a real advantage: one definition serves validation and test data, with nothing extra to write.

The downside is that generation is guessed from a schema written for something else. Values are picked mostly by matching the field's name to faker's function names, so a field called email gets an email because of its name, not because the schema says so. Its own README lists ten Zod types it doesn't handle, including ZodTuple and ZodUnion.

Fabricator works the other way around. The schema exists to describe generation, so every primitive fully supports generating, and bounds are stated instead of guessed. For validation, you adapt the schema outward to TypeBox. Use zod-mock when you already have Zod schemas and want test data from them for free. Use fabricator when generating data is the schema's real job.

Factory libraries

This is the largest group. Every library in it follows the pattern in The trouble with factories: a shape is code, with a value or a function per field, and a variation is defined by what it overrides. They differ mostly in how much they use the type system.

The untyped ports

rosie is the most widely used, and its README calls it "inspired by factory_girl." You register a factory by name in a global registry and build it by name:

Factory.define("player")
  .sequence("id")
  .sequence("name", (i) => `player${i}`)
  .attr("position", ["id"], (id) => positions[id % positions.length]);
 
Factory.build("player", { position: "pitcher" });

.attr(name, deps, fn) makes one field depend on another, .after() changes the built object after the fact, and .option() declares a build parameter that steers generation without appearing in the output.

factory-girl is the other direct port of the Ruby original, and it extends the idea to the database. Its adapters for Mongoose, Bookshelf, and Sequelize let factory.create() save through your ORM and factory.cleanUp() remove it again. It also has factory.assoc() for associations and factory.chance() for realistic values. Fabricator does none of this. Its last release was in 2018.

Neither ships types. rosie's published package is a single .js file, and TypeScript users install @types/rosie or @types/factory-girl separately. Both check the attributes you do define against a type you supply, but never require the rest. So Factory.define<User>("user").attr("id", 1).build() is typed User, but the object it produces is { id: 1 }. factory-girl's types are looser still: build<T>() takes its type at the call site, with no connection to the definition.

The typed ones

Three libraries close that gap.

cooky-cutter is built around a completeness check: "whenever the entity type changes, the factories become invalid." Its config is a mapped type over every key of the interface you give it, so leaving one out is a compile error:

const user = define<User>({
  id: random,
  firstName: (i) => `Bob #${i}`,
  lastName: "Smith",
  age: sequence,
});

fishery gets the same guarantee from a definition function that must return the full type. It rejects unknown keys at build() and types its transient params and associations. It comes from thoughtbot, the authors of factory_bot, and keeps the parts of the Ruby original that work well in JavaScript, including create()/onCreate() for factories that save what they build.

@factory-js/factory stands out because it takes no target type at all. define({ props }) infers the result from the props you wrote, so the type follows the definition instead of being checked against one declared elsewhere. Fabricator works the same way. It also has vars (values used to compute props that don't appear in the output), traits via .use(), and a Prisma plugin.

@travelperksl/fabricator is unrelated, despite the name. It is a smaller version of the pattern and has the same gap as rosie: Fabricator<User>({ id: () => 1 }) type-checks under strict and its result is typed User.

What none of them do

A field is whatever its function returns. Nothing in a definition states a field's bounds or distribution, and nothing varies unless you write code that varies it. rosie's first README example defines a field as Math.random(), and cooky-cutter ships a random helper documented as useful "to avoid tests passing as a result of ordering." That is the same reason fabricator varies every field you don't pin: a test shouldn't pass because of a value it never set (see A test depends on what it pins). But their randomness is unseeded, so when it does catch a test like that, the failing run can't be repeated.

What a definition does carry is counter state. Every library here has a sequence, each sequence is a counter stored on the factory, and so the id a test sees depends on how many objects were built before it. That is why rosie has resetAll(), fishery has rewindSequence(), and cooky-cutter has resetSequence(). It is also why factory-js's README warns that under Vitest or Jest, values from seq "may not be unique across workers." This is the trouble with plain random data again: the counter keeps counting across tests, so the same test gets different values depending on how many tests ran before it.

The underlying difference is in kind, not degree. A factory is code that returns an object. A fabricator Schema describes a set of possible values. That is why a Schema can be bounded with .whereby(), enumerated with coverage/combinatorial, and replayed from a clock, while a factory returns whatever its function returned this time. Type inference doesn't close this gap: factory-js derives a type from the definition, but still can't tell you which values a field may take.

The gap goes the other way too. Four of these libraries can pass a parameter that steers generation without it appearing in the output: rosie's option, fishery's transient, factory-js's vars, and factory-girl's buildOptions. Fabricator has no equivalent. The closest is .refine(), which computes a field into the result. Three of them save what they build, and fabricator doesn't.

Property-based testing

fast-check solves a different problem. It generates inputs to falsify a property, and when it finds a failing input it shrinks it to the smallest input that still fails. Shrinking is the core feature, and fabricator has nothing like it.

Fabricator answers a different question: what does one plausible instance of a shape look like, and how do you get it back? The two complement each other. If your goal is to show an invariant holds for all inputs, use fast-check.

When not to use fabricator

  • You need validation. Fabricator doesn't check values against a schema. Fabricated data has the right shape by construction. To validate, adapt the schema to a validation library. There is an official adapter for TypeBox (v0), with others planned.

Limitations has the full list.

Next

Go to installation, then the quick start for the whole loop on one page. Mental model explains why Registry, Schema, and Fabricator are separate.