Skip to content

A Node.js library for data seeding, making data preparation for integration testing easier.

License

Notifications You must be signed in to change notification settings

chehsunliu/seeder.js

Repository files navigation

Seeder.js

Test NPM Version

Seeder.js is a Node.js library for data seeding, making data preparation for integration testing easier.

Supported Services

Getting Started

Install Seeder.js:

npm install -D @chehsunliu/seeder @chehsunliu/seeder-mysql

Configure the seeders in setup.ts, which should be loaded in Jest setupFilesAfterEnv or in Vitest setupFiles:

import { seederManager } from "@chehsunliu/seeder";
import { MySqlSeeder } from "@chehsunliu/seeder-mysql";

seederManager.configure([
  new MySqlSeeder({
    connection: {
      host: "127.0.0.1",
      port: 3306,
      user: "root",
      password: "xxx",
      database: "demo",
    },
    dataSelectors: [
      // The order matters. A table will only pick the first matched selector.
      { type: "sql", getFilename: (tableName: string) => `${tableName}.sql` },
      { type: "json", getFilename: (tableName: string) => `${tableName}.json` },
    ],
    excludedTables: ["flyway_schema_history"],
  }),
]);

afterAll(async () => {
  await seederManager.release();
});

Put some test data in data/users.json:

[
  { "id": 1, "username": "alice" },
  { "id": 2, "username": "bob" }
]

Invoke the seeders in tests:

import { seederManager } from "@chehsunliu/seeder";

beforeEach(async () => {
  await seederManager.truncate();
  await seederManager.seed(path.join(__dirname, "data"));
});

test("blah blah blah", () => {
  // Data should be available here.
});

Custom Seeder

You can also create one if you want:

import { Seeder } from "@chehsunliu/seeder";

class MySeeder implements Seeder {
  truncate = async (): Promise<void> => {};
  seed = async (folder: string): Promise<void> => {};
  release = async (): Promise<void> => {};
}