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 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.
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, throughimport, 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.blacksmith-mocha run -- in front of your Mocha arguments:
mocha command and select from .mocharc.cjs:
--parallel runs are both supported.
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.
Options
Options live inblacksmith-mocha.json at the repository root. Globs are relative to the repository root.
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 withpatch-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:
[blacksmith] nothing to run and the step passes.