node-test-mocha-reporter
v1.0.1
Published
A custom reporter for the Node.js built-in test runner (node:test) that prints results like mocha's default spec reporter. Zero dependencies.
Downloads
343
Maintainers
Readme
node-test-mocha-reporter
A custom reporter for the Node.js built-in test runner (node:test) that prints results like mocha's default spec reporter. Zero dependencies.
Why
I replaced mocha with node:test to have fewer dependencies to keep updating.
The built-in spec reporter of node:test works, but it's noisy:
every line has a timing, suites are marked with ▶, and each suite is repeated after its tests.
I found mocha's output much easier to read, so this reporter brings it back.
If I ever don't want to maintain it, switching back to --test-reporter=spec is a one-word change.
Usage
Install it as a dev dependency:
npm install --save-dev node-test-mocha-reporterThen pass it to node --test, for example in the test script of package.json:
{
"scripts": {
"test": "node --test --test-reporter=node-test-mocha-reporter"
}
}It requires Node.js 22 or later.
Output
This is the output for three of the test fixtures, failing.js, pending.js and slow.js:
suite
✔ passes
1) fails
2) fails with a multiline message
suite
- is skipped
- is todo
- skips itself
✔ passes
suite
✔ takes a while (102ms)
3 passing (144ms)
3 pending
2 failing
1) suite
fails:
Error: boom
at failing.js:5:3
2) suite
fails with a multiline message:
Error: first line
second line
at failing.js:8:3
What it supports, the same way mocha does:
- Failures are numbered in the tree, and listed after the summary with their suite path, error and location.
- Skipped and todo tests (
it.skip,it.todo,t.skip()) are shown as- nameand counted as pending. - Failed hooks are reported with their own error, e.g.
"before all" hookor"before each" hook for "test name". Tests cancelled because of a failedbeforehook aren't listed separately. - Test files that crash while loading are listed as a failure, e.g.
test file exited with code 1, see its output above. The crash itself is in the file's output, printed before the results. - Slow tests show their duration: yellow above 37ms, red above 75ms, as with mocha's default
slow: 75. - Colors are used when the output is a terminal.
FORCE_COLOR=1andNO_COLOR=1work as usual.
Differences from mocha
- Console output of a test file is printed before that file's results, not next to the test that logged it. Node runs each test file in a separate process and reports its output first, so there's no way around it.
- Locations point to where the failed test is declared, not to the line that threw. The location is relative to the working directory when the file is inside it, and absolute otherwise.
- Thresholds for slow tests are fixed at mocha's defaults, there's no
--slowoption.
How it works
node:test emits events like test:start, test:pass and test:fail, and the reporter turns them into lines of output.
The only non-obvious part is suite headers:
test:start doesn't tell suites from tests, so a suite's name is printed right before the first result inside it.
Tests
The tests run each file in fixtures/ under node --test with the reporter, and compare the whole output.
They cover passing and failing tests, pending tests, failed hooks, crashing test files, console output, slow tests, locations, colors and exit codes.
Test durations are removed before comparing, so a slow machine doesn't make the tests fail.
npm testThe test script names index.test.js explicitly, so the fixtures never run as tests of their own.
Node's default patterns also match every file in a test/ folder, so they could pick up the fixtures if they were moved there.
Credits
Only the output format follows mocha (MIT licensed), none of its code is used.
