Skip to content

Commit 11a92c5

Browse files
committed
add custom reporter tutorial to API documentation
- configuration to support tutorials - sort jsdoc config object because fussy
1 parent 246a4de commit 11a92c5

File tree

4 files changed

+140
-19
lines changed

4 files changed

+140
-19
lines changed

docs/_config.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
# Jekyll config
2+
13
url: https://mochajs.org
24
exclude:
35
- README.md
46
- .*
57
- LICENSE*
8+
- api-tutorials
69
- API.md
710
repository: mochajs/mocha
811
source: docs
9-
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"custom-reporter": {
3+
"title": "Create a Custom Reporter"
4+
}
5+
}

jsdoc.conf.json

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
11
{
2-
"tags": {
3-
"allowUnknownTags": true
2+
"markdown": {
3+
"hardwrap": true,
4+
"parser": "gfm"
45
},
5-
"source": {
6-
"include": ["lib/", "./docs/API.md", "bin"]
6+
"mocha-docdash": {
7+
"sort": true,
8+
"static": false
79
},
8-
"plugins": ["plugins/markdown"],
910
"opts": {
10-
"encoding": "utf8",
11-
"template": "node_modules/@mocha/docdash",
1211
"destination": "docs/api",
12+
"encoding": "utf8",
1313
"recurse": true,
14+
"template": "node_modules/@mocha/docdash",
15+
"tutorials": "./docs/api-tutorials",
1416
"verbose": true
1517
},
16-
"markdown": {
17-
"parser": "gfm",
18-
"hardwrap": true
18+
"plugins": ["plugins/markdown"],
19+
"source": {
20+
"include": ["lib/", "./docs/API.md", "bin"]
21+
},
22+
"tags": {
23+
"allowUnknownTags": true
1924
},
2025
"templates": {
2126
"cleverLinks": false,
22-
"monospaceLinks": false,
2327
"default": {
24-
"outputSourceFiles": true,
25-
"includeDate": false
26-
}
27-
},
28-
"mocha-docdash": {
29-
"static": false,
30-
"sort": true
28+
"includeDate": false,
29+
"outputSourceFiles": true
30+
},
31+
"monospaceLinks": false
3132
}
3233
}

0 commit comments

Comments
 (0)