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.
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
muslnode 18–226 runs
build_error
glibcnode 18–226 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();
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.