codepunkt/vitest-environment-prisma-postgres
codepunkt/vitest-environment-prisma-postgres has 42 GitHub stars and 4 forks. GitHot measured 0 stars in 24 hours and 0 in 7 days. Last checked 2026-11-22 12:04 UTC. Last push 2026-11-20.
What you can do
- Run integration tests against a real PostgreSQL instance.
- Seed your database once at the beginning of the test run.
- Tests run inside sandboxed transactions, but application-level transactions still work normally.
- Test transactions are rolled back after every test.
Gemessener Sternzuwachs
- in 24h
- 0
- in 7 days
- 0
- in 15 days
- Verlauf wird gesammelt
- in 1 month
- Verlauf wird gesammelt
Nettoänderungen in überlappenden Zeiträumen, keine Tagessummen. Fehlende Messwerte bleiben leer. Sterne zeigen Interesse, nicht Qualität.
Über uns
Vitest environment for Prisma & PostgreSQL designed for fast integration tests: seed your database once with production-like data, then run each test in a transaction that is rolled back after execution. Tests remain isolated without expensive reseeding.
DatenREADME.md
Original
vitest-environment-prisma-postgres
Features • Installation & Setup • TypeScript Configuration • Known Limitations
Motivation
Vitest environment for Prisma + PostgreSQL designed for fast integration tests.
Integration tests against a database are often slow because each test needs its own database state. Teams typically either:
- Reseed the database before every test, or
- Insert all required data inside each test.
Both approaches are slow, repetitive, and dominate test runtime.
This environment eliminates that cost.
You run migrations and seed your test database once. Each test then runs inside its own database transaction, which is rolled back automatically after the test finishes. Your tests stay isolated, realistic, and extremely fast, creating dedicated data for every test.
If you want to understand the full design rationale, implementation details, and performance benchmarks behind this environment, check out the in-depth article on my blog:
https://codepunkt.de/writing/blazing-fast-prisma-and-postgres-tests-in-vitest/
Features
- Run integration tests against a real PostgreSQL instance.
- Seed your database once at the beginning of the test run.
- Tests run inside sandboxed transactions, but application-level transactions still work normally.
- Test transactions are rolled back after every test.
- Tests are isolated, fast, and order independent.
Installation & Setup
Requires Node.js 20 or newer (CI covers 20.x, 22.x, 24.x).
Step 1: Install the environment
First, install the environnment:
npm install vitest-environment-prisma-postgres --save-dev
Step 2: Ensure peer dependency availability
Then, ensure that the required peer dependencies are available. This library requires that you have all of the following installed in your project:
vitestin version4.1or newerprismain version7.x@prisma/adapter-pgin version7.x
Step 3: Enable and configure the environment in your Vitest config
Configure the environment in your Vitest config:
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
environment: 'prisma-postgres',
environmentOptions: {
'prisma-postgres': {
// You must configure the path to your prisma client.
clientPath: "./generated/prisma-client"
}
},
setupFiles: [
// Registers hooks that start and roll back a database transaction around every test.
'vitest-environment-prisma-postgres/setup',
// This is where you mock your Prisma client to use the test environment's client.
'./vitest.setup.ts'
],
}
});
Step 4: Provide a DATABASE_URL
This environment will create the Prisma client and PostgreSQL adapter for your tests, so it has to know the connectionString to your test database.
You provide it by running your tests with a DATABASE_URL environment variable, which must point to a PostgreSQL instance for testing. It can point to:
- a real local PostgresSQL instance
- a docker-compose container
- a Testcontainers-created instance (see below)
- a cloud-hosted PostgresSQL instance, e.g, Supabase or Prisma Postgres
Step 5: Mock Prisma client
In your setupFile, vitest.setup.ts, mock your local Prisma client with the client provided by this environment:
import { vi } from 'vitest';
vi.mock('./generated/prisma-client', () => ({
default: prismaPostgresTestContext.client,
}));
This ensures that your application code uses the Prisma client created by the test environment. Combined with the vitest-environment-prisma-postgres/setup file, which starts and rolls back a transaction around every test, this means all Prisma queries from your code run inside an isolated transaction per test.
Please make sure that you're mocking exactly the module path that your code is using to import your Prisma client.
Step 6: Seed once per test run
Make sure to seed your test database at the beginning of every test run.
Use your own Prisma client and Vitest environment
Register the transaction hooks in a Vitest setup file to keep your current
environment (for example, node or jsdom) and use your application's Prisma
client factory. The factory can configure the PostgreSQL adapter and return a
client with Prisma extensions. Register this setup file through test.setupFiles
as the only registration of Prisma rollback hooks for the suite.
// vitest.setup.ts
import { vi } from 'vitest';
import { setupPrismaPostgres } from 'vitest-environment-prisma-postgres/hooks';
import { createPrismaClient } from './src/db/create-client';
const prisma = setupPrismaPostgres(createPrismaClient);
vi.mock('./src/db', () => ({ prisma }));
Here, ./src/db exports the shared client used by your application.
Keep the factory in a separate module that can be imported independently of that
shared instance. The factory should return a fresh client connected to your
test database. Each transaction covers beforeEach, the test body, and
afterEach. The hooks disconnect the client after the suite.
TypeScript configuration
If you are using TypeScript, make sure to add this environment to your compilerOptions.types array in your tsconfig.json:
{
"compilerOptions": {
"types": [
"node",
"vitest/globals",
"vitest-environment-prisma-postgres"
]
}
}
This is required because this environment provides a global type declaration:
declare global {
var prismaPostgresTestContext: PublicPrismaPostgresTestContext;
}
Without adding the package to compilerOptions.types, TypeScript will not include this global augmentation, and you will get type errors when mocking your Prisma client in vitest.setup.ts:
vi.mock('./generated/prisma-client', () => ({
default: globalThis.prismaPostgresTestContext.client,
// ^^^^^^^^^^^^^^^^^^^^^^^^^
// Error: Element implicitly has an 'any' type because
// type 'typeof globalThis' has no index signature.
}));
Adding "vitest-environment-prisma-postgres" to compilerOptions.types ensures that the global declaration is loaded and the mock is type-safe.
Known limitations
- Support for Vitest pools set to
vmThreadsorvmThreadsis not implemented - This environment assumes that tests inside a single worker run one at a time:
- Do not use
test.concurrentfor tests that touch the database. - Keep
maxConcurrencyat1for DB integration tests so a worker does not run multiple DB tests at once.
- Do not use
License
MIT
Ähnliche Projekte entdecken
Wird geladen…