Skip to content

Commit f6e8d9a

Browse files
docs: switch util.inherits to class extends (#5677)
Co-authored-by: Mark Wiemer <7833360+mark-wiemer@users.noreply.github.com> Co-authored-by: Mark Wiemer <markwiemer@outlook.com>
1 parent 722edec commit f6e8d9a

1 file changed

Lines changed: 43 additions & 36 deletions

File tree

docs/src/content/docs/explainers/third-party-reporters.mdx

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,62 @@ description: Create custom reporters.
33
title: Third party reporters
44
---
55

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`.
6+
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`.
77

8-
Here is a minimalistic sample reporter, which you can use by executing: `mocha --reporter my-reporter.js`
8+
:::note[New in v12.0.0]
9+
In Mocha 12, the extension logic is switched to modern `class`es instead of `util.inherits`.
10+
If you're on Mocha 11 or older, use `mocha.util.inherits`. See [v11.mochajs.org](https://v11.mochajs.org/explainers/third-party-reporters/) for details.
11+
:::
12+
13+
Here is a minimalistic sample reporter for Mocha 12, which you can use by executing: `mocha --reporter my-reporter.js`
914

1015
```js
1116
// my-reporter.js
1217
import mocha from "mocha";
1318

14-
export default function MyReporter(runner) {
15-
mocha.reporters.Base.call(this, runner);
16-
let passes = 0;
17-
let failures = 0;
19+
// You can extend other reporters by changing the class you extend:
20+
// export default class MyReporter extends mocha.reporters.Spec {
21+
export default class MyReporter extends mocha.reporters.Base {
22+
constructor(runner) {
23+
super(runner);
1824

19-
runner.on("pass", function (test) {
20-
passes++;
21-
console.log("pass: %s", test.fullTitle());
22-
});
25+
let passes = 0;
26+
let failures = 0;
2327

24-
runner.on("fail", function (test, err) {
25-
failures++;
26-
console.log("fail: %s -- error: %s", test.fullTitle(), err.message);
27-
});
28+
runner.on("pass", function (test) {
29+
passes++;
30+
console.log("pass: %s", test.fullTitle());
31+
});
2832

29-
runner.on("end", function () {
30-
console.log("end: %d/%d", passes, passes + failures);
31-
});
32-
}
33+
runner.on("fail", function (test, err) {
34+
failures++;
35+
console.log("fail: %s -- error: %s", test.fullTitle(), err.message);
36+
});
3337

34-
// To have this reporter "extend" a built-in reporter uncomment the following line:
35-
// mocha.utils.inherits(MyReporter, mocha.reporters.Spec);
38+
runner.on("end", function () {
39+
console.log("end: %d/%d", passes, passes + failures);
40+
});
41+
}
42+
}
3643
```
3744

38-
For details look at the implementations in [lib/reporters/\*](https://github.com/mochajs/mocha/tree/main/lib/reporters).
45+
For details, look at the implementations in [lib/reporters/\*](https://github.com/mochajs/mocha/tree/main/lib/reporters).
3946

40-
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).
47+
[mocha-examples: third-party-reporter (GitHub)](https://github.com/mochajs/mocha-examples/tree/main/packages/third-party-reporter) is another sample implementation.
4148

4249
Mocha provides the following events:
4350

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
51+
- `start`: Execution started
52+
- `waiting`: Execution of root `Suite` delayed
53+
- `ready`: Execution of root `Suite` started
54+
- `end`: Execution complete
55+
- `suite`: Test suite execution started
56+
- `suite end`: All tests (and sub-suites) have finished
57+
- `test`: Test execution started
58+
- `test end`: Test completed
59+
- `hook`: Hook execution started
60+
- `hook end`: Hook complete
61+
- `pass`: Test passed
62+
- `fail`: Test failed
63+
- `pending`: Test pending
64+
- `retry`: Test failed and retries

0 commit comments

Comments
 (0)