# Write Vitest Tests for a tRPC Router

Yes. An agent writes Vitest tests for your tRPC routers through createCaller: cursor pagination, NOT_FOUND, zod limits and the health check, on a real Postgres.

[Run this on Rush](https://cloud.getrush.ai/run/write-vitest-tests/trpc/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads the one test that exists before it writes more. In an app like trpc/examples-next-prisma-starter, `package.json` runs `vitest` as `test-unit`, Playwright as `test-e2e`, and `prisma generate` plus `prisma migrate deploy` as `prebuild`; dependencies are the `canary` tRPC packages, `@prisma/client ^6.7.0`, `zod ^4.2.1`, `superjson` and Next.js. `vitest.config.mts` sets `globals: true`, excludes `playwright/`, aliases `~/` to `src/` and loads `dotenv/config`, and `tsconfig.json` adds `vitest/globals` to `types`. `src/server/routers/post.test.ts` holds a single test: it builds a context with `createContextInner({})`, gets a caller from `createCaller`, calls `post.add` then `post.byId`, and asserts `toMatchObject` on the input. `.github/workflows/main.yml` runs that in a `unit` job with a `postgres` service, `NODE_ENV: test`, `pnpm prebuild` and then `pnpm test-unit` and `pnpm tsc`.

What is untested is most of `src/server/routers/post.ts`. `list` takes `limit` between 1 and 100 defaulting to 50 and a nullable `cursor`, fetches `limit + 1` rows ordered by `createdAt` descending with a cursor on `id`, pops the extra row to produce `nextCursor`, and returns the page reversed; `byId` throws a `TRPCError` with code `NOT_FOUND` and the message `No post with id '…'`; `add` validates `title` between 1 and 32 characters, non-empty `text` and an optional `id` that must be a UUID. The root router in `_app.ts` also has `healthcheck`, which returns the string `yay!`. None of the branches, the pagination arithmetic, the `NOT_FOUND` path or the zod rejections has a test, and the seed in `prisma/seed.ts` upserts one post with a fixed id that any ordering test has to account for.

The agent writes `src/server/routers/post.list.test.ts` and `post.validation.test.ts` next to the existing file, in the same style: `createContextInner`, `createCaller`, `inferProcedureInput` for typed inputs. For pagination it inserts posts with `createdAt` values a second apart through `prisma.post.createMany`, asks for `limit: 2`, asserts the two newest items come back in ascending order with a `nextCursor` equal to the id of the next-older row, follows the cursor and asserts the last page has `nextCursor` set to `undefined`, and checks that `limit: 0` and `limit: 101` reject with a `BAD_REQUEST` zod error. For `byId` it asserts the `NOT_FOUND` code and the message with the missing id. For `add` it asserts a 33-character title, an empty `text` and a non-UUID `id` each reject with `BAD_REQUEST`, and that a valid UUID `id` is stored as given. A `beforeEach` deletes from `post` with `prisma.post.deleteMany()` so the seeded row and earlier tests do not leak into ordering assertions, and a `healthcheck` test asserts `yay!`.

The tests need the database the existing test already assumes. The agent installs PostgreSQL in its sandbox with apt, starts it, creates the database `DATABASE_URL` in `.env` names, runs `pnpm install`, `pnpm prebuild` and `pnpm test-unit`, then `pnpm tsc` and `pnpm lint`, because `eslint.config.mjs` enables type-checked rules that a careless test file trips. It keeps the output and mentions in the pull request that the `unit` job in `main.yml` already provides this database, so nothing in CI changes.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Write Vitest tests for the tRPC routers in this repository. Read package.json, vitest.config.mts, tsconfig.json, .env, .github/workflows/main.yml, prisma/schema.prisma, prisma/seed.ts, src/server/trpc.ts, src/server/context.ts, src/server/routers/_app.ts, src/server/routers/post.ts and src/server/routers/post.test.ts first. Install and start PostgreSQL in the sandbox, create the database that DATABASE_URL names, and run pnpm install and pnpm prebuild. Add tests beside post.test.ts in the same style, using createContextInner, createCaller and inferProcedureInput, with a beforeEach that clears the post table: list with limit 2 over posts created a second apart, asserting ascending order within a page, nextCursor pointing at the next row, and no nextCursor on the last page; limit 0 and 101 rejected as BAD_REQUEST; byId with a missing id rejected as NOT_FOUND with its message; add rejecting a 33-character title, empty text and a non-UUID id, and storing a given UUID id; and healthcheck returning the literal string _app.ts declares. Run pnpm test-unit, pnpm tsc and pnpm lint, fix what they report without changing router code, and open a pull request that includes the test output and notes that the unit job in main.yml already has the Postgres service.",
  "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 `src/server/routers/post.list.test.ts` and `src/server/routers/post.validation.test.ts`, and nothing else: no router, schema or workflow changes. The existing `post.test.ts` stays as it is.

The description lists each case and the branch of `post.ts` it covers, and quotes the `vitest` summary, the `tsc` result and the `eslint` result from the sandbox. The transcript at `GET /o/{org}/sessions/{id}/messages` keeps the PostgreSQL setup and every command.

Before merging, a reviewer checks that `deleteMany` in `beforeEach` is acceptable against the shared test database the CI service provides, decides whether the Playwright smoke test should keep covering `add` through the UI, and may ask for a test of the `createdAt` transformer path through `superjson` as a follow-up in the same session.

## Run it on every Sunday night

Create an automation in the Project with a `schedule` trigger, a `cron` such as `0 2 * * 0` with your `timezone`, and a prompt that asks for tests on the router procedures changed that week that have no test file beside them. Each run opens one pull request on its own branch, so Monday starts with cases to review.

For one repository now, send this body to `POST /o/{org}/sessions` with a `request_id` that is a UUID v5 of the repository's full name, so a retry returns the run it already started. The key's `webhook_url` reports `run.completed` with `session_url` when the turn finishes.

## Why run it on Rush?

Testing a tRPC router honestly means a Postgres with the migrations applied, a Prisma client generated for it, and attention to a seed row that changes the ordering of every pagination assertion; on a laptop that is a database left in whatever state the last run abandoned. The agent builds that environment from scratch in its own sandbox, runs the suite there and keeps the output, so the pull request arrives with the cases proven rather than described.

Several services can be covered at once, each in its own sandbox, and the reviewer reads the `vitest`, `tsc` and `eslint` results in the pull request instead of reproducing them.

## Frequently asked questions

### Why go through createCaller instead of HTTP?

It is how the existing `post.test.ts` works: `createCaller` from `_app.ts` invokes procedures with a context built by `createContextInner`, so input validation, the router and Prisma all run while Next.js and the HTTP adapter stay out of the test. The Playwright smoke test in `playwright/` covers the HTTP path.

### Does this need Docker for Postgres?

No. The sandbox has no Docker, so the agent installs PostgreSQL with apt and starts it directly, then runs `pnpm prebuild` to apply `prisma/migrations`. In CI the `postgres` service in `main.yml` already does this job.

### Will the tests fight with the seeded post?

The `beforeEach` clears the `Post` table, so ordering and cursor assertions see only the rows a test created. The seed's fixed UUID is left for the development database.

### How are zod rejections asserted?

tRPC wraps an input failure in a `TRPCError` with code `BAD_REQUEST`, so each test uses `rejects.toMatchObject({ code: 'BAD_REQUEST' })`; the `NOT_FOUND` test also matches the message `post.ts` builds with the id.

## More tRPC jobs

- [Add API Error Handling to a tRPC App](https://getrush.ai/run/add-api-error-handling/trpc/)
- [Add CORS to a tRPC API](https://getrush.ai/run/add-cors/trpc/)
- [Add Vitest Coverage to CI for a tRPC and Prisma App](https://getrush.ai/run/add-coverage-to-ci/trpc/)

## The same job on other stacks

- [Write Vitest Tests for a Turborepo Monorepo](https://getrush.ai/run/write-vitest-tests/turborepo/)
- [Write Vitest Tests for a Vite React App](https://getrush.ai/run/write-vitest-tests/vite-react/)
- [Write Vitest Tests for a Vue App](https://getrush.ai/run/write-vitest-tests/vue/)

## Keep reading

- [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/)
