> ## Documentation Index
> Fetch the complete documentation index at: https://docs.blacksmith.sh/llms.txt
> Use this file to discover all available pages before exploring further.

# Vitest Test Selection

> Blacksmith runs only the Vitest tests a change can affect

## Overview

Every CI run of a Vitest suite starts from zero and runs every test, even when a change only touches a few files. The [@useblacksmith/vitest](https://www.npmjs.com/package/@useblacksmith/vitest) plugin uses dependency tracking to run only the tests affected since the last successful run. It is one plugin in `vitest.config.ts`, with no workflow change.

> [!NOTE]
>
> The plugin is in early beta. Options and the cache format may change between releases. Please report any issues in the [GitHub repository](https://github.com/useblacksmith/vitest/issues).

## How it works

Each run records the modules and files every test depends on, through `import`, `require`, and `fs`. The next run compares the checkout against that record and runs only the tests that reach a changed file. A test the last successful run did not cover always runs.

On Blacksmith runners, Blacksmith transparently stores this record from run to run, with no extra workflow steps. Jobs that run the same suite share one record, so a test that passed in one job is skipped in another.

## Install the plugin

The plugin requires Vitest 4.1.1 or newer.

```sh theme={"system"}
npm install --save-dev @useblacksmith/vitest
```

```ts theme={"system"}
// vitest.config.ts
import { defineConfig } from 'vitest/config'
import blacksmith from '@useblacksmith/vitest'

export default defineConfig({
  plugins: [blacksmith()],
})
```

`vitest run` now runs the affected tests.

Add the @useblacksmith/vitest plugin to the Vitest config in \<repository name> and open a PR.

You get back: A PR adding the plugin, so CI runs only the tests affected by each change.

Try it in \[code]smith

### Vite+

Vite+ runs Vitest under `vp test`, so the plugin is loaded from the shared `vite.config.ts`. It requires `vite-plus` 1.5.0 or newer, the first release that ships Vitest 5.1.

```ts theme={"system"}
// vite.config.ts
import 'vite-plus/test/config'
import { defineConfig } from 'vite-plus'
import blacksmith from '@useblacksmith/vitest'

export default defineConfig({
  plugins: [blacksmith()],
})
```

## Options

| Option | Default | Effect |
| - | - | - |
| `runAllTests` | `false` | Run every test instead of the affected selection. |
| `dependencies` | `{}` | Dependencies the tracking cannot see, as globs of test files mapped to the glob(s) of the files they depend on, relative to the config root: `{ 'tests/**/*.spec.ts': 'fixtures/**' }`. A change to a matching file reruns the mapped tests. |
| `cacheKey` | none | Metadata used for skipping is keyed by working directory, test configuration and runtime (Node major version, OS, architecture). Set this if you need additional granularity, e.g. a job matrix that runs the same suite against different databases, services or environment variables. |
| `maxShards` | `20` | The largest `--shard` count accepted. A run split further fails. |

## Limitations

Files read by `globalSetup`, by programs other than Node, or reached over the network are not tracked; declare them with the `dependencies` option.

A package changed in place at the same version, such as with `patch-package` or a git dependency, is not seen as changed.

A run with coverage runs every test.

## Verify the selection

The plugin logs every decision with a `[blacksmith]` prefix in the job output: which records were restored, how many test files are affected, and why each one runs. The first run on a repository has no record and runs everything; compare the test step's duration across later runs.

A run after a change to one source file restores the record, runs only the test file that depends on it, and saves a new record:

```text theme={"system"}
[blacksmith] restored dependency cache blacksmith-vitest-v7-node24-linux-x64-9ecc8459ea5f-shard1-50284a30936f668205db594eb10ef1476f6a4ea3

 RUN  v5.1.0 /home/runner/work/app/app

[blacksmith] 1 of 3 test file(s) affected; changed: src/pricing.js
  run  test/pricing.test.js  (changed: src/pricing.js)
 ✓ test/pricing.test.js (1 test) 1ms

 Test Files  1 passed (1)
   Affected  1 of 3 test files (related to 1 changed file)
      Tests  1 passed (1)

[blacksmith] filtered out 2 of 3 test files as unaffected, ran 1
[blacksmith] saved dependency cache blacksmith-vitest-v7-node24-linux-x64-9ecc8459ea5f-shard1-26520c57cc6a09f83c7c3e83716544c2b461d700 (653 bytes)
[blacksmith] record: 3 of 3 test file(s) vouched for, 10 path(s) anchored
```

When no change affects any test, Vitest prints `No changed files found, exiting with code 0` and the step passes.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.