@os-team/typeorm-seeder 
Simple seeder for seeding data in tests using TypeORM.
Usage
Install the package in devDependencies (-D) using the following command:
yarn add -D @os-team/typeorm-seederExample of test
import 'reflect-metadata';
import { Connection, createConnection } from 'typeorm';
import createSeeder, { Seeder } from '@os-team/typeorm-seeder';
import User from '../../entities/User';
import Payment from '../../entities/Payment';
let connection: Connection;
let seeder: Seeder;
beforeEach(async () => {
connection = await createConnection();
seeder = createSeeder(connection.manager); // Create the seeder
});
afterEach(async () => {
await connection.close();
});
it('Should change the payment status to succeeded', async () => {
// Seed a user
const userData = {
id: 1,
name: 'name',
};
await seeder.seed(User, userData);
// Seed payments
await seeder.seed(Payment, [
{
id: 1,
amount: 1000,
status: 'pending',
user: { id: userData.id },
},
{
id: 2,
amount: 2000,
status: 'pending',
user: { id: userData.id },
},
]);
// ...
});Most likely you will use multiple test files (e.g. one test file for testing one API method). In this case:
- Put
beforeEach,afterEachand global variables (connection, seeder) to a separate file. Export theseedervariable to use it in any test file. - Add the path to this file to setupFilesAfterEnv. Jest will run this code before each test file in the suite is executed.
- Set the
dropSchemaoption in the TypeORM config totrueONLY in the test environment. TypeORM drops the schema each time connection is being established (before each test file). See more about TypeORM connection options.