npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@flefebvre/prisma-test-helper

v1.0.0

Published

Throwaway-Postgres test harness for Prisma + Vitest: one container per run, one database per worker. Postgres-only.

Readme

@flefebvre/prisma-test-helper

Fast, isolated Postgres for Prisma + Vitest tests: a real database per worker, a clean database for every test, zero cleanup code.

npm version CI License: MIT

Why

Testing against a real database usually forces a trade-off. Either you serialize your suite and truncate tables between tests — slow, and one forgotten cleanup poisons every test after it — or you swap the database for SQLite or a mock, and stop testing your actual migrations, constraints, and queries.

This harness removes the trade-off. Each test run gets one throwaway Postgres container; each Vitest worker gets its own database; tests commit for real, and after every test the harness empties that database again in one batched statement. Your tests look like this:

import { expect, test } from "vitest";

import { setupDatabase } from "@flefebvre/prisma-test-helper";
import { db } from "../src/db/client.js";

setupDatabase(db);

test("persists an author", async () => {
  await db.author.create({ data: { name: "Ada" } });
  expect(await db.author.count()).toBe(1);
});

test("starts from a clean database — the author above is gone", async () => {
  expect(await db.author.count()).toBe(0);
});
  • No cleanup code. Every test starts from an empty database with its sequences rewound — no truncation of your own, no ordering constraints, no leftover rows. Rows your migrations insert, such as lookup tables, are the exception: every test starts with them, restored after any test that changed them. So are the rows of the reference-data seed you can hand global setup: your project's own seed function, not prisma db seed.
  • Real commits. Nothing is wrapped or faked: $transaction behaves as in production, and a second client, a queue worker, or a deferred constraint sees exactly what it would there.
  • Real Postgres. Not SQLite, not a mock. Your migrations, your constraints, your triggers, your citext columns.
  • Parallel by default. One database per Vitest worker slot, so files running in parallel cannot see each other's writes.
  • Nothing to tear down. The container is removed when the run ends, including after a crash. In url mode the run drops its databases, and the next run drops those a killed run left.

Postgres-only. The harness starts a real Postgres in Docker, or uses one you point it at, and relies on CREATE DATABASE … TEMPLATE for cloning; no other database is supported.

How it works

One run of your test suite goes through this lifecycle:

  1. Vitest's global setup starts one tuned throwaway Postgres container (tmpfs data directory, fsync=off) through Testcontainers.
  2. Your project's own prisma migrate deploy runs once against a Template Database inside that container, followed by your own seed function if you configured one. The tables they leave rows in form the Baseline, which the harness snapshots there.
  3. Before any worker forks, one Worker Database per Vitest worker slot is cloned from the template (<databaseName>_1..N) with CREATE DATABASE … TEMPLATE — a fast file-level copy.
  4. Each worker points DATABASE_URL at its own clone. Tests commit to it for real.
  5. Before a file's first test, and after every test, the Reset empties that clone again: one DO block, sent through your own Prisma client, that empties every table and rewinds every sequence in a single statement — restoring, instead of emptying, any Baseline table the test wrote to.
  6. The container is removed when the run ends.

Already running Postgres? Pass a url and the same lifecycle runs on that server, under database names unique to the run, with no container.

Migrations run once, clones are cheap, and the Reset is one planned statement — so the per-test overhead is a few milliseconds (about 7 ms at 73 tables, 10 ms with 20 workers resetting at once), nearly flat in the size of your schema. Why DELETE and not TRUNCATE, with the benchmark behind it: ADR 0001.

