# Add Missing Tests to Every Pull Request in a Next.js App

An agent finds the untested rules a Next.js pull request adds to its posts API routes, writes Vitest tests, runs them, and posts a review with a patch.

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

## What the agent does

The agent reads the diff against `app/api/posts/route.ts`, `app/api/posts/[postId]/route.ts`, `components/editor.tsx` and `lib/validations/post.ts`, and lists the rules it changes: the session check through `getServerSession(authOptions)`, the zod `postPatchSchema`, or the free-plan limit that answers `402` once a user has three posts.

shadcn-ui/taxonomy has no test runner, so the patch adds Vitest with pnpm, a `vitest.config.ts` that maps the `@/` alias, and a `test` script. The tests call the handlers with a `Request` and `{ params: { postId } }`, replace `lib/db.ts`, `lib/subscription.ts` and the session with `vi.mock`, and set test values for the variables `env.mjs` validates. Each checks one answer: `403` for a post the user cannot access, `422` for a title under 3 characters, `204` after the owner deletes.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Review this pull request for untested behavior. Check it out and read its diff against the base branch. List each new or changed behavior in app/api/posts, components/editor.tsx and lib/validations/post.ts that no existing test covers. If the repository has no test runner, add Vitest with pnpm, a vitest.config.ts that maps the @/ alias from tsconfig.json and a test script. Write Vitest tests next to the code they cover: call the route handlers with a Request and route params, replace the session, the Prisma client and the subscription lookup with vi.mock at the module boundary, and set test values for env.mjs. Run npx vitest run until the new tests pass, then pnpm lint. Do not change application code: if a test fails because the code is wrong, keep it out of the patch and describe the failure. Do not push commits, commit to the branch or open a pull request. Post one review on the pull request that lists each untested behavior with file and line, each test you wrote, any test setup you added, the npx vitest run and pnpm lint results, and the new tests as one patch. If this run names no pull request, in resources or after this prompt, stop and say so.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app/pull/123"
    }
  ]
}
```

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

One review on the pull request: each untested rule with its route file and line, the test that covers it, and the `npx vitest run` and `pnpm lint` results. The patch holds the new test files and, on a repository with no runner, `vitest.config.ts` and the `package.json` and `pnpm-lock.yaml` changes. No application file changes and nothing is pushed.

A reviewer decides whether to keep Vitest as a new dev dependency, and whether a test left out because a handler failed it, such as a `PATCH` on another user's post, is a real bug.

## Run it on every pull request opened against main

A `github` trigger on `pull_request.opened` and `pull_request.synchronize` with `branch_patterns` `main`, on an automation whose prompt is this request, runs it per pull request and again per push. Owners, members and collaborators fire it; a fork's pull request never does. The event gives the URL and base branch without SHAs, so the agent reads the head from the URL. Until a `test` script is merged, each run's patch carries the Vitest setup again.

## Why run it on Rush?

The posts handlers lean on NextAuth and Prisma, and `POST` also on the Stripe plan. A test that calls the real handler and the real zod schema, with only those replaced at the module boundary, is the cheapest proof that the `402`, `403` and `422` answers still hold, and the review names which this pull request touched.

## Frequently asked questions

### Why Vitest and not Jest?

Vitest reads TypeScript and the `@/` alias with one small config and no Babel setup, suiting a repository with no test runner yet.

### Why does the test setup come back in a patch?

Adding a test tool is the author's call, and this recipe never pushes. The review shows the patch passing, and `git apply` keeps the tests and the setup in one step.

## More Next.js jobs

- [Translate a Next.js App into Another Language](https://getrush.ai/run/translate-app-strings/nextjs/)
- [Upgrade Next.js 13 to Next.js 16](https://getrush.ai/run/upgrade-framework-major/nextjs/)
- [Plan an Upgrade to Next.js 16](https://getrush.ai/run/upgrade-impact-report/nextjs/)

## The same job on other stacks

- [Add Missing Tests to Every Pull Request in a Nuxt App](https://getrush.ai/run/test-every-pull-request/nuxt/)
- [Add Missing Tests to Every Pull Request in a Phoenix App](https://getrush.ai/run/test-every-pull-request/phoenix/)
- [Add Missing Tests to Every Pull Request in a Rails App](https://getrush.ai/run/test-every-pull-request/rails/)

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