Seeder.js is a Node.js library for data seeding, making data preparation for integration testing easier.
- @chehsunliu/seeder-dynamodb
- @chehsunliu/seeder-ftp
- @chehsunliu/seeder-minio
- @chehsunliu/seeder-mysql
- @chehsunliu/seeder-postgres
- @chehsunliu/seeder-redis
- @chehsunliu/seeder-sftp
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.
});
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> => {};
}