# Rstest

What [Variance Authority](https://variance-authority.dev/docs/index.md) does for your Rstest suite starts from one
record: the lines of your code each Rstest test ran. You wrap
`rstest.config.ts` once. Then 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
// rstest.config.ts
import { defineConfig } from '@rstest/core';
import { withTestSelection } from '@variance-authority/sense/rstest';

export default defineConfig(withTestSelection(
  { globals: true, setupFiles: ['./test/setup.ts'] },
  { preconditions: ['rstest.config.ts'] },
));
```

Rstest does not say which config file it loaded, so the second argument names
it: a change there runs every test. Run `npx rstest run` once to write the
record, change a source file, and run again:

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

Keep `isolate: true`, the Rstest 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 adds the files your change did not reach to the project's `exclude`
before Rstest plans its run, so they never start, and your setup files,
reporters and classic coverage setup stay as they are. Under `projects`, wrap
each project and the root, and each project prints its own line.
[Cut an Rstest run down to a diff](https://variance-authority.dev/reference/packages/sense/index.md#cut-an-rstest-run-down-to-a-diff)
lists every option of the wrap.

### Read one test's story

Set `VARIANCE_AUTHORITY_STORY=1` on `npx rstest run` for one test, then run
`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 Rstest tests that
ran line 40. [Covering](https://variance-authority.dev/reference/packages/cli/index.md#covering-which-tests-covered-this-line)
lists every way to ask.

### Visual review

In `jsdom` or `node`, a test writes a capture of what it mounted, and a later
`variance` run paints it in Chromium. Under `@rstest/playwright`, the test
observes the page it already drives, and `-u` approves a change. [Add visual
review to an Rstest suite](https://variance-authority.dev/start/rstest/index.md) sets up both and says which to
choose. Neither needs the wrap.

## Not yet

- Skipping cases inside a kept file: Rstest runs it whole, and
  `VARIANCE_AUTHORITY_GRAIN=case` fails the run.
- Shard placement by recorded time: Rstest's `--shard` divides by count.
- Selection in watch mode.

## Go further

- [Run the tests an edit needs](https://variance-authority.dev/docs/test-an-edit/index.md), with `VARIANCE_AUTHORITY_AT_DISTANCE=0-1` for each edit.
- [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 an Rstest run with `isolate: false`.
- [What a skipped test promises](https://variance-authority.dev/docs/selection-safety/index.md): the rule behind each Rstest skip.
- [Supported frameworks](https://variance-authority.dev/docs/supported-frameworks/index.md) puts Rstest beside every other runner.
