|
| 1 | +Mocha allows you to define and use custom reporters install via `npm`. |
| 2 | + |
| 3 | +For example, if `mocha-foo-reporter` was published to the npm registry, you could install it via `npm install mocha-foo-reporter -D`, then use it via `mocha --reporter mocha-foo-reporter`. |
| 4 | + |
| 5 | +## Example Custom Reporter |
| 6 | + |
| 7 | +If you're looking to get started quickly, here's an example of a custom reporter. |
| 8 | + |
| 9 | +```js |
| 10 | +// my-reporter.js |
| 11 | + |
| 12 | +const Mocha = require('mocha'); |
| 13 | +const { |
| 14 | + RUNNER_EVENT_END, |
| 15 | + RUNNER_EVENT_FAIL, |
| 16 | + RUNNER_EVENT_PASS, |
| 17 | + RUNNER_EVENT_SUITE, |
| 18 | + RUNNER_EVENT_SUITE_END |
| 19 | +} = Mocha.Runner.constants; |
| 20 | +const Base = Mocha.reporters.Base; |
| 21 | + |
| 22 | +// this reporter outputs test results, indenting two spaces per suite |
| 23 | +class MyReporter extends Base { |
| 24 | + constructor(runner) { |
| 25 | + super(runner); |
| 26 | + |
| 27 | + this._indents = 0; |
| 28 | + |
| 29 | + runner |
| 30 | + .once(RUNNER_EVENT_START, () => { |
| 31 | + console.log('start'); |
| 32 | + }) |
| 33 | + .on(RUNNER_EVENT_SUITE, () => { |
| 34 | + this.increaseIndent(); |
| 35 | + }) |
| 36 | + .on(RUNNER_EVENT_SUITE_END, () => { |
| 37 | + this.decreaseIndent(); |
| 38 | + }) |
| 39 | + .on(RUNNER_EVENT_PASS, test => { |
| 40 | + // Test#fullTitle() returns the suite name(s) |
| 41 | + // prepended to the test title |
| 42 | + console.log(`${this.indent()}pass: ${test.fullTitle()}`); |
| 43 | + }) |
| 44 | + .on(RUNNER_EVENT_FAIL, (test, err) => { |
| 45 | + console.log( |
| 46 | + `${this.indent()}fail: ${test.fullTitle()} - error: ${err.message}` |
| 47 | + ); |
| 48 | + }) |
| 49 | + .once(RUNNER_EVENT_END, () => { |
| 50 | + // Base reporter has a `stats` property |
| 51 | + console.log( |
| 52 | + `end: ${this.stats.passes}/${this.stats.passes + |
| 53 | + this.stats.failures} ok` |
| 54 | + ); |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + indent() { |
| 59 | + return Array(this._indents) |
| 60 | + .fill(' ') |
| 61 | + .join(''); |
| 62 | + } |
| 63 | + |
| 64 | + increaseIndent() { |
| 65 | + this._indents++; |
| 66 | + } |
| 67 | + |
| 68 | + decreaseIndent() { |
| 69 | + this._indents--; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +module.exports = MyReporter; |
| 74 | +``` |
| 75 | + |
| 76 | +To use this reporter, execute `mocha --reporter /path/to/my-reporter.js`. |
| 77 | + |
| 78 | +For further examples, the built-in reporter implementations are the [best place to look](<(https://github.com/mochajs/mocha/tree/master/lib/reporters)>). The [integration tests](https://github.com/mochajs/mocha/tree/master/test/reporters) may also be helpful. |
| 79 | + |
| 80 | +## Events |
| 81 | + |
| 82 | +A reporter should listen for events emitted from the `runner` (an instance of [Runner]). |
| 83 | + |
| 84 | +The event names are exported from the `constants` property of `Mocha.Runner`: |
| 85 | + |
| 86 | +| Constant | Event Name | Event Arguments | Description | |
| 87 | +| ------------------------ | ----------- | --------------- | ------------------------------------------------------------------------------------------- | |
| 88 | +| `RUNNER_EVENT_END` | `end` | _(n/a)_ | All [Suite]s, [Test]s and [Hook]s have completed execution | |
| 89 | +| `RUNNER_EVENT_FAIL` | `fail` | `Test`, `Error` | A [Test] has failed or thrown an exception | |
| 90 | +| `RUNNER_EVENT_HOOK` | `hook` | `Hook` | A [Hook] is about to execute | |
| 91 | +| `RUNNER_EVENT_HOOK_END` | `hook end` | `Hook` | A [Hook] has completed execution | |
| 92 | +| `RUNNER_EVENT_PASS` | `pass` | `Test` | A [Test] has passed | |
| 93 | +| `RUNNER_EVENT_PENDING` | `pending` | `Test` | A [Test] was skipped | |
| 94 | +| `RUNNER_EVENT_RETRY` | `retry` | `Test`, `Error` | A [Test] failed, but is about to be retried; never emitted unless `retry` option is nonzero | |
| 95 | +| `RUNNER_EVENT_START` | `start` | _(n/a)_ | Execution will begin | |
| 96 | +| `RUNNER_EVENT_SUITE` | `suite` | `Suite` | The [Hook]s and [Test]s within a [Suite] are about to be executed | |
| 97 | +| `RUNNER_EVENT_SUITE_END` | `suite end` | `Suite` | The [Hook]s and [Test]s within a [Suite] (and any children [Suite]s) completed execution | |
| 98 | +| `RUNNER_EVENT_TEST` | `test` | `Test` | A [Test] is about to be executed | |
| 99 | +| `RUNNER_EVENT_TEST_END` | `test end` | `Test` | A [Test] has completed execution | |
| 100 | +| `RUNNER_EVENT_WAITING` | `waiting` | _(n/a)_ | Waiting for `global.run()` to be called; only emitted when `delay` option is `true` | |
| 101 | + |
| 102 | +**Please use these constants** instead of the event names in your own reporter! This will ensure compatibility with future versions of Mocha. |
| 103 | + |
| 104 | +> It's important to understand that all suite callbacks will be run _before_ the [Runner] emits `RUNNER_EVENT_START`. Hooks and tests, however, won't run until _after_ the [Runner] emits `RUNNER_EVENT_START`. |
| 105 | +
|
| 106 | +## Custom Reporter for Browser |
| 107 | + |
| 108 | +As of Mocha v6.0.0, custom reporters are _only_ "officially" supported in Node.js. [Mochify](https://npm.im/mochify) might help you. YMMV! |
| 109 | + |
| 110 | +[runner]: /api/mocha.runner |
| 111 | +[test]: /api/mocha.test |
| 112 | +[hook]: /api/mocha.hook |
| 113 | +[suite]: /api/mocha.suite |
0 commit comments