# Add Coverage Thresholds to a Vitest Suite

Yes. An agent adds the V8 coverage provider to your Vitest config, sets thresholds at the measured level, makes CI fail under them, and opens a PR.

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

## What the agent does

The agent starts from what the project already measures. In a React app laid out like alan2207/bulletproof-react, `apps/react-vite/vite.config.ts` carries a `test` block with `globals`, a `jsdom` environment, `setupFiles: './src/testing/setup-tests.ts'`, an `exclude` that keeps `**/e2e/**` out of Vitest, and a `coverage` block that names only `include: ['src/**']`: no provider, no reporter, no thresholds. `package.json` pins `vitest` at `^2.1.4` and has a `test` script of plain `vitest`, with no `@vitest/coverage-v8` or `@vitest/coverage-istanbul` among the devDependencies, so `vitest --coverage` would stop and ask for one. `.github/workflows/react-vite-ci.yml` runs `yarn test` in its `all-cli-checks` job after `yarn build`, and `.gitignore` already lists `/coverage`.

It then installs `@vitest/coverage-v8` on the same major line as `vitest`, so the two agree on the Vitest version they were built for, and adds a `test:coverage` script that runs `vitest run --coverage`. In `vite.config.ts` the `coverage` block gains `provider: 'v8'`, reporters of `text`, `lcov` and `json-summary`, and an `exclude` for what is not product code: the eleven `*.stories.tsx` files, `src/testing/**` (the MSW handlers, the mock database and `test-utils.tsx`), `src/main.tsx` and `**/*.d.ts`. Without that exclusion the mock server would count as uncovered application code and the numbers would describe the wrong thing.

The thresholds come from a measurement, not a guess. The agent runs the coverage report once over the twelve existing test files, which cover the dialog, drawer, form and notification components, the login and register forms, the authorization helper, and the discussions route, and sets `thresholds.lines`, `functions`, `branches` and `statements` just under the figures that run prints, rounded down to whole percentages. That makes the pull request green on the day it lands and turns any later drop into a failure, which is the point. It leaves `thresholds.autoUpdate` off and explains in the description how to raise the numbers as tests are added.

In CI, `yarn test` in the `all-cli-checks` job becomes `yarn test:coverage`, and an `actions/upload-artifact@v4` step keeps the `coverage/` directory with `if: always()`, the same way the `e2e` job already keeps `playwright-report/`. The agent runs `yarn lint` and `yarn check-types` too, since `lint-staged` runs both on every commit and the config file is TypeScript, and it quotes the coverage table from its own run in the pull request.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add coverage thresholds to the Vitest suite in apps/react-vite. Read package.json, vite.config.ts, src/testing/setup-tests.ts, .gitignore and .github/workflows/react-vite-ci.yml first. Install @vitest/coverage-v8 on the same major version as vitest, add a test:coverage script that runs vitest run --coverage, and in vite.config.ts set coverage.provider to v8, reporters text, lcov and json-summary, and exclude stories, src/testing, src/main.tsx and d.ts files from the report. Run the report once and set lines, functions, branches and statements thresholds just under the measured numbers, rounded down to whole percentages. In the CI workflow run yarn test:coverage instead of yarn test and upload the coverage directory as an artifact with if: always(). Run yarn lint and yarn check-types, and open a pull request that quotes the coverage 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

What comes back is a pull request touching four files: `package.json` and the lockfile for the provider and the script, `vite.config.ts` for the coverage block, and `.github/workflows/react-vite-ci.yml` for the step and the artifact. The description quotes the text reporter's table, names the four thresholds and the measured figure each sits under, and lists what was excluded and why.

The run's record keeps the full `vitest run --coverage` output, so a reviewer can see the per-file lines rather than a summary of them, and check that the exclusions removed the mocks and not a component.

Before merging, a reviewer decides whether the thresholds should sit at the measured level or at a target the team means to reach, and whether `src/app/routes/**` should be excluded as well, since route files are mostly composition. Either is a one-line change to the config.

## Run it on every repository with a Vitest suite and no coverage floor

A team with several Vite apps starts one run per repository with one `POST /o/{org}/sessions` each, this prompt as `input` and the repository in `resources`. Each run measures its own suite and sets thresholds from its own numbers, so an app with thin tests gets a low floor it can clear and one with good tests keeps the bar where it is.

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 the key starts finishes, with `session_url` to read the pull request from and the run's final answer in `result`.

## Why run it on Rush?

A coverage floor set from memory is either too high, and blocks the first pull request after it, or too low, and catches nothing. The number has to come from running the suite, and the exclusions have to come from reading what is in `src/`. The agent does both in its own sandbox, with the same `yarn install` the workflow runs, and keeps the output.

The pull request carries the config, the workflow change and the measured table, so the review is a diff plus one number per threshold rather than a local checkout and a coverage run of your own. Several apps can be done at once, one sandbox each, each with its own pull request and its own figures.

## Frequently asked questions

### Why V8 rather than Istanbul?

V8 coverage comes from the engine and needs no instrumentation of the source, so it runs faster and does not change the code under test. Istanbul is more precise on branches in some TypeScript output. The provider is one string in the config, so switching later is cheap.

### Will the thresholds block the next pull request?

Only one that lowers coverage. They are set just under what the suite measures today, so the pull request that adds them passes, and a change that deletes tests or adds untested code without tests fails the `all-cli-checks` job with the threshold that was missed.

### What about the Playwright tests?

They stay out. The config's `exclude` already keeps `e2e/` away from Vitest, and the `e2e` job runs them separately, so the coverage figure describes the unit and integration tests only.

### Can the thresholds rise on their own?

Vitest has `thresholds.autoUpdate`, which rewrites the config whenever coverage goes up. The agent leaves it off, because a config file that changes during a test run surprises reviewers; raising the numbers is a deliberate edit in the pull request that adds the tests.

## More Vitest jobs

- [Add Snapshot Tests to a Vitest Component Suite](https://getrush.ai/run/add-snapshot-tests/vitest/)
- [Write Vitest Component Tests for an Untested Feature](https://getrush.ai/run/write-component-tests/vitest/)
- [Add Accessibility Tests to a Vitest Component Suite](https://getrush.ai/run/add-accessibility-tests/vitest/)

## The same job on other stacks

- [Add Coverage Thresholds to a Jest Suite](https://getrush.ai/run/add-coverage-thresholds/jest/)
- [Add Coverage Thresholds to a Mocha Suite With nyc](https://getrush.ai/run/add-coverage-thresholds/mocha/)
- [Add Coverage Thresholds to a pytest Suite](https://getrush.ai/run/add-coverage-thresholds/pytest/)

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