|
| 1 | +--- |
| 2 | +description: Create custom reporters. |
| 3 | +title: Third party reporters |
| 4 | +--- |
| 5 | + |
| 6 | +Mocha 1.3.0 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`. |
| 7 | + |
| 8 | +Here is a minimalistic sample reporter, which you can use by executing: `mocha --reporter my-reporter.js` |
| 9 | + |
| 10 | +```js |
| 11 | +// my-reporter.js |
| 12 | +var mocha = require('mocha'); |
| 13 | +module.exports = MyReporter; |
| 14 | + |
| 15 | +function MyReporter(runner) { |
| 16 | + mocha.reporters.Base.call(this, runner); |
| 17 | + var passes = 0; |
| 18 | + var failures = 0; |
| 19 | + |
| 20 | + runner.on('pass', function (test) { |
| 21 | + passes++; |
| 22 | + console.log('pass: %s', test.fullTitle()); |
| 23 | + }); |
| 24 | + |
| 25 | + runner.on('fail', function (test, err) { |
| 26 | + failures++; |
| 27 | + console.log('fail: %s -- error: %s', test.fullTitle(), err.message); |
| 28 | + }); |
| 29 | + |
| 30 | + runner.on('end', function () { |
| 31 | + console.log('end: %d/%d', passes, passes + failures); |
| 32 | + }); |
| 33 | +} |
| 34 | + |
| 35 | +// To have this reporter "extend" a built-in reporter uncomment the following line: |
| 36 | +// mocha.utils.inherits(MyReporter, mocha.reporters.Spec); |
| 37 | +``` |
| 38 | + |
| 39 | +For details look at the implementations in [lib/reporters/*](https://github.com/mochajs/mocha/tree/main/lib/reporters). |
| 40 | + |
| 41 | +Another sample implementation can be found at [mocha-examples: third-party-reporter (GitHub)](https://github.com/mochajs/mocha-examples/tree/main/packages/third-party-reporter). |
| 42 | + |
| 43 | +Mocha provides the following events: |
| 44 | +- **start**: Execution started |
| 45 | +- **waiting**: Execution of root `Suite` delayed |
| 46 | +- **ready**: Execution of root `Suite` started |
| 47 | +- **end**: Execution complete |
| 48 | +- **suite**: Test suite execution started |
| 49 | +- **suite end**: All tests (and sub-suites) have finished |
| 50 | +- **test**: Test execution started |
| 51 | +- **test end**: Test completed |
| 52 | +- **hook**: Hook execution started |
| 53 | +- **hook end**: Hook complete |
| 54 | +- **pass**: Test passed |
| 55 | +- **fail**: Test failed |
| 56 | +- **pending**: Test pending |
| 57 | +- **retry**: Test failed and retries |
0 commit comments