Third party reporters
Mocha allows you to define custom third-party reporters within your own test suite, or by using npm modules. For example if “lcov-reporter” was published to npm, you would simply add it to your package.json in devDependencies and use --reporter lcov-reporter.
Here is a minimalistic sample reporter for Mocha 12, which you can use by executing: mocha --reporter my-reporter.js
import mocha from "mocha";
// You can extend other reporters by changing the class you extend:// export default class MyReporter extends mocha.reporters.Spec {export default class MyReporter extends mocha.reporters.Base { constructor(runner) { super(runner);
let passes = 0; let failures = 0;
runner.on("pass", function (test) { passes++; console.log("pass: %s", test.fullTitle()); });
runner.on("fail", function (test, err) { failures++; console.log("fail: %s -- error: %s", test.fullTitle(), err.message); });
runner.on("end", function () { console.log("end: %d/%d", passes, passes + failures); }); }}For details, look at the implementations in lib/reporters/*.
mocha-examples: third-party-reporter (GitHub) is another sample implementation.
Mocha provides the following events:
start: Execution startedwaiting: Execution of rootSuitedelayedready: Execution of rootSuitestartedend: Execution completesuite: Test suite execution startedsuite end: All tests (and sub-suites) have finishedtest: Test execution startedtest end: Test completedhook: Hook execution startedhook end: Hook completepass: Test passedfail: Test failedpending: Test pendingretry: Test failed and retries