makeit-factory
v1.0.0-rc.4
Published
Simplify the creation of fake data within your database seeders or tests with factories.
Downloads
22
Maintainers
Readme
MakeIt Factory
Installation
npm install --save-dev makeit-factory
pnpm add -D makeit-factory
yarn add -D makeit-factory
Examples
Define UserFactory
// database/factories/UserFactory.ts
import { defineFactory } from 'makeit-factory'
export const UserFactory = defineFactory(() => ({
name: faker.pearson.fullName(),
email: faker.internet.email(),
active: faker.datatype.boolean()
}))
.defineState('active', () => ({ active: true }))
.defineState('disabled', () => ({ active: false }))
.getFactory();
Seeding your database
// database/seeders/UserSeeder.ts
import { UserFactory } from '../factories/UserFactory'
export default async () => {
const activeUsers = UserFactory.useState('active').makeMany(5);
const disabledUsers = UserFactory.useState('disabled').makeMany(5);
const users = [...activeUsers, ...disabledUsers];
//seed your db with your orm of choice
await prisma.user.create({data: users});
}
Usage in tests
// tests/users.test.ts
import { describe, it, expect } from 'vitest'
import request from 'supertest';
import { UserFactory } from '../database/factories/UserFactory'
describe('User API', () => {
it('should respond with a list of users', async () => {
const users = UserFactory.makeMany(10);
await prisma.user.create({ data: users });
const response = await request.get('/api/users').send();
expect(response.body).toHaveProp('data');
expect(response.body.data).toHaveLength(10);
});
})