Speed up Mocha execution by only running tests impacted by a change.
Beta The plugin is under active development; options and the record format may change between releases. Please report any issues in the GitHub repository
npm install --save-dev @useblacksmith/mochaPut blacksmith-mocha run -- in front of your Mocha arguments:
npx blacksmith-mocha run -- 'test/**/*.test.js'Or keep the plain mocha command and select from .mocharc.cjs:
module.exports = require('@useblacksmith/mocha/mocharc')({
spec: ['test/**/*.test.js'],
})Serial and --parallel runs are both supported. Requires Node 22.15 or newer
and Mocha 10 or newer.
-
Imports. Every
require()andimportis recorded as an edge between two files. A spec file depends on everything reachable from it. -
Files, processes, workers. Files read through
node:fs, and the modules and reads of node child processes and worker threads, are recorded for the test that caused them. A file a module reads once and keeps is recorded for the module, so every spec importing it depends on the file. -
Per test. A record is kept for each test. A spec file is skipped only when every test in it has passed against the current files, so files run in part with
--grepare handled: tests that have not run yet keep the file selected. -
Changes. The recorded files are compared by content with what they were when the record was made, so compiled output that git ignores (
dist/) works too. -
Installed packages. The import graph names the packages a spec file loaded, transitive ones included, each with the version in its
package.json. After an install, a spec file reruns when one of its packages has another version or is gone. Lockfiles are not compared, and apackage.jsonchange that only touches its dependency fields reruns nothing by itself. A new version of a package loaded before the first spec file (Mocha, what.mocharc.*requires) runs everything.
A tsconfig*.json in a directory below the project reruns the test files that
loaded a file under that directory. So does a package.json, when the fields
modules resolve by change (exports, imports, type, main, name). A change to
anything else in a package.json reruns only the test files that read the
file themselves or that a dependencies rule maps it to.
Changes to the mocha, babel or browserslist field of package.json, to tsconfig.json or
.mocharc.* run everything.
Each run ends with a summary of what it ran and skipped and how it ended, then the same as JSON for whatever collects the job's logs:
[blacksmith] summary: passed; ran 28 of 540 spec files, skipped 512 unaffected; tests: 310 passed, 0 failed, 2 skipped; dependency record restored, saved; plugin took 1.2s
[blacksmith] run {"v":1,"plugin":"@useblacksmith/mocha","version":"1.0.0","runner":"mocha","outcome":"passed","files":{"total":540,"affected":28,"skipped":512,"ran":28,"failed":0},...}
On a Blacksmith runner the plugin also sends that JSON to the runner, with the repository's id, so Blacksmith can show what test selection saved. It sends nothing else, does not wait for an answer, and a report that is not taken changes nothing about the run.
A change to an ES module does not rerun every spec file that loaded it. The recorder notes, for each spec file, which top-level functions its tests and hooks entered in every module it loaded, and a change to the body of a function reruns the spec files that entered it. What else in a file counts as changed for whom is as in the Jest plugin.
Spec files share one module cache, so a function a module calls while it loads counts as entered by every spec file that loads the module, and so does one entered outside any test or hook of a spec file (a root hook).
Any change reruns every spec file that loaded the file when the file is:
- a CommonJS module (
require,module.exports), or anything the Jest plugin's list names; - compiled by a loader that reads the source itself. TypeScript loaded with
--require ts-node/registeris narrowed; with--require tsx/cjsit is not. Other loaders are untested: one that hands the recorder a source other than the file's is left alone.
Narrowing needs module.registerHooks (Node 22.15) and is off under
coverage (NODE_V8_COVERAGE, as c8 sets, or nyc), where the added calls
would be counted; BLACKSMITH_MOCHA_NARROW=0 turns it off too.
To record function entries the recorder adds one call at the start of each top-level function and of the module, before any other loader compiles it. Line numbers stay as they are; columns on the first line of a function and of the file move right.
Options go in blacksmith-mocha.json in the directory Mocha runs from:
{
"services": {
"localhost:5432": ["db/migrations/**"],
"localhost:4010": ["mocks/**"]
},
"dependencies": {
"test/e2e/**": ["docker/**"]
}
}| option | default | effect |
|---|---|---|
dependencies |
{} |
Spec file glob → globs of files those spec files depend on that nothing reads, e.g. { "test/e2e/**": ["docker/**"] }. Globs are relative to the repository root. |
services |
{} |
A database or a mock service is not a file a test reads. The plugin records which host:port each test talks to; this maps those to globs of the files that define what answers there, e.g. { "localhost:5432": ["db/migrations/**"] }. |
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. |
- Mocha loads all spec files of a process into one module cache. State a spec leaves in memory for a later one is not a file, and is not tracked; 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 is known by its directory and version. Not seen: a package
changed in place at the same version (
patch-package, a git orfile:dependency) and a package installed outside the repository. A copy installed later in front of the one a spec file loaded (inside a workspace, as Yarn and npm do for a version that differs from the hoisted one, or inside the package that loads it) counts as a change. With pnpm, which links each name to one of the versions it keeps side by side, a package also counts as changed when a link that led to it now leads to another version. - A test that reads
package.jsonthrough a helper package reruns on any change to it, unless the helper reads it while being imported.