Vitest + Drizzle.
npm install vitest-drizzle-postgres
// vitest.setup.ts
/// <reference types="vitest-drizzle-postgres/types" />
import { setupTestDb, useTestDb, cleanupTestDb, teardownTestDb } from 'vitest-drizzle-postgres';
import { beforeAll, beforeEach, afterEach } from 'vitest';
import { db } from './src/db'; // Your existing database connection
import * as schema from './src/schema';
beforeAll(async () => {
await setupTestDb({
schema,
db,
migrationsFolder: "./migrations", // optional
});
return async () => {
await teardownTestDb();
}
});
beforeEach(async (ctx) => {
await useTestDb(ctx);
return async () => {
await cleanupTestDb();
};
});
// user.test.ts
import { describe, test, expect } from 'vitest';
import { users } from './schema';
import { db } from './src/db'; // Your existing database connection
describe('User tests', () => {
test('should create and find user', async () => {
const [user] = await db
.insert(users)
.values({ name: 'John', email: '[email protected]' })
.returning();
expect(user.name).toBe('John');
const foundUsers = await db.select().from(users);
expect(foundUsers).toHaveLength(1);
});
test('should not see data from previous test', async () => {
const users = await db.select().from(users);
expect(users).toHaveLength(0); // Clean slate
});
});
- Uses PostgreSQL savepoints for fast test isolation
- Schema change detection and caching
- Works with your existing Drizzle database connection
- Supports both savepoint and truncate modes
- Minimal setup required
packages/vitest-drizzle-postgres
- Core librarypackages/vitest-drizzle-postgres-tests
- Test suite
This is a pnpm monorepo with Turborepo for build orchestration:
packages/vitest-drizzle-postgres
- Core librarypackages/vitest-drizzle-postgres-tests
- Test suite
pnpm install
pnpm build
pnpm test
This project uses Changesets for version management and publishing. See PUBLISHING.md for detailed instructions.
Quick workflow:
pnpm changeset # Create a changeset
pnpm changeset:version # Update versions
pnpm changeset:publish # Publish to npm
See individual package READMEs for detailed documentation.