All jobs

Add Vitest Coverage to CI for a tRPC and Prisma App

Yes. An agent adds V8 coverage to your tRPC app's Vitest config, writes the router tests it shows missing, sets thresholds from a real run and reports from CI.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent reads the test setup before it measures anything. In a project like trpc/examples-next-prisma-starter, package.json runs vitest as test-unit, Playwright as test-e2e, prisma generate and prisma migrate deploy as prebuild, and uses pnpm with enable-pre-post-scripts in .npmrc; no lockfile is committed, which the workflow's own comment about enabling the cache once there is one confirms. vitest.config.mts sets globals, excludes playwright/, aliases ~/ to src/ and loads dotenv/config, and has no coverage block. There is one unit test, src/server/routers/post.test.ts, which creates a caller with createCallerFactory and adds and reads a post against the real database from DATABASE_URL. .github/workflows/main.yml has two jobs with a postgres service each: unit runs pnpm prebuild, pnpm test-unit and pnpm tsc on Node 22, and e2e builds, runs Playwright in Chromium and uploads results with actions/upload-artifact@v2.

The code worth covering is small and server-side. src/server/routers/post.ts has list with cursor pagination and a limit between 1 and 100, byId that throws a TRPCError with code NOT_FOUND, and add with a zod schema that caps title at 32 characters; _app.ts adds a healthcheck query; src/server/env.ts parses DATABASE_URL and NODE_ENV with zod and throws on failure; src/utils/transformer.ts re-exports superjson. The agent adds @vitest/coverage-v8 at the major that matches the installed vitest, and a coverage block with provider: 'v8', include of src/server/** and src/utils/** so a file no test imports still counts, exclude of the test files and src/pages/**, and reporter set to text, lcov and json-summary.

It needs a database to run the existing test, so it installs PostgreSQL in its sandbox, creates the trpcdb database with the postgres user the workflow uses, sets DATABASE_URL and runs pnpm prebuild to generate the client and apply the four migrations in prisma/migrations/. The first vitest run --coverage shows what is missed: nothing exercises list across a cursor boundary, the NOT_FOUND branch of byId, the rejected 33-character title in add, or the healthcheck query, and env.ts has only its happy path. The agent writes those tests in post.test.ts and a new env.test.ts, using the same createContextInner and createCaller pattern, then sets coverage.thresholds for lines, functions, branches and statements to whole numbers just under what it measured with the new tests in.

In the workflow it changes the unit job to run pnpm vitest run --coverage, uploads the coverage/ directory as an artifact, and writes the totals from coverage/coverage-summary.json into the job summary so the number is visible without downloading anything; it moves both jobs' upload-artifact to a current major, since the v2 the e2e job uses has been retired by GitHub. It runs pnpm lint, pnpm tsc and the coverage run in the sandbox and keeps the output.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Add Vitest coverage to CI for this tRPC and Prisma app and write the tests the report shows missing. Read package.json, .npmrc, vitest.config.mts, playwright.config.ts, .github/workflows/main.yml, .env, prisma/schema.prisma, the migrations directory, src/server/env.ts, src/server/context.ts, src/server/trpc.ts, every file under src/server/routers/ and src/utils/transformer.ts first, and check whether a lockfile exists. Add the V8 coverage provider matching the installed vitest major, with include set to the server and utils directories, the test files and pages excluded, and text, lcov and json-summary reporters. Install PostgreSQL in your sandbox, create the database the workflow uses, set DATABASE_URL, run pnpm prebuild, run the suite with coverage, and add tests for the uncovered paths in the post router, the healthcheck query and env parsing using the existing caller pattern. Set thresholds just under the measured numbers. Change the unit job to run with coverage, upload the report, write the totals to the job summary, and move upload-artifact to a current major. Run lint, tsc and the coverage run, and open a pull request with the summary before and after your tests.",
  "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

The pull request changes vitest.config.mts, package.json and .github/workflows/main.yml, extends src/server/routers/post.test.ts and adds src/server/env.test.ts. Its description shows the text coverage table from the first run and from the final run, the thresholds it set and the gap under each measured number, and the output of lint and typecheck.

Once merged, the unit job fails when coverage drops below the floor, with Vitest naming the metric, and every run leaves an lcov artifact and a summary line with the totals. The e2e job is unchanged except for the artifact action, so Playwright still runs against the built app.

Before merging, a reviewer checks that the new router tests describe intended behaviour, especially the NOT_FOUND error shape and the limit bounds, decides whether src/pages/** should stay excluded or get component tests later, and may commit a pnpm-lock.yaml so the workflow can turn on the pnpm cache it already mentions.

Run it on every nightly

In the console, a scheduled automation in the Project that has the repository starts one run each night with this prompt, on a schedule trigger with every set to 1d in the team's timezone. Each run reads the current coverage-summary.json, writes tests for the least covered router procedure, and raises the thresholds only when its tests pass, so the floor climbs with the suite rather than ahead of it. An automation run has its own max_runs_per_hour cap.

A team with several tRPC services can instead send this body once per repository to POST /o/{org}/sessions from a script, each with its own repository in resources and a request_id that is a UUID v5 of the repository name, so a retry returns the run that already started; the key's webhook_url then receives one run.completed per repository.

The use case this job follows, with its automation and trigger: Write the missing tests.

Why run it on Rush?

Coverage for a tRPC backend is only meaningful when the tests hit the procedures through the caller against a real Prisma client, which is how this project already tests. The agent sets that up in its own sandbox, PostgreSQL and migrations included, reads the real uncovered lines, and writes tests that follow the file's existing pattern instead of mocking the database to make a number move.

The thresholds come from a measured run, and both runs, the migrations and the lint and typecheck output are kept with the run and pasted into the pull request. Several services can be handled at once, each with its own database in its own sandbox.

Frequently asked questions

Why the V8 provider and not Istanbul?

V8 needs no transform step and matches how Vitest already runs this project's TypeScript, so the numbers come without instrumenting the build. Istanbul would work too and the pull request says what to change if the team prefers its branch counting.

Do the tests need a database in CI?

Yes, as the existing test already does. The unit job has a postgres service and runs pnpm prebuild before the tests, so the new tests use the same database the current one uses; the agent adds nothing to the service definition.

Will the thresholds fail my next pull request?

Only when it lowers coverage below the floor, which sits just under the number measured with the new tests in place. Adding a procedure with a test passes; adding one without, or deleting a test, fails with the metric named.

Why are the pages excluded from coverage?

The src/pages/ components are exercised by Playwright, not Vitest, and counting them in a unit coverage number would make the floor meaningless. The pull request keeps them out and notes that component tests would be the way to bring them in.

More tRPC jobs

The same job on other stacks

Keep reading