In the Mocha/Chai javascript testing framework is it possible to remove the millisecond speed reported by Mocha?
I'd like an easier way to compare outputs from different tests, and currently each test line shows a diff since the speed (ms) varies from run to run.
test1.js
description here
✔ should do the things (211ms)
Mocha use spec as its default reporter.The spec reporter inherits from Base reporter. It use .epilogue() method of Base for EVENT_RUN_END event.
The method will print the milliseconds, the source code:
Base.consoleLog(fmt, stats.passes || 0, milliseconds(stats.duration));
You can create a custom reporter for mocha.
./reporters/tidy.js:
const Mocha = require('mocha');
const { EVENT_RUN_END, EVENT_TEST_FAIL, EVENT_TEST_PASS } = Mocha.Runner.constants;
class Tidy {
constructor(runner) {
const stats = runner.stats;
runner
.on(EVENT_TEST_PASS, (test) => {
console.log(`pass: ${test.fullTitle()}`);
})
.on(EVENT_TEST_FAIL, (test, err) => {
console.log(`fail: ${test.fullTitle()} - error: ${err.message}`);
})
.once(EVENT_RUN_END, () => {
console.log(`end: ${stats.passes}/${stats.passes + stats.failures} ok`);
});
}
}
module.exports = Tidy;
Use fullTitle() → {string} to
Return the full title generated by recursively concatenating the parent's full title.
It will give you pass: <test suite title> <test case title> string.
Example test:
const { expect } = require('chai');
describe('description here', () => {
it('should pass', () => {
expect(1 + 1).to.be.equal(2);
});
it('should fail', () => {
expect(1 + 1).to.be.equal(3);
});
});
Output:
⚡ npx mocha --reporter ./reporters/tidy.js /Users/dulin/workspace/github.com/mrdulin/expressjs-research/reporters/index.test.js
pass: description here should pass
fail: description here should fail - error: expected 2 to equal 3
end: 1/2 ok
While the accepted answer is more sophisticated, I've found a much cheaper way to remove the speeds by increasing the --slow option to a very large number since the speed only appears for slow tests.