# Add Vitest Coverage to CI for a Vite React App

Yes. An agent installs the Vitest coverage provider your config expects, measures the suite, sets thresholds from the result, and has CI run and publish it, in a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-coverage-to-ci/vite-react/). Opens 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 touches it. In an app like the `apps/react-vite` application of alan2207/bulletproof-react, `package.json` runs tests with a bare `vitest` script on `vitest` 2.1 with `jsdom`, `@testing-library/react`, `@testing-library/jest-dom`, `msw` 2 and `@mswjs/data`, and installs with Yarn 1, which the README asks for alongside Node 20. `vite.config.ts` carries the `test` block: globals on, `jsdom`, `setupFiles` pointing at `src/testing/setup-tests.ts`, `e2e/` excluded, and a `coverage` entry that already says `include: ['src/**']` but names no provider. No `@vitest/coverage-v8` or `@vitest/coverage-istanbul` is installed, so `vitest run --coverage` would stop and ask for one. `.gitignore` already lists `/coverage`, which says someone meant to do this.

There are twelve test files, under `__tests__` directories beside what they test: two route tests for discussions, the `head`, `dialog`, `confirmation-dialog`, `drawer`, `form` and `notifications` component tests, `login-form` and `register-form`, the `use-disclosure` hook and `authorization`. `setup-tests.ts` starts an MSW server with `onUnhandledRequest: 'error'`, mocks `zustand`, stubs `ResizeObserver`, and seeds and resets an in-memory database around every test, so the suite runs without a network. `.github/workflows/react-vite-ci.yml` runs on pushes to every branch and pull requests to `master`, with a job that moves `.env.example` into place, runs `yarn install`, `yarn build`, `yarn test`, `yarn lint` and `yarn check-types` in `apps/react-vite`, and a separate `e2e` job that starts the mock server with pm2, runs Playwright and uploads `playwright-report/` as an artifact.

The agent adds `@vitest/coverage-v8` at the same major as the installed Vitest, adds a `test:coverage` script that runs `vitest run --coverage`, and extends the `coverage` block: `provider: 'v8'`, reporters `text`, `json-summary`, `lcov` and `html`, and an `exclude` list for what should not count, `src/testing/**` (the mocks and generators), `src/main.tsx`, `src/types/**` and the `__tests__` directories themselves. Then it runs the script in its sandbox, reads the summary table, and writes `thresholds` for lines, functions, branches and statements a few points under the measured values, so the gate fails on a drop rather than on day one. It does not invent the numbers: the measured figures and the thresholds go into the pull request side by side.

In `react-vite-ci.yml` the `yarn test` step becomes `yarn test:coverage`, so the thresholds are enforced on every push, an `actions/upload-artifact@v4` step like the one the `e2e` job already uses saves `coverage/` for the run, and a short step appends the per-metric totals from `coverage/coverage-summary.json` to `$GITHUB_STEP_SUMMARY`, so the number is visible on the run page without a third-party action. The `.husky` pre-commit and `lint-staged` configuration stay as they are; coverage belongs in CI, not in the commit hook.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add Vitest coverage reporting to this Vite React app and enforce it in CI. Read package.json, yarn.lock, vite.config.ts, tsconfig.json, src/testing/setup-tests.ts, every test file, .gitignore and .github/workflows/react-vite-ci.yml first. Add @vitest/coverage-v8 at the same major as the installed Vitest, add a test:coverage script that runs vitest run --coverage, and extend the existing coverage block with the v8 provider, the text, json-summary, lcov and html reporters, and an exclude list for the test utilities, mocks, entry point, type declarations and the __tests__ directories. Run the script in the sandbox, read the summary, and set lines, functions, branches and statements thresholds a few points under the measured values. In the workflow replace the test step with yarn test:coverage, upload the coverage directory as an artifact, and append the totals from coverage-summary.json to the job summary. Do not change the Husky hook or lint-staged. Open a pull request that shows the measured coverage next to each threshold and the per-file table.",
  "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 touches `apps/react-vite/package.json` and `yarn.lock` for the one new devDependency and script, `vite.config.ts` for the coverage block, and `.github/workflows/react-vite-ci.yml` for the test step, the artifact upload and the summary step. No test is added or changed, no source file moves, and the Next.js apps in the same repository are untouched.

