# Run Prisma Tests Against Their Own Database, Not the Dev One

Yes. An agent gives Vitest its own Postgres database, resets it in a global setup, clears tables between tests, guards the dev database and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-test-database-isolation/prisma/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads where the tests write today. In a Next.js and tRPC app laid out like trpc/examples-next-prisma-starter, `vitest.config.mts` sets `globals: true`, excludes `**/playwright/**` and lists `dotenv/config` in `setupFiles`, so every test loads the committed `.env`, whose `DATABASE_URL` and `DIRECT_URL` both point at `postgresql://postgres:@localhost:5432/next-prisma-starter-new`, the database `pnpm dx` seeds and `pnpm dev` serves. `src/server/routers/post.test.ts` builds a caller with `createContextInner({})` and `createCaller(ctx)`, calls `caller.post.add({ text: 'hello test', title: 'hello test' })` and reads it back with `post.byId`; nothing deletes the row. Every `pnpm test-unit` leaves one more `hello test` post in the developer's own data, and a test that counted posts could never be written.

It then gives the tests a database of their own. A new `.env.test` names `next-prisma-starter-test` in both URLs, and `vitest.config.mts` loads it before `.env`: `dotenv` never overrides a variable that is already set, so the test values win locally, while in CI, where the `unit` job sets no `DATABASE_URL` of its own, the same file applies. A `globalSetup` script runs `prisma migrate reset --force --skip-seed` against that URL once per run; Prisma documents the command as dropping the database or schema, creating it again and applying every migration, here the four under `prisma/migrations`, so the schema can never drift from the one the app runs. `src/server/env.ts` already accepts `NODE_ENV` as `test`, which Vitest sets, and `src/server/prisma.ts` reads `DATABASE_URL` through the generated client, so no application code changes.

Between tests the agent adds a setup file that runs `prisma.post.deleteMany()` in `beforeEach`, the one table `prisma/schema.prisma` declares, and disconnects in `afterAll`. The same file refuses to start unless `DATABASE_URL` ends in `-test`: a mistyped environment then fails the run with that message rather than emptying `next-prisma-starter-new`. With a clean table guaranteed, the agent adds the test that was impossible before: `caller.post.list({})` after one `post.add` returns exactly one item and `nextCursor` as `undefined`, which passes only when the table started empty and so proves the isolation on every run.

In its sandbox the agent installs Postgres, runs `pnpm install` and `pnpm prebuild` for the dev database, then `pnpm test-unit` twice, and reads `select count(*) from "Post"` on `next-prisma-starter-new` before and after to show the number did not move. It also reads `.github/workflows/main.yml`: the `unit` job starts a `postgres` service, sets `NODE_ENV: test`, runs `pnpm prebuild` and then `pnpm test-unit`, so the test database is created by the global setup on the same service and the job needs no new step. The job's service passes `POSTGRES_DATABASE: trpcdb`, a name the official image does not read, so the agent notes it in the description and leaves it to the reviewer.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Isolate the Vitest suite from the development database. Read vitest.config.mts, .env, src/server/env.ts, src/server/prisma.ts, src/server/routers/post.test.ts, prisma/schema.prisma and .github/workflows/main.yml first. Add a .env.test that names a separate Postgres database and load it before .env in the Vitest config. Add a globalSetup that runs prisma migrate reset --force --skip-seed against it, and a setupFile that empties the Post table before each test, disconnects after the suite and refuses to run unless DATABASE_URL ends in -test. Add a test that post.list returns exactly one item after one post.add. Run pnpm test-unit twice against a local Postgres, show the development database's post count before and after, quote the results and open a pull request.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request that adds `.env.test`, a global setup script and a test setup file, changes `vitest.config.mts` to load them, and extends `src/server/routers/post.test.ts` with the `list` test. No application file changes, and the `.env` developers use stays exactly as it was.

The description quotes two consecutive `pnpm test-unit` runs with the new test passing in both, the `prisma migrate reset` output naming the four migrations it applied, and the dev database's row count before and after, unchanged. The repository's `Testing` workflow runs its `unit` job on the branch, which is where the global setup first runs against a service container, and the check sits beside the agent's own run.

A reviewer decides whether the CI job should also set an explicit `DATABASE_URL` for the test database instead of relying on `.env.test`, and whether to drop the `POSTGRES_DATABASE` line from the service definition. Both are small edits to `main.yml` that the agent leaves out of the change.

## Run it on every GitHub issue labelled testing

In the Project that holds the repository, create an automation with a general form of this page's prompt and a `github` trigger on `issues.labeled` with `testing` in its `labels`. The trigger puts the issue after the prompt and fills in nothing, so the prompt says to read the issue that follows, make the test setup change it describes, prove it by running the suite twice, and open a pull request that links the issue. It fires only when the label is added by someone who can write to the repository.

An engineer who finds stray `hello test` rows in their local data files the issue with the row count; the run reads it, isolates the suite and opens the pull request with the counts in it. The same automation handles the next request, such as a database per Vitest worker, as its own run and branch.

## Why run it on Rush?

Showing that a test suite no longer touches the dev database means having both databases, running the suite more than once and counting rows in between, which is a Postgres install and a sequence of commands no one wants to type on a laptop twice. The sandbox does it from a clean start and keeps every command's output, so the pull request opens with the counts in it.

The run's record holds the migrate output, both test runs and the row counts, so a reviewer reads what happened instead of a claim that it works. Across a team's Prisma services the same request runs at once, one sandbox each, and every pull request shows that service's own tables and migrations.

## Frequently asked questions

### Why reset with migrations instead of prisma db push?

`prisma migrate reset` applies the files under `prisma/migrations`, so the test schema is the one production gets, including the index that one migration adds and the next drops. `db push` builds the schema from `schema.prisma` directly and could pass a test that a real migration would fail.

### Does this work in the CI job?

Yes. The `unit` job already starts a Postgres service and runs `pnpm test-unit`; the global setup creates and migrates the test database on that service. The job sets no `DATABASE_URL`, so `.env.test` applies there as it does locally.

### What if a test needs rows that persist across tests?

The `beforeEach` empties the `Post` table, so each test seeds what it needs. A test that wants shared rows creates them in a `beforeAll` and must expect the first `beforeEach` to remove them; the agent notes this in the setup file's header.

### Can the tests run in parallel workers?

Not against one database: two workers would empty each other's rows. The change keeps Vitest's default file-level parallelism for the single test file that exists; a database per worker, named with `VITEST_POOL_ID`, is the follow-up when more files appear.

## More Prisma jobs

- [Add Transactions to a Prisma App](https://getrush.ai/run/add-transactions/prisma/)
- [Fix updatedAt Columns That Never Update in a Prisma Schema](https://getrush.ai/run/add-updated-at-timestamps/prisma/)
- [Find Slow Postgres Queries in a Prisma App](https://getrush.ai/run/investigate-slow-queries/prisma/)

## More jobs like this: Write the missing tests

- [Add Test Retries for Flaky Jest Tests in CI](https://getrush.ai/run/add-test-retries/jest/)
- [Add Test Retries to a Playwright Suite](https://getrush.ai/run/add-test-retries/playwright/)
- [Set Test, Expect and Global Timeouts in a Playwright Suite](https://getrush.ai/run/add-test-timeouts/playwright/)

## Keep reading

- [What an agent can do in Prisma itself, once you connect it](https://getrush.ai/integrations/prisma/)
- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
