# Vitest

[Variance Authority](https://variance-authority.dev/docs/index.md) records which lines of your code each Vitest
test ran, and puts that record to work on your Vitest 4.1 suite. You wrap
`vitest.config.ts` once. From then on CI runs only the test files your change
reached, you can read what one test did and ask which tests cover a line, and
visual review runs on your machine and in CI with no hosted service.

## Try it

```bash
npm install --save-dev @variance-authority/sense @variance-authority/cli
```

```ts
// vitest.config.ts
import { defineConfig } from 'vitest/config';
import { withTestSelection } from '@variance-authority/sense/vitest';

export default withTestSelection(defineConfig({ test: { include: ['src/**/*.test.ts'] } }));
```

Run `npx vitest run` once, as you always do, and that run writes the record.
Your plugins, setup files, reporters and classic coverage setup stay as they
are. Change a source file and run again with one variable set:

```bash
$ VARIANCE_AUTHORITY_SINCE= npx vitest run
variance-authority: selected 12 of 340
```

Keep `isolate: true`, the Vitest default, so the record sees every module each
test file used.

## What you get

### Leave out the test files a change did not reach

The wrap removes the files your change did not reach before Vitest starts any
of them, and a run that keeps none passes. `VARIANCE_AUTHORITY_AT_DISTANCE=0-1`
keeps only the tests within one import of the change, for the loop after each
edit. With `projects`, wrap each project and the root.
[Let the runner skip them](https://variance-authority.dev/reference/packages/sense/index.md#let-the-runner-skip-them)
lists every variable and every line the run prints.

### Skip the cases a change did not reach

Add `VARIANCE_AUTHORITY_GRAIN=case`, and a kept file also skips the cases that
ran none of the changed code. They show as skipped, the way `it.skip` shows
them. [Skip the cases your edit did not
reach](https://variance-authority.dev/docs/test-an-edit/index.md#skip-the-cases-your-edit-did-not-reach) says when a file
still runs whole.

### Place shards by recorded time

`npx vitest run --shard 3/4` places whole test files by the time each took on
the last recorded run, longest first, so the shards finish close together.
[Split a test suite across CI jobs](https://variance-authority.dev/docs/sharding/index.md) gets those times to a fresh CI
checkout.

### Read one test's story

Run `npx vitest run` on one test with `VARIANCE_AUTHORITY_STORY=1` set, then
`variance story`.
The story lists the calls of your code the test made, in order, with the branch
each one took. In browser mode, each statement of the test opens what the page
did next. [Test stories](https://variance-authority.dev/docs/test-stories/index.md) shows how to read one and compare two.

### Ask which tests cover a line

`variance covering --file src/cart.ts --line 40` names the Vitest tests that
ran line 40, from the same record.
[Covering](https://variance-authority.dev/reference/packages/cli/index.md#covering-which-tests-covered-this-line)
lists every way to ask.

### Visual review

A test in `jsdom` writes a capture of what it mounted, and a later `variance`
run paints it in Chromium: [add visual review to the unit tests you already
have](https://variance-authority.dev/start/unit/index.md). A browser-mode test compares what it mounted inside the
Vitest run, and `-u` approves a change: [add one baseline comparison to a Vitest
browser-mode test](https://variance-authority.dev/start/vitest-browser/index.md). Neither needs the wrap.

## Not yet

- Recording each case in browser mode: a file in a page is recorded whole, and
  runs whole under case grain.
- Probes placed after your plugins in browser mode: a compiler that reads
  syntax reads the probes there.
- Selection in watch mode.

## Go further

- [Options on the Vitest seam](https://variance-authority.dev/reference/packages/sense/index.md#options-on-the-vitest-seam): every option of the wrap.
- [A record needs a module graph per test file](https://variance-authority.dev/docs/selecting/index.md#a-record-needs-a-module-graph-per-test-file), for `isolate: false`.
- [What a skipped test promises](https://variance-authority.dev/docs/selection-safety/index.md): the rule behind each Vitest skip.
- [Supported frameworks](https://variance-authority.dev/docs/supported-frameworks/index.md) puts Vitest beside every other runner.