The description shows the coverage summary table as the agent measured it in the sandbox, the four thresholds beside the measured figures, the exclude list with a line on why each entry is there, and the files with the lowest coverage, which is where the next tests should go. The `vitest run --coverage` output is in the session transcript at `GET /o/{org}/sessions/{id}/messages`, and the pull request's own CI run is the first enforcement of the thresholds, with the summary on its run page.

Before merging, a reviewer checks that the excluded paths are the ones they agree should not count, and that the headroom under each threshold matches how strict they want the gate. A follow-up message to the same session can write tests for the lowest-covered files the report names and move the thresholds up.

## Run it on every repository with tests but no coverage number

Run this once per repository: have a script send the same request with each repository's URL in `resources` and a fresh UUID as `request_id`, so a retried request returns the run it already started rather than a second pull request. Each run has its own sandbox and its own branch, so several front-end repositories get their pull requests at once. The Rush GitHub App must be installed on each; `GET /o/{org}/github` lists what it can reach.

Set a webhook URL on the key and Rush POSTs `run.completed` or `run.failed` when each run's turn finishes, with `session_url` your server reads to post the pull request link and the measured totals to the team's channel. A key may have at most `max_concurrent` runs in progress, 4 by default, so a longer list of repositories should be queued.

## Why run it on Rush?

Picking coverage thresholds means running the whole suite with a provider installed, reading the per-file table, deciding what to exclude and running it again, then watching a CI run to see the number hold. The agent does that loop in its own sandbox with the real suite, MSW server and all, and writes down what it measured rather than a round number.

The run keeps the coverage output and every command behind it in the session record, so the thresholds in the pull request are traceable to the run that measured them. Several repositories can run the same request at once, each in its own sandbox, each ending in one pull request with its own figures.

## Frequently asked questions

### Why v8 rather than istanbul?

Because the app already builds with Vite on a current Node, and the v8 provider needs no instrumentation step and runs as fast as the suite itself. Istanbul is a one-line change in the same block if the team prefers its branch accounting, and the pull request says so.

### Will the thresholds fail the first CI run?

They should not. They are set a few points under the figures the agent measured on the same commit, and the pull request's own CI run shows them passing. They fail when a later change lowers coverage past the headroom, which is the point.

### Why exclude src/testing and the __tests__ directories?

The mocks, the data generators and the MSW handlers under `src/testing/` exist only for the tests, and counting test files toward their own coverage inflates the total. Excluding them makes the number describe the application code. Each exclusion is listed in the pull request so the team can disagree with one.

### Does this change the Playwright job?

No. The `e2e` job keeps its mock server and its Playwright report artifact. Unit coverage comes from Vitest in the first job; combining it with end-to-end coverage would need instrumenting the built app and is left out of this pull request.

## More Vite + React jobs

- [Add Sentry Crash Reporting to a Vite React App](https://getrush.ai/run/add-crash-reporting/vite-react/)
- [Add a Dark Mode Toggle to a Vite React App from a Linear Issue](https://getrush.ai/run/add-dark-mode/vite-react/)
- [Add Dependabot to a Vite React Monorepo](https://getrush.ai/run/add-dependabot/vite-react/)

## The same job on other stacks

- [Add Code Coverage to CI for a VS Code Extension](https://getrush.ai/run/add-coverage-to-ci/vscode-extension/)
- [Add Test Coverage Reporting to Wagtail CI](https://getrush.ai/run/add-coverage-to-ci/wagtail/)
- [Add Test Coverage to CI for an aiohttp Project](https://getrush.ai/run/add-coverage-to-ci/aiohttp/)

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