# Write Vitest Component Tests for an Untested Feature

Yes. An agent finds the feature your Vitest suite never renders, writes tests for its pagination, empty and permission branches with MSW, and opens a PR.

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

## What the agent does

The agent reads what is tested and what is not before it writes anything. In a React app laid out like alan2207/bulletproof-react's `apps/react-vite`, twelve test files sit in `__tests__` folders: the auth forms, the dialog, drawer, form and notifications components, the head component, the discussions routes, one hook and the authorization helper. The comments feature has none of its own: `src/app/routes/app/discussions/__tests__/discussion.test.tsx` creates and deletes one comment through the page, and that is the only time `CommentsList` renders in a test. `src/features/comments/components/comments-list.tsx` has four branches that test never reaches: a spinner while loading, a `No Comments Found` list when there are none, a `Load More Comments` button when `hasNextPage` is set, and an `Authorization` wrapper that shows the delete button only when `POLICIES['comment:delete']` allows it, which is any `ADMIN` or the comment's own author with the `USER` role.

It reads the harness those tests would use. `src/testing/test-utils.tsx` exports `renderApp`, which logs a user in through the mock cookie, mounts the component inside `AppProvider` and a memory router, and waits for the loading markers to go; it has `createUser` and `createDiscussion` helpers that write to the MSW data model in `src/testing/mocks/db.ts` but no `createComment`, though `src/testing/data-generators.ts` already has one. `src/testing/mocks/handlers/comments.ts` pages comments ten at a time and computes `totalPages`, and `setup-tests.ts` starts the MSW server with `onUnhandledRequest: 'error'` and resets the database between tests. `vite.config.ts` runs Vitest in jsdom with globals and that setup file.

Then the tests, in `src/features/comments/components/__tests__/comments-list.test.tsx`. The agent adds a `createComment` helper to `test-utils.tsx` in the shape of `createDiscussion`, then writes one test per branch: an empty discussion renders the `comments` list with the not-found text; eleven comments render ten list items and the load-more button, and a click renders eleven and removes the button; a signed-in `USER` sees the delete button on their own comment and not on another user's, read through each list item's accessible name; and an `ADMIN` sees it on both. Each test seeds the mock database directly and renders `CommentsList` with the discussion id, so it exercises the real `useInfiniteComments` query, the real handler and the real policy with no module mocked.

The agent runs `yarn test` and checks the four tests fail when it reverts the relevant branch, then runs `yarn lint`, which includes the testing-library and vitest plugins the project configures, and `yarn check-types`. It reports the suite's count before and after.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Write Vitest component tests for the comments feature. Read apps/react-vite/vite.config.ts, apps/react-vite/src/testing/setup-tests.ts, apps/react-vite/src/testing/test-utils.tsx, apps/react-vite/src/testing/data-generators.ts, apps/react-vite/src/testing/mocks/db.ts, apps/react-vite/src/testing/mocks/handlers/comments.ts, apps/react-vite/src/lib/authorization.tsx, every file under apps/react-vite/src/features/comments, and apps/react-vite/src/app/routes/app/discussions/__tests__/discussion.test.tsx first. Add a createComment helper to test-utils.tsx like createDiscussion. In src/features/comments/components/__tests__/comments-list.test.tsx write tests for the empty state, for pagination with eleven comments and the Load More Comments button, and for the delete button being shown to a USER on their own comment only and to an ADMIN on every comment. Seed the mock database directly and do not mock any module. Check each test fails when its branch is broken, run yarn test, yarn lint and yarn check-types, and open a pull request with the output and the test count before and after.",
  "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 one test file and one helper. The description names each branch of `CommentsList` and the test that covers it, shows the suite output before and after with the counts, and shows the lint and type-check runs.

The run's record keeps the step where each test was made to fail on purpose, so a reviewer can see a test that would catch a regression and not one that passes whatever the component does. A reviewer checks that the pagination test's eleventh comment really crosses the handler's page size and that the permission test reads the delete button inside a specific list item rather than anywhere on the page.

## Run it on every repository with a feature no test renders

A team with several apps on this structure starts one run per repository with one `POST /o/{org}/sessions` each, this prompt as `input` and the repository in `resources`. Each run reads its own `__tests__` folders and features, so the untested feature differs per app and the prompt's comments example is only where this run started; a repository whose features are all rendered somewhere gets a description saying which branches still are not.

Send a `request_id` that is a UUID v5 of the repository's full name so a retried call returns the run it already started. The key's `webhook_url` reports when each run finishes, with `session_url` to read the pull request from.

## Why run it on Rush?

The tests that are missing are rarely the obvious ones; here the feature is rendered once in a route test and its pagination and permission branches never run. Finding that means reading every test file against every component, and writing the tests means reading the mock handler's page size and the policy function so the data crosses the right lines. The agent does that reading in its sandbox, seeds the same mock database the app's development mode uses, and keeps the run where each test is shown to fail.

The pull request is one test file and a helper, so review is reading four tests. A company with several front ends runs this on all of them at once, each in its own sandbox with its own pull request.

## Frequently asked questions

### Why render CommentsList alone and not the discussion page?

The page test already proves the page wires the component in. Rendering the list alone with a seeded database makes eleven comments and two users cheap to set up, and a failure points at the component rather than at the page. The route test stays as it is.

### Does this mock the query or the API client?

No. The tests go through the real `useInfiniteComments`, the real axios client and the MSW handler the development build also uses. The only thing arranged is the data in the mock database, so a change to the handler's page size would fail the pagination test.

### What about the loading spinner branch?

`renderApp` waits for loading markers to leave before returning, so the spinner is covered as the state the helper waits through rather than by a separate assertion. The agent says so in the description instead of adding a test that only checks a spinner exists.

### Can it do the same for another feature?

Yes. The prompt names comments because that is the gap in this repository; a run on another repository reads its own test folders and picks the feature with the most unrendered branches, and the description explains the choice.

## More Vitest jobs

- [Add Accessibility Tests to a Vitest Component Suite](https://getrush.ai/run/add-accessibility-tests/vitest/)
- [Add Coverage Thresholds to a Vitest Suite](https://getrush.ai/run/add-coverage-thresholds/vitest/)
- [Add Snapshot Tests to a Vitest Component Suite](https://getrush.ai/run/add-snapshot-tests/vitest/)

## The same job on other stacks

- [Write Cypress Component Tests for the Untested Forms in a React App](https://getrush.ai/run/write-component-tests/cypress/)
- [Write Component Tests for an Expo App](https://getrush.ai/run/write-component-tests/expo/)
- [Write Livewire Component Tests for a Laravel App](https://getrush.ai/run/write-component-tests/livewire/)

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