Requirements

  • Node ^22.22, ^24 or >= 26, as Vitest 5 and Testcontainers require. The package ships ESM only, but your project need not be — the wiring runs under Vitest, which transforms modules through Vite, so a CommonJS project (a stock Next.js app, say) works too.
  • Docker (or another container runtime Testcontainers can reach), or an existing Postgres 13+ server you point the run at with the url option or PRISMA_TEST_HELPER_URL.
  • Peer dependencies: Vitest ^5.0.3 and Prisma ^7, with your migrations committed (prisma migrate deploy is what the harness runs).
  • A Postgres superuser for the Reset. The container's user is one, so there is nothing to do; in url mode, the URL's role must be one.
  • Testcontainers comes with this package; do not add it yourself. Under pnpm, none of its build scripts needs approving.

Details for each, including the pnpm allowBuilds entries, are in Before you start.

Installation

pnpm add -D @flefebvre/prisma-test-helper
# or: npm i -D / yarn add -D / bun add -d

Quick start

Three wiring files and a test. They assume a prisma.config.ts that reads DATABASE_URL, and a Prisma client module built from DATABASE_URL: Setup walks through all five files, step by step.

// tests/global-setup.ts
import { createGlobalSetup } from "@flefebvre/prisma-test-helper/global-setup";

export default createGlobalSetup();
// tests/setup.ts: no static imports of your app here
import { setupWorkerDatabase } from "@flefebvre/prisma-test-helper/setup";

setupWorkerDatabase();
// vitest.config.ts
import { defineConfig } from "vitest/config";

export default defineConfig({
  test: {
    globalSetup: ["tests/global-setup.ts"],
    setupFiles: ["tests/setup.ts"],
    pool: "forks",
    isolate: true,
  },
});
// tests/author.test.ts
import { expect, test } from "vitest";

import { setupDatabase } from "@flefebvre/prisma-test-helper";
import { db } from "../src/db/client.js";

setupDatabase(db);

test("persists an author", async () => {
  await db.author.create({ data: { name: "Ada" } });
  expect(await db.author.count()).toBe(1);
});

isolate: true is Vitest's default and the one these docs prescribe: every test file gets fresh modules. isolate: false is supported too. Each worker then keeps your app's modules and Prisma client across files, while every file still gets its own opt-in and a clean Worker Database. On a generated 73-table suite with a 300 ms app import, it cut the run by 46 % at 4 workers and 35 % at 16. The trade-offs: module state and vi.mock calls leak between files on a worker, Reset Hooks registered in a test file carry over, and schema changes outside migrations go unseen. See isolate: false.

Documentation

  • Setup: the five files, merging into existing configs, isolate: false, projects, path aliases, server-only, and a smoke test.
  • Writing tests: the setupDatabase(db) opt-in, where test data belongs, hooks and the Reset, locks, and Reset Hooks for fixtures.
  • API: every export, its options, and its entry point.
  • Troubleshooting: every error, what is mis-wired, and where the fix lives.
  • For AI coding agents: the rules, setup steps and a by-task guide.
  • Migrating: upgrade notes per major version. The package follows semver, so breaking changes land only in major versions.

These pages ship inside the package, so they always match the version you have installed: node_modules/@flefebvre/prisma-test-helper/docs/.

AI coding agents

Point your agent (Claude Code, Cursor, Codex, …) at docs/for-agents.md by adding one line to your project's AGENTS.md:

Tests that touch the database use @flefebvre/prisma-test-helper: read
node_modules/@flefebvre/prisma-test-helper/docs/for-agents.md first.

That page ships with the package, so the agent reads the guidance for the installed version. An agent asked to wire the harness from scratch adds this line itself as the last setup step.

Contributing

Issues and pull requests are welcome — open one here. The package is dogfooded: this repo's own test suite runs through the exact wiring Setup documents.

git clone https://github.com/flolefebvre/prisma-test-helper.git
cd prisma-test-helper
pnpm install
pnpm run generate             # generate the fixture Prisma client
pnpm test                     # every vitest project (all but unit need Docker)
pnpm run gate                 # typecheck, lint, duplication, tests, build
pnpm run test:url-concurrent  # not in the gate, but in CI: concurrent url-mode runs

License

MIT — see LICENSE.