Registry / testing / fishery

fishery

JSON →
library2.4.0jsnpmunverified

Fishery is a JavaScript and TypeScript library designed to streamline the creation of test data objects, heavily inspired by the Ruby `factory_bot` gem. It simplifies the process of generating complex objects with sensible default values, while allowing for easy overrides to suit specific test cases. The library is currently at version 2.4.0 and maintains an active release cadence, with multiple minor and patch updates rolled out over the past year, indicating consistent development and bug fixes. Its core differentiator lies in its strong TypeScript support, providing comprehensive type-checking during factory definition and object instantiation. This ensures type safety and data validity within testing environments, making it a robust choice for projects utilizing TypeScript. It supports both synchronous `build` operations and asynchronous `create` operations, enabling integration with database or other async data persistence layers.

npm install fishery
INSTALL
IMPORT
SIG · FISHERY
F
fishery
testingjavascriptv2.4.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Factory
import { Factory } from 'fishery';
const { Factory } = require('fishery');
While CommonJS `require` might work in some transpiled environments, `fishery` ships with ES Modules, and `import` is the idiomatic way, especially in TypeScript projects.
DeepPartialObject
import type { DeepPartialObject } from 'fishery';
import { DeepPartialObject } from 'fishery';
This is a TypeScript type, so `import type` is the correct and recommended syntax for clarity and to ensure it's stripped during compilation. It was explicitly exported starting from v2.3.1 to resolve TypeScript errors.
define
import { Factory } from 'fishery'; Factory.define(...);
import { define } from 'fishery';
`define` is a static method of the `Factory` class, not a direct export. You must import `Factory` and then call `Factory.define`.

This example demonstrates how to define a basic `User` factory and a nested `Post` factory. It shows synchronous object building (`build` and `buildList`), parameter overrides, and how default values are merged with provided parameters, including the use of `sequence` for unique IDs.

import { Factory } from 'fishery'; interface Post { id: number; title: string; content: string; } interface User { id: number; name: string; email: string; address: { city: string; state: string; country: string }; posts: Post[]; } // A dummy post factory for demonstration const postFactory = Factory.define<Post>(({ sequence }) => ({ id: sequence, title: `Post ${sequence}`, content: `Content for post ${sequence}`, })); const userFactory = Factory.define<User>(({ sequence }) => ({ id: sequence, name: 'Default User', email: `user-${sequence}@example.com`, address: { city: 'Austin', state: 'TX', country: 'USA' }, posts: postFactory.buildList(2), })); // Build a user with some overrides const user = userFactory.build({ name: 'Susan Smith', address: { city: 'El Paso' }, email: 'susan.smith@example.com', }); console.log(user.name); // Susan Smith console.log(user.address.city); // El Paso console.log(user.address.state); // TX (from factory default) console.log(user.posts.length); // 2 // Example of creating multiple users const users = userFactory.buildList(3, { address: { country: 'Canada' } }); console.log(users[0].address.country); // Canada console.log(users[1].email); // user-2@example.com (sequence increments)
Debug
Known issues
breakingIn Fishery v2.0.0, the `onCreate` hook was changed to only allow a single definition per factory. If you previously defined multiple `onCreate` hooks for a single factory, only the last one defined would be used. For scenarios requiring multiple post-creation actions, you should now use the `afterCreate` hook (if introduced, or chain logic within the single `onCreate`).
fix
Refactor factories to have a single `onCreate` hook, or use the `afterCreate` hook if chaining post-creation logic.
affects: >=2.0.0
gotchaVersion 2.2.0 had potential build issues. It's recommended to skip directly to 2.2.1 or newer to avoid any unexpected behavior related to the package's distribution.
fix
Upgrade to `fishery@2.2.1` or any subsequent version (e.g., `npm install fishery@latest`).
affects: =2.2.0
gotchaPrior to v2.2.2, directly modifying the `params` object within your factory definition could lead to unintended side effects or inconsistencies in the built objects, as the object might not have been properly cloned before merging.
fix
Upgrade to `fishery@2.2.2` or later. If upgrading is not immediately possible, avoid direct mutation of the `params` object; instead, return a new object with desired modifications or use spread syntax to create a new object.
affects: <2.2.2
gotchaBefore v2.4.0, there was a known issue with the merging of `Buffer` objects, which could lead to incorrect or unexpected data in your generated test objects when `Buffer` instances were part of the parameters.
fix
Upgrade to `fishery@2.4.0` or a newer version to ensure correct handling of `Buffer` objects during parameter merging.
affects: <2.4.0
Errors
Common errors & fixes
Property 'foo' does not exist on type 'User'.
Attempting to access a property on a built object that is not defined in the corresponding TypeScript interface for the factory's return type.
fix
Ensure that the property `foo` is defined in the `User` interface, or access only properties that are explicitly part of the interface. This is a core benefit of TypeScript, catching type mismatches early.
Argument of type '{ foo: string; }' is not assignable to parameter of type 'Partial<User>'.
Passing parameters to `factory.build()` or `factory.create()` that include properties not defined in the factory's primary type (`User` in this example), or with incorrect types.
fix
Only pass properties that exist on the `User` interface (or a `DeepPartial` of it) and ensure their types are compatible. If you need to pass data that doesn't become part of the final object, use `transient` parameters via the second argument to `build` (e.g., `userFactory.build({}, { transient: { foo: 'bar' } })`).
Cannot find name 'DeepPartialObject'.
Attempting to import or use the `DeepPartialObject` type in TypeScript environments before it was officially exported, or in versions prior to its introduction as an export.
fix
Ensure your `fishery` package version is `2.3.1` or higher. If you are on an older version and cannot upgrade, you might need to manually define a similar utility type in your project.
Upgrade
Version history
2.4.0latest on npm
Audit
Dependencies
lodash.mergewithrequiredUsed internally for deep merging of parameters when building objects.
Agent activity
2 hits · last 30 days
node
2
Resources
fishery — npm install fishery · libregistry