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

# Jest Test Selection

> Blacksmith runs only the Jest tests a change can affect

## Overview

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

> [!NOTE]
>
> The plugin is in early beta. Options and the cache format may change between releases.

## How it works

Each run records the modules and files every test file depends on, through `import`, `require`, and `fs`. The next run compares the checkout against that record and runs only the test files that reach a changed file. A test file with no record, or with a test that failed last time, always runs.

On Blacksmith runners, Blacksmith transparently stores this record from run to run, with no extra workflow steps.

## Install the plugin

The plugin requires Jest 29 or newer and Node 22.15 or newer.

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

```js theme={"system"}
// jest.config.js
const { withBlacksmith } = require('@useblacksmith/jest')

module.exports = withBlacksmith({
  testEnvironment: 'node',
})
```

`jest` now runs the affected selection.

Add the @useblacksmith/jest plugin to the Jest 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

### Projects

Inline project objects in `projects` are wrapped with the rest of the config. A project given as a path needs the plugin's runtime in its own config:

```js theme={"system"}
runtime: require.resolve('@useblacksmith/jest/runtime'),
```

### Running locally

The same selection applies locally: a run reruns what changed on disk since the last successful run, committed or not. Watch mode is left to Jest.

## Options

Options are the second argument of `withBlacksmith`.

| Option | Default | Effect |
| - | - | - |
| `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 repository root: `{ 'test/db/**': 'migrations/**' }`. A change to a matching file reruns the mapped tests. |
| `cacheKey` | none | Prefix to key results by. Useful when running multiple projects that differ by something untracked, e.g. environment variables. |
| `maxShards` | `20` | The largest `--shard` count accepted. A run split further fails. |

## Limitations

The plugin sets Jest's `runtime` and `testSequencer` and adds a reporter. A config that sets `runtime` or `testSequencer` itself is refused, and `--reporters` on the command line stops the record from updating.

What a test reaches over the network, such as a database or a mock server, is not tracked; declare the files behind it 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-jest-v7-node24-linux-x64-9ecc8459ea5f-shard1-6f4287d2a5a61b9ec1613cff76b867b8bb0a194a
[blacksmith] 1 of 3 test file(s) affected; changed: src/pricing.js
  run  test/pricing.test.js  (changed: src/pricing.js)
PASS test/pricing.test.js
  ✓ total (1 ms)

[blacksmith] saved dependency cache blacksmith-jest-v7-node24-linux-x64-9ecc8459ea5f-shard1-80fdb3da31055af7765f5e282fcff9a28a978bf2 (702 bytes)
[blacksmith] record: 3 of 3 test file(s) vouched for, 8 path(s) anchored
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
```

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


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