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

# Mocha Test Selection

> Blacksmith runs only the Mocha tests a change can affect

## Overview

Every CI run of a Mocha suite starts from zero and runs every test, even when a change only touches a few files. The [@useblacksmith/mocha](https://www.npmjs.com/package/@useblacksmith/mocha) plugin uses dependency tracking to run only the tests affected since the last successful run. It is one command in front of your Mocha arguments, or one wrapper in `.mocharc.cjs`, with no other 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 depends on, through `import`, `require`, and `fs`. The next run compares the checkout against that record and runs only the spec files that reach a changed file.

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

## Install the plugin

The plugin requires Mocha 10 or newer and Node 22.15 or newer.

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

Put `blacksmith-mocha run --` in front of your Mocha arguments:

```sh theme={"system"}
npx blacksmith-mocha run -- 'test/**/*.test.js'
```

Or keep the plain `mocha` command and select from `.mocharc.cjs`:

```js theme={"system"}
// .mocharc.cjs
module.exports = require('@useblacksmith/mocha/mocharc')({
  spec: ['test/**/*.test.js'],
})
```

Serial and `--parallel` runs are both supported.

Add the @useblacksmith/mocha plugin to the Mocha setup 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

### Shards

`blacksmith-mocha run --shard i/n` assigns spec files to shards by a hash of their path, so a file stays in its shard as others are added or skipped. With `.mocharc.cjs`, set `BLACKSMITH_MOCHA_SHARD=i/n`. Changing the shard count reruns nothing.

### Running locally

The same selection applies locally: a run reruns what changed on disk since the last successful run, committed or not.

## List the affected tests

`blacksmith-mocha affected` prints the affected spec files and runs none. Add `--json` for JSON that names the tests to run in each file, and `--shard i/n` for one shard.

```sh theme={"system"}
npx blacksmith-mocha affected --json -- 'test/**/*.test.js'
```

## Options

Options live in `blacksmith-mocha.json` at the repository root. Globs are relative to the repository root.

| Option | Default | Effect |
| - | - | - |
| `dependencies` | `{}` | Dependencies the tracking cannot see, as globs of spec files mapped to the globs of the files they depend on: `{ "test/e2e/**": ["docker/**"] }`. A change to a matching file reruns the mapped tests. |
| `services` | `{}` | The files behind a service tests talk to, by `host:port`: `{ "localhost:5432": ["db/migrations/**"] }`. A change to a matching file reruns the tests that connected there. |
| `cacheKey` | none | A name for what surrounds the run that no file tells, such as the database engine in a job matrix. Runs with different values keep separate records. |
| `maxShards` | `20` | The largest `--shard` count accepted. A run split further fails. |

## Limitations

Mocha loads all spec files of a process into one module cache. A test that only passes after another spec file ran can fail when that file is skipped.

Tests are identified by their full title. Two tests with the same title in one file share a record.

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

## 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-mocha-v7-node24-linux-x64-9ecc8459ea5f-shard1-6f4287d2a5a61b9ec1613cff76b867b8bb0a194a
[blacksmith] 1 of 3 spec file(s) affected; changed: src/pricing.js
  run  test/pricing.test.js  (changed: src/pricing.js)

  ✔ total

  1 passing (2ms)

[blacksmith] saved dependency cache blacksmith-mocha-v7-node24-linux-x64-9ecc8459ea5f-shard1-80fdb3da31055af7765f5e282fcff9a28a978bf2 (688 bytes)
[blacksmith] record: 3 spec file(s), 3 of 3 test(s) vouched for, 7 path(s) anchored
```

When no change affects any test, the plugin prints `[blacksmith] nothing to run` and the step passes.


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