@farbenmeer/prisma-migrate-test
v0.3.0
Published
Factory functions for creating isolated test databases using the template-clone pattern: migrations run **once** to build a template, then each test gets a cheap clone — no repeated migration overhead.
Downloads
2,181
Keywords
Readme
@farbenmeer/prisma-migrate-test
Factory functions for creating isolated test databases using the template-clone pattern: migrations run once to build a template, then each test gets a cheap clone — no repeated migration overhead.
Installation
npm install @farbenmeer/prisma-migrate-testSQLite
Peer dependencies
npm install better-sqlite3 @prisma/adapter-better-sqlite3
npm install -D @types/better-sqlite3Usage
import { createSqliteTestDb } from "@farbenmeer/prisma-migrate-test/sqlite";
import { PrismaClient } from "@prisma/client";
import { afterAll, afterEach, beforeAll, beforeEach } from "vitest";
let testDb: SqliteTestDb;
let prisma: PrismaClient;
beforeAll(() => {
testDb = createSqliteTestDb({ migrationsPath: "prisma/migrations" });
});
afterAll(() => {
testDb.cleanup();
});
beforeEach(async () => {
prisma = new PrismaClient({ adapter: await testDb.getAdapter() });
});
afterEach(async () => {
await prisma.$disconnect();
});API
createSqliteTestDb(options?): SqliteTestDb
Runs all migrations once into an in-memory template, then provides cheap per-test clones written to a temp directory.
Parameters:
options.migrationsPath— path to the Prisma migrations folder (default:"prisma/migrations")
Returns: SqliteTestDb
getAdapter(): Promise<PrismaBetterSqlite3>— writes a clone of the template to disk and returns a new adapter backed by itcleanup(): void— deletes the temp directory
PGlite
Peer dependencies
npm install @electric-sql/pglite pglite-prisma-adapterUsage
Configure your Prisma schema to use the postgresql provider and generate the client with the pglite previewFeature.
import { createPgliteTestDb } from "@farbenmeer/prisma-migrate-test/pglite";
import { PrismaClient } from "@prisma/client";
import { afterAll, afterEach, beforeAll, beforeEach } from "vitest";
let testDb: PgliteTestDb;
let prisma: PrismaClient;
beforeAll(() => {
testDb = createPgliteTestDb({ migrationsPath: "prisma/migrations" });
});
afterAll(async () => {
await testDb.cleanup();
});
beforeEach(async () => {
prisma = new PrismaClient({ adapter: await testDb.getAdapter() });
});
afterEach(async () => {
await prisma.$disconnect();
});API
createPgliteTestDb(options?): PgliteTestDb
Runs all migrations once into an in-memory PGlite instance and dumps the result, then provides cheap per-test clones restored from that dump.
Parameters:
options.migrationsPath— path to the Prisma migrations folder (default:"prisma/migrations")options.extensions— PGlite extensions to pass to each instance (default:{})options.seed— optional seed data applied after migrations. Pass a SQL string or an async function(adapter: PrismaPGlite) => Promise<void>to seed via Prisma. The seed runs once when the template is built, so every clone includes the seeded data.
Returns: PgliteTestDb
getAdapter(): Promise<PrismaPGlite & { reset(): Promise<void> }>— restores a clone from the dump and returns a new adapter with areset()method that truncates all public tablescleanup(): Promise<void>— closes all open PGlite instances
Seeding
// Seed with raw SQL
const testDb = createPgliteTestDb({
seed: `INSERT INTO "User" (email, name) VALUES ('[email protected]', 'Alice')`,
});
// Seed with a function (uses the Prisma adapter)
const testDb = createPgliteTestDb({
seed: async (adapter) => {
const prisma = new PrismaClient({ adapter });
await prisma.user.create({ data: { email: "[email protected]", name: "Alice" } });
await prisma.$disconnect();
},
});Resetting between tests
The adapter returned by getAdapter() includes a reset() method that truncates all public tables (except _prisma_migrations):
const adapter = await testDb.getAdapter();
const prisma = new PrismaClient({ adapter });
// ... run test ...
await adapter.reset(); // truncates all tablesLicense
MIT
