Registry / testing / factory-girl-ts

factory-girl-ts

JSON →
library2.3.1jsnpmunverified

factory-girl-ts is a dedicated TypeScript library designed for generating test data, serving as a modern, actively maintained alternative to the unmaintained factory-girl package. Currently at version 2.3.1, it sees a steady release cadence with minor feature additions and bug fixes every few weeks to months. Its core purpose is to streamline the creation of complex test data, especially for projects utilizing popular ORMs such as Sequelize, TypeORM, and MikroORM, for which it provides dedicated adapters. Key differentiators include its TypeScript-first design, intuitive API for defining factories and managing associations, and robust support for asynchronous data creation operations, making it highly suitable for database-backed tests. The library supports both repository and active record patterns, offering flexibility in how test models are instantiated and persisted.

testingdatabase
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.

FactoryGirl is a named export. ESM-only usage is expected for Node.js environments >= 18.16.0.

import { FactoryGirl } from 'factory-girl-ts';

Adapters like SequelizeAdapter are named exports from the main package and are used to integrate with specific ORMs.

import { SequelizeAdapter } from 'factory-girl-ts';

The define method is a static method on the FactoryGirl class, not a top-level export.

FactoryGirl.define(User, defaultAttributesFactory);

This quickstart demonstrates how to set up `factory-girl-ts` with a mock Sequelize model, define a factory with default attributes, and use its `build`, `create`, and `createMany` methods to generate test data.

import { User } from './models/user'; import { FactoryGirl, SequelizeAdapter } from 'factory-girl-ts'; // A dummy User model for demonstration purposes class User { id!: number; name!: string; email!: string; address!: { state: string; country: string }; // Mimic Sequelize's static methods static build(attributes: Partial<User>): User { return Object.assign(new User(), attributes); } static async create(attributes: Partial<User>): Promise<User> { console.log('Creating user in DB:', attributes); const user = Object.assign(new User(), attributes); user.id = Math.floor(Math.random() * 1000) + 1; // Simulate ID from DB return Promise.resolve(user); } } // Step 1: Specify the adapter for your ORM. // For actual Sequelize usage, ensure SequelizeAdapter is correctly configured with a model. FactoryGirl.setAdapter(new SequelizeAdapter()); // Step 2: Define your factory with default attributes for the model. const defaultAttributesFactory = () => ({ name: 'John', email: 'some-email@mail.com', address: { state: 'Some state', country: 'Some country', }, }); const userFactory = FactoryGirl.define(User, defaultAttributesFactory); // Step 3: Use the factory to create instances of the model. async function runExample() { const defaultUser = await userFactory.build(); console.log('Built default user:', defaultUser); const createdUser = await userFactory.create({ email: 'new-user@example.com' }); console.log('Created user in DB:', createdUser); const manyUsers = await userFactory.createMany(2, { name: 'Bulk User' }); console.log('Created many users:', manyUsers); } runExample();
Debug
Known footguns
breakingUsers migrating from the original 'factory-girl' package should be aware that 'factory-girl-ts' is a separate, modern rewrite. It is not a drop-in replacement and requires a full migration of existing factory definitions due to API and internal implementation differences.
gotchaAll factory instance methods like `build()`, `create()`, `buildMany()`, and `createMany()` are asynchronous and return Promises. Failing to `await` these calls will lead to unhandled promise rejections or tests completing before data is ready.
breakingThe `additional parameters type` feature was removed in v2.2.0, which might impact custom adapter implementations or advanced factory configurations that relied on this specific typing for additional parameters.
gotcha`factory-girl-ts` is designed for modern JavaScript environments (Node.js >=18.16.0), implying a preference for ECMAScript Modules (ESM). Using CommonJS `require()` for imports will not work correctly for its named exports.
gotchaPrior to v2.3.1, a bug could cause factories to inadvertently create unused initial associations, potentially leading to unnecessary database operations or test data pollution.
Upgrade
Version history

Breaking-change detection hasn't run for this library yet.

Audit
Security & dependencies

CVE tracking and dependency tree are planned for a later release.

Agent activity
10 hits · last 30 days
gptbot
4
ahrefsbot
4
script
1
chatgpt-user
1
Resources