# `node:test`

With [Variance Authority](https://variance-authority.dev/docs/index.md), your `node:test` suite gets a record of
the lines of your code each case ran. One script replaces the `node --test`
command, and nothing in your test files changes. Then CI starts only the test
files your change reached, you can read what one test did, and you can ask
which tests cover a line.

## Try it

You need Node 22.15 or newer. The script runs each file in its own process, as
`node --test` does.

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

```js
// scripts/test.mjs
import { runNodeTest } from '@variance-authority/sense/node-test';

const { passed } = await runNodeTest({
  files: ['test/alpha.test.mjs', 'test/beta.test.cjs'],
});
process.exitCode = passed ? 0 : 1;
```

Run `node scripts/test.mjs` once to write the record, change a source file, and
run again:

```bash
$ VARIANCE_AUTHORITY_SINCE=origin/main node scripts/test.mjs
variance-authority: selected 1 of 2
```

## What you get

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

The files the record shows the diff did not reach are not started. A file that
failed, or whose process died, is recorded incomplete and runs next time. Run
the suite without the variable on your mainline after a merge, so the next diff
is read against a fresh record. [Run the tests an edit needs](https://variance-authority.dev/docs/test-an-edit/index.md)
walks through the loop.

### Read one test's story

Set `VARIANCE_AUTHORITY_STORY=1` on the script for one file, then run
`variance story`. It lists the calls of your code the case made, in order.
[Record one](https://variance-authority.dev/docs/test-stories/index.md#record-one) shows the steps.

### Ask which tests cover a line

```bash
variance covering --file src/label.cjs --function label
```

It names the cases that ran `label`, each under its `describe` blocks, such as
`label > takes the alpha path`.
[Covering](https://variance-authority.dev/reference/packages/cli/index.md#covering-which-tests-covered-this-line)
lists every way to ask.

## Not yet

- Skipping cases inside a kept file: a kept file runs whole.
- Shard placement by recorded time.
- A documented setup for visual review.
- Recording under Bun's `bun test`: [Bun is not
  recorded](https://variance-authority.dev/docs/node-test-reference/index.md#bun-is-not-recorded) says why and what runs
  instead.

## Go further

- [The `node:test` reference](https://variance-authority.dev/docs/node-test-reference/index.md): what the script records and how it names each case.
- [Record a runner this package has no seam for](https://variance-authority.dev/reference/packages/sense/index.md#record-a-runner-this-package-has-no-seam-for), for `suite`, `root` and every option.
- [What a skipped test promises](https://variance-authority.dev/docs/selection-safety/index.md): the rule behind each `node:test` skip.
- [Supported frameworks](https://variance-authority.dev/docs/supported-frameworks/index.md) puts `node:test` beside every other runner.
