# Jest

[Variance Authority](https://variance-authority.dev/docs/index.md) keeps a record of the lines each Jest test ran,
and your Jest 30 suite uses it from the next run on. You wrap
`jest.config.mjs` 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
```

```js
// jest.config.mjs
import { withTestSelection } from '@variance-authority/sense/jest';

export default withTestSelection({
  // the configuration you have today, unchanged
  testEnvironment: 'node',
});
```

Jest does not say which config file it loaded, so name it as a precondition:

```json
{ "suites": { "unit": { "kind": "unit", "before": ["jest.config.mjs"] } } }
```

Save that as `variance.config.json`, run `npx jest` once to write the record,
change a source file, and run again:

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

Under Node 24 or newer, run a recorded suite with
`NODE_OPTIONS=--no-async-context-frame`, so Jest frees each finished test file.

## What you get

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

The wrap leaves files out in Jest's `filter`, before `--shard`, so every shard
sees the same list, and your transforms, setup files, reporters and classic
coverage setup stay as they are. `VARIANCE_AUTHORITY_AT_DISTANCE=0-1` keeps only
the tests within one import of the change.
[Cut a Jest run down to a diff](https://variance-authority.dev/reference/packages/sense/index.md#cut-a-jest-run-down-to-a-diff)
lists every option of the wrap.

### Skip the cases a change did not reach

Add `VARIANCE_AUTHORITY_GRAIN=case`, and a `jest-circus` handler marks the
cases that ran none of the changed code as skipped before they run, as
`it.skip` would. [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 jest --shard 3/4` places whole test files by the time each took on the
last recorded run, longest first. [Split a test suite across CI
jobs](https://variance-authority.dev/docs/sharding/index.md) gets those times to CI, and keeps Jest's transforms between
runs.

### Read one test's story

Name one test to `npx jest -t` with `VARIANCE_AUTHORITY_STORY=1` set, then run
`variance story`.
The story lists the calls of your code the test made, in order, with the branch
each one took. [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 Jest 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

A test in `jsdom` writes a capture of what it mounted, and a later `variance`
run paints it in Chromium. The test stays without a browser and keeps its own
assertions. [Add visual review to the unit tests you already
have](https://variance-authority.dev/start/unit/index.md) sets it up, with no wrap needed.

## Not yet

- A browser inside the test: Jest has no browser mode, so visual review starts
  from a capture painted after the run, and a test story is of the code that ran
  in Node.
- Selection in watch mode, or under `--filter` or `--skipFilter` on the command
  line.
- A project named by path in `projects`: write it inline to have it recorded.

## Go further

- [Record Jest journeys without selecting tests](https://variance-authority.dev/reference/packages/sense/index.md#record-jest-journeys-without-selecting-tests), with `withJourneyCoverage`.
- [A result a cache returned](https://variance-authority.dev/docs/selecting/index.md#a-result-a-cache-returned): the one gap inside a Jest file.
- [What a skipped test promises](https://variance-authority.dev/docs/selection-safety/index.md): the rule behind each Jest skip.
- [Supported frameworks](https://variance-authority.dev/docs/supported-frameworks/index.md) puts Jest beside every other runner.
