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 fisheryVerified import paths — ran on the pinned version, not inferred.
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.
Refactor factories to have a single `onCreate` hook, or use the `afterCreate` hook if chaining post-creation logic.
Upgrade to `fishery@2.2.1` or any subsequent version (e.g., `npm install fishery@latest`).
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.
Upgrade to `fishery@2.4.0` or a newer version to ensure correct handling of `Buffer` objects during parameter merging.
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.
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' } })`).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.