VARIANCE AUTHORITY
BrowseVitest

Your test suite

Vitest

Variance Authority 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

npm install --save-dev @variance-authority/sense @variance-authority/cli
// 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:

$ 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 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 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 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 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 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. 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. 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