Skip to content

Commit c5a0ef5

Browse files
docs: improve third-party reporter docs (#5285)
* Improve hosted docs for `runnable` * Document usages of `Runner.constants` * Update lib/runnable.js Co-authored-by: Josh Goldberg ✨ <[email protected]> * Update lib/runnable.js --------- Co-authored-by: Josh Goldberg ✨ <[email protected]>
1 parent 75dcf8c commit c5a0ef5

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

lib/runnable.js

+2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ var MAX_TIMEOUT = Math.pow(2, 31) - 1;
2424

2525
module.exports = Runnable;
2626

27+
// "Additional properties" doc comment added for hosted docs (mochajs.org/api)
2728
/**
2829
* Initialize a new `Runnable` with the given `title` and callback `fn`.
30+
* Additional properties, like `getFullTitle()` and `slow()`, can be viewed in the `Runnable` source.
2931
*
3032
* @class
3133
* @extends external:EventEmitter

lib/runner.js

+40-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,39 @@ var globals = [
4747

4848
var constants = utils.defineConstants(
4949
/**
50-
* {@link Runner}-related constants.
50+
* {@link Runner}-related constants. Used by reporters. Each event emits the corresponding object, unless otherwise indicated.
51+
* @example
52+
* const Mocha = require('mocha');
53+
* const Base = Mocha.reporters.Base;
54+
* const {
55+
* EVENT_HOOK_BEGIN,
56+
* EVENT_TEST_PASS,
57+
* EVENT_TEST_FAIL,
58+
* EVENT_TEST_END
59+
* } = Mocha.Runner.constants
60+
*
61+
* function MyReporter(runner, options) {
62+
* Base.call(this, runner, options);
63+
*
64+
* runner.on(EVENT_HOOK_BEGIN, function(hook) {
65+
* console.log('hook called: ', hook.title);
66+
* });
67+
*
68+
* runner.on(EVENT_TEST_PASS, function(test) {
69+
* console.log('pass: %s', test.fullTitle());
70+
* });
71+
*
72+
* runner.on(EVENT_TEST_FAIL, function(test, err) {
73+
* console.log('fail: %s -- error: %s', test.fullTitle(), err.message);
74+
* });
75+
*
76+
* runner.on(EVENT_TEST_END, function() {
77+
* console.log('end: %d/%d', runner.stats.passes, runner.stats.tests);
78+
* });
79+
* }
80+
*
81+
* module.exports = MyReporter;
82+
*
5183
* @public
5284
* @memberof Runner
5385
* @readonly
@@ -97,7 +129,13 @@ var constants = utils.defineConstants(
97129
*/
98130
EVENT_TEST_END: 'test end',
99131
/**
100-
* Emitted when {@link Test} execution fails
132+
* Emitted when {@link Test} execution fails. Includes an `err` object of type `Error`.
133+
* @example
134+
* runner.on(EVENT_TEST_FAIL, function(test, err) {
135+
* console.log('fail: %s -- error: %s', test.fullTitle(), err.message);
136+
* });
137+
*
138+
*
101139
*/
102140
EVENT_TEST_FAIL: 'fail',
103141
/**

0 commit comments

Comments
 (0)