Skip to content

Commit 9642e60

Browse files
committed
renames, rearranging constants
- much destructuring - many renames - adds `EVENT_DELAY_END` emitted from `Runner` for reporter use; needs tests - consolidated use of `object.assign` polyfill into `utils` module - some docstring fixes/consistency - add `utils.createMap()` and `utils.defineConstants()`; needs more tests - fix broken custom assertion - don't needlessly extend `Base` reporter in simple reporter example
1 parent a585f07 commit 9642e60

39 files changed

+653
-429
lines changed

.wallaby.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,14 @@ module.exports = () => {
3838
const runningMocha = wallaby.testFramework;
3939
runningMocha.timeout(200);
4040
// to expose it/describe etc. on the mocha under test
41-
const mochaUnderTest = new (require('./'))();
42-
mochaUnderTest.suite.emit('pre-require', global, '', mochaUnderTest);
41+
const MochaUnderTest = require('./');
42+
const mochaUnderTest = new MochaUnderTest();
43+
mochaUnderTest.suite.emit(
44+
MochaUnderTest.Suite.constants.EVENT_FILE_PRE_REQUIRE,
45+
global,
46+
'',
47+
mochaUnderTest
48+
);
4349
// to make test/node-unit/color.spec.js pass, we need to run mocha in the project's folder context
4450
const childProcess = require('child_process');
4551
const execFile = childProcess.execFile;
@@ -53,6 +59,7 @@ module.exports = () => {
5359
return execFile.apply(this, arguments);
5460
};
5561
require('./test/setup');
56-
}
62+
},
63+
debug: true
5764
};
5865
};

docs/api-tutorials/custom-reporter.md

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,44 +11,42 @@ If you're looking to get started quickly, here's an example of a custom reporter
1111

1212
const Mocha = require('mocha');
1313
const {
14-
RUNNER_EVENT_BEGIN,
15-
RUNNER_EVENT_END,
16-
RUNNER_EVENT_FAIL,
17-
RUNNER_EVENT_PASS,
18-
RUNNER_EVENT_SUITE_BEGIN,
19-
RUNNER_EVENT_SUITE_END
14+
EVENT_RUN_BEGIN,
15+
EVENT_RUN_END,
16+
EVENT_TEST_FAIL,
17+
EVENT_TEST_PASS,
18+
EVENT_SUITE_BEGIN,
19+
EVENT_SUITE_END
2020
} = Mocha.Runner.constants;
21-
const Base = Mocha.reporters.Base;
2221

2322
// this reporter outputs test results, indenting two spaces per suite
24-
class MyReporter extends Base {
23+
class MyReporter {
2524
constructor(runner) {
2625
super(runner);
2726

2827
this._indents = 0;
2928

3029
runner
31-
.once(RUNNER_EVENT_BEGIN, () => {
30+
.once(EVENT_RUN_BEGIN, () => {
3231
console.log('start');
3332
})
34-
.on(RUNNER_EVENT_SUITE_BEGIN, () => {
33+
.on(EVENT_SUITE_BEGIN, () => {
3534
this.increaseIndent();
3635
})
37-
.on(RUNNER_EVENT_SUITE_END, () => {
36+
.on(EVENT_SUITE_END, () => {
3837
this.decreaseIndent();
3938
})
40-
.on(RUNNER_EVENT_PASS, test => {
39+
.on(EVENT_TEST_PASS, test => {
4140
// Test#fullTitle() returns the suite name(s)
4241
// prepended to the test title
4342
console.log(`${this.indent()}pass: ${test.fullTitle()}`);
4443
})
45-
.on(RUNNER_EVENT_FAIL, (test, err) => {
44+
.on(EVENT_TEST_FAIL, (test, err) => {
4645
console.log(
4746
`${this.indent()}fail: ${test.fullTitle()} - error: ${err.message}`
4847
);
4948
})
50-
.once(RUNNER_EVENT_END, () => {
51-
// Base reporter has a `stats` property
49+
.once(EVENT_RUN_END, () => {
5250
console.log(
5351
`end: ${this.stats.passes}/${this.stats.passes +
5452
this.stats.failures} ok`
@@ -78,33 +76,46 @@ To use this reporter, execute `mocha --reporter /path/to/my-reporter.js`.
7876

7977
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.
8078

79+
## The `Base` Reporter Class
80+
81+
[Base] is an "abstract" class. It contains console-specific settings and utilities, commonly used by built-in reporters. Browsing the built-in reporter implementations, you may often see static properties of [Base] referenced.
82+
83+
It's often useful--but not necessary--for a custom reporter to extend [Base].
84+
85+
## Statistics
86+
87+
Mocha adds a `stats` property of type [StatsCollector](/api/module-lib_stats-collector.html) to each reporter instance immediately following instantiation.
88+
8189
## Events
8290

83-
A reporter should listen for events emitted from the `runner` (an instance of [Runner]).
91+
A reporter should listen for events emitted from the `runner` (a singleton instance of [Runner]).
8492

8593
The event names are exported from the `constants` property of `Mocha.Runner`:
8694

87-
| Constant | Event Name | Event Arguments | Description |
88-
| -------------------------- | ----------- | --------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
89-
| `RUNNER_EVENT_BEGIN` | `start` | _(n/a)_ | Execution will begin. Corresponding "end" event is `RUNNER_EVENT_END`. |
90-
| `RUNNER_EVENT_END` | `end` | _(n/a)_ | All [Suite]s, [Test]s and [Hook]s have completed execution. Corresponding "begin" event is `RUNNER_EVENT_BEGIN`. |
91-
| `RUNNER_EVENT_SUITE_BEGIN` | `suite` | `Suite` | The [Hook]s and [Test]s within a [Suite] are about to be executed. Any nested [Suite]s will also be executed. Corresponding "end" event is `RUNNER_EVENT_SUITE_END`. |
92-
| `RUNNER_EVENT_SUITE_END` | `suite end` | `Suite` | The [Hook]s, [Test]s, and nested [Suite]s within a [Suite] have completed execution. Corresponding "begin" event is `RUNNER_EVENT_SUITE_BEGIN`. |
93-
| `RUNNER_EVENT_HOOK_BEGIN` | `hook` | `Hook` | A [Hook] will execute. Corresponding "end" event is `RUNNER_EVENT_HOOK_END`. |
94-
| `RUNNER_EVENT_HOOK_END` | `hook end` | `Hook` | A [Hook] has completed execution. Corresponding "begin" event is `RUNNER_EVENT_HOOK_BEGIN`. |
95-
| `RUNNER_EVENT_TEST_BEGIN` | `test` | `Test` | A [Test] will execute. Corresponding "end" event is `RUNNER_EVENT_TEST_END`. |
96-
| `RUNNER_EVENT_TEST_END` | `test end` | `Test` | A [Test] has completed execution. Corresponding "begin" event is `RUNNER_EVENT_TEST_BEGIN`. |
97-
| `RUNNER_EVENT_FAIL` | `fail` | `Test`, `Error` | A [Test] has failed or thrown an exception. |
98-
| `RUNNER_EVENT_PASS` | `pass` | `Test` | A [Test] has passed. |
99-
| `RUNNER_EVENT_PENDING` | `pending` | `Test` | A [Test] was skipped. |
100-
| `RUNNER_EVENT_RETRY` | `retry` | `Test`, `Error` | A [Test] failed, but is about to be retried; only emitted if the `retry` option is nonzero. |
101-
| `RUNNER_EVENT_WAITING` | `waiting` | _(n/a)_ | Waiting for `global.run()` to be called; only emitted when `delay` option is `true`. |
95+
| Constant | Event Name | Event Arguments | Description |
96+
| -------------------- | ----------- | --------------- | ------------------------------------------------------------------------------------------- |
97+
| `EVENT_RUN_BEGIN` | `start` | _(n/a)_ | Execution will begin. |
98+
| `EVENT_RUN_END` | `end` | _(n/a)_ | All [Suite]s, [Test]s and [Hook]s have completed execution. |
99+
| `EVENT_DELAY_BEGIN` | `waiting` | _(n/a)_ | Waiting for `global.run()` to be called; only emitted when [delay] option is `true`. |
100+
| `EVENT_DELAY_END` | `ready` | _(n/a)_ | User called `global.run()` and the root suite is ready to execute. |
101+
| `EVENT_SUITE_BEGIN` | `suite` | `Suite` | The [Hook]s and [Test]s within a [Suite] will be executed, including any nested [Suite]s. |
102+
| `EVENT_SUITE_END` | `suite end` | `Suite` | The [Hook]s, [Test]s, and nested [Suite]s within a [Suite] have completed execution. |
103+
| `EVENT_HOOK_BEGIN` | `hook` | `Hook` | A [Hook] will be executed. |
104+
| `EVENT_HOOK_END` | `hook end` | `Hook` | A [Hook] has completed execution. |
105+
| `EVENT_TEST_BEGIN` | `test` | `Test` | A [Test] will be executed. |
106+
| `EVENT_TEST_END` | `test end` | `Test` | A [Test] has completed execution. |
107+
| `EVENT_TEST_FAIL` | `fail` | `Test`, `Error` | A [Test] has failed or thrown an exception. |
108+
| `EVENT_TEST_PASS` | `pass` | `Test` | A [Test] has passed. |
109+
| `EVENT_TEST_PENDING` | `pending` | `Test` | A [Test] was skipped. |
110+
| `EVENT_TEST_RETRY` | `retry` | `Test`, `Error` | A [Test] failed, but is about to be retried; only emitted if the `retry` option is nonzero. |
102111

103112
**Please use these constants** instead of the event names in your own reporter! This will ensure compatibility with future versions of Mocha.
104113

105-
> It's important to understand that all suite callbacks will be run _before_ the [Runner] emits `RUNNER_EVENT_BEGIN`. Hooks and tests, however, won't run until _after_ the [Runner] emits `RUNNER_EVENT_BEGIN`.
114+
> It's important to understand that all `Suite` callbacks will be run _before_ the [Runner] emits `EVENT_RUN_BEGIN`. Hooks and tests, however, won't run until _after_ the [Runner] emits `EVENT_RUN_BEGIN`.
106115
107116
[runner]: /api/mocha.runner
108117
[test]: /api/mocha.test
109118
[hook]: /api/mocha.hook
110119
[suite]: /api/mocha.suite
120+
[base]: /api/mocha.reporters.base
121+
[delay]: /#delayed-root-suite

lib/browser/growl.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*/
1111
var Date = global.Date;
1212
var setTimeout = global.setTimeout;
13-
var RUNNER_EVENT_END = require('../runner').constants.RUNNER_EVENT_END;
13+
var EVENT_RUN_END = require('../runner').constants.EVENT_RUN_END;
1414

1515
/**
1616
* Checks if browser notification support exists.
@@ -54,7 +54,7 @@ exports.notify = function(runner) {
5454
.catch(notPermitted);
5555
};
5656

57-
runner.once(RUNNER_EVENT_END, sendNotification);
57+
runner.once(EVENT_RUN_END, sendNotification);
5858
};
5959

6060
/**

lib/growl.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
const os = require('os');
99
const path = require('path');
1010
const {sync: which} = require('which');
11-
const {RUNNER_EVENT_END} = require('./runner').constants;
11+
const {EVENT_RUN_END} = require('./runner').constants;
1212

1313
/**
1414
* @summary
@@ -42,7 +42,7 @@ exports.isCapable = () => {
4242
* @param {Runner} runner - Runner instance.
4343
*/
4444
exports.notify = runner => {
45-
runner.once(RUNNER_EVENT_END, () => {
45+
runner.once(EVENT_RUN_END, () => {
4646
display(runner);
4747
});
4848
};

lib/interfaces/bdd.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use strict';
22

33
var Test = require('../test');
4-
var Suite = require('../suite');
4+
var EVENT_FILE_PRE_REQUIRE = require('../suite').constants
5+
.EVENT_FILE_PRE_REQUIRE;
56

67
/**
78
* BDD-style interface:
@@ -23,11 +24,7 @@ var Suite = require('../suite');
2324
module.exports = function bddInterface(suite) {
2425
var suites = [suite];
2526

26-
suite.on(Suite.constants.EVENT_FILE_PRE_REQUIRE, function(
27-
context,
28-
file,
29-
mocha
30-
) {
27+
suite.on(EVENT_FILE_PRE_REQUIRE, function(context, file, mocha) {
3128
var common = require('./common')(suites, context, mocha);
3229

3330
context.before = common.before;

lib/interfaces/qunit.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use strict';
22

33
var Test = require('../test');
4-
var Suite = require('../suite');
4+
var EVENT_FILE_PRE_REQUIRE = require('../suite').constants
5+
.EVENT_FILE_PRE_REQUIRE;
56

67
/**
78
* QUnit-style interface:
@@ -31,11 +32,7 @@ var Suite = require('../suite');
3132
module.exports = function qUnitInterface(suite) {
3233
var suites = [suite];
3334

34-
suite.on(Suite.constants.EVENT_FILE_PRE_REQUIRE, function(
35-
context,
36-
file,
37-
mocha
38-
) {
35+
suite.on(EVENT_FILE_PRE_REQUIRE, function(context, file, mocha) {
3936
var common = require('./common')(suites, context, mocha);
4037

4138
context.before = common.before;

lib/interfaces/tdd.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use strict';
22

33
var Test = require('../test');
4-
var Suite = require('../suite');
4+
var EVENT_FILE_PRE_REQUIRE = require('../suite').constants
5+
.EVENT_FILE_PRE_REQUIRE;
56

67
/**
78
* TDD-style interface:
@@ -31,11 +32,7 @@ var Suite = require('../suite');
3132
module.exports = function(suite) {
3233
var suites = [suite];
3334

34-
suite.on(Suite.constants.EVENT_FILE_PRE_REQUIRE, function(
35-
context,
36-
file,
37-
mocha
38-
) {
35+
suite.on(EVENT_FILE_PRE_REQUIRE, function(context, file, mocha) {
3936
var common = require('./common')(suites, context, mocha);
4037

4138
context.setup = common.beforeEach;

lib/mocha.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ var builtinReporters = require('./reporters');
1212
var growl = require('./growl');
1313
var utils = require('./utils');
1414
var mocharc = require('./mocharc.json');
15-
var assign = require('object.assign').getPolyfill();
1615
var errors = require('./errors');
1716
var Suite = require('./suite');
1817
var createStatsCollector = require('./stats-collector');
1918
var createInvalidReporterError = errors.createInvalidReporterError;
2019
var createInvalidInterfaceError = errors.createInvalidInterfaceError;
20+
var EVENT_FILE_PRE_REQUIRE = Suite.constants.EVENT_FILE_PRE_REQUIRE;
21+
var EVENT_FILE_POST_REQUIRE = Suite.constants.EVENT_FILE_POST_REQUIRE;
22+
var EVENT_FILE_REQUIRE = Suite.constants.EVENT_FILE_REQUIRE;
2123

2224
exports = module.exports = Mocha;
2325

@@ -90,7 +92,7 @@ exports.Test = require('./test');
9092
* @param {boolean} [options.useInlineDiffs] - Use inline diffs?
9193
*/
9294
function Mocha(options) {
93-
options = assign({}, mocharc, options || {});
95+
options = utils.assign({}, mocharc, options || {});
9496
this.files = [];
9597
this.options = options;
9698
// root suite
@@ -278,7 +280,7 @@ Mocha.prototype.ui = function(name) {
278280
}
279281
this._ui = this._ui(this.suite);
280282

281-
this.suite.on(Suite.constants.EVENT_FILE_PRE_REQUIRE, function(context) {
283+
this.suite.on(EVENT_FILE_PRE_REQUIRE, function(context) {
282284
exports.afterEach = context.afterEach || context.teardown;
283285
exports.after = context.after || context.suiteTeardown;
284286
exports.beforeEach = context.beforeEach || context.setup;
@@ -315,9 +317,9 @@ Mocha.prototype.loadFiles = function(fn) {
315317
var suite = this.suite;
316318
this.files.forEach(function(file) {
317319
file = path.resolve(file);
318-
suite.emit(Suite.constants.EVENT_FILE_PRE_REQUIRE, global, file, self);
319-
suite.emit(Suite.constants.EVENT_FILE_REQUIRE, require(file), file, self);
320-
suite.emit(Suite.constants.EVENT_FILE_POST_REQUIRE, global, file, self);
320+
suite.emit(EVENT_FILE_PRE_REQUIRE, global, file, self);
321+
suite.emit(EVENT_FILE_REQUIRE, require(file), file, self);
322+
suite.emit(EVENT_FILE_POST_REQUIRE, global, file, self);
321323
});
322324
fn && fn();
323325
};

lib/reporters/base.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ var milliseconds = require('ms');
1212
var utils = require('../utils');
1313
var supportsColor = process.browser ? null : require('supports-color');
1414
var constants = require('../runner').constants;
15-
var RUNNER_EVENT_PASS = constants.RUNNER_EVENT_PASS;
16-
var RUNNER_EVENT_FAIL = constants.RUNNER_EVENT_FAIL;
15+
var EVENT_TEST_PASS = constants.EVENT_TEST_PASS;
16+
var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL;
1717

1818
/**
1919
* Expose `Base`.
@@ -277,7 +277,7 @@ function Base(runner) {
277277
this.stats = runner.stats; // assigned so Reporters keep a closer reference
278278
this.runner = runner;
279279

280-
runner.on(RUNNER_EVENT_PASS, function(test) {
280+
runner.on(EVENT_TEST_PASS, function(test) {
281281
if (test.duration > test.slow()) {
282282
test.speed = 'slow';
283283
} else if (test.duration > test.slow() / 2) {
@@ -287,7 +287,7 @@ function Base(runner) {
287287
}
288288
});
289289

290-
runner.on(RUNNER_EVENT_FAIL, function(test, err) {
290+
runner.on(EVENT_TEST_FAIL, function(test, err) {
291291
if (showDiff(err)) {
292292
stringifyDiffObjs(err);
293293
}

lib/reporters/doc.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
var Base = require('./base');
1010
var utils = require('../utils');
1111
var constants = require('../runner').constants;
12-
var RUNNER_EVENT_PASS = constants.RUNNER_EVENT_PASS;
13-
var RUNNER_EVENT_FAIL = constants.RUNNER_EVENT_FAIL;
14-
var RUNNER_EVENT_SUITE_BEGIN = constants.RUNNER_EVENT_SUITE_BEGIN;
15-
var RUNNER_EVENT_SUITE_END = constants.RUNNER_EVENT_SUITE_END;
12+
var EVENT_TEST_PASS = constants.EVENT_TEST_PASS;
13+
var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL;
14+
var EVENT_SUITE_BEGIN = constants.EVENT_SUITE_BEGIN;
15+
var EVENT_SUITE_END = constants.EVENT_SUITE_END;
1616

1717
/**
1818
* Expose `Doc`.
@@ -38,7 +38,7 @@ function Doc(runner) {
3838
return Array(indents).join(' ');
3939
}
4040

41-
runner.on(RUNNER_EVENT_SUITE_BEGIN, function(suite) {
41+
runner.on(EVENT_SUITE_BEGIN, function(suite) {
4242
if (suite.root) {
4343
return;
4444
}
@@ -49,7 +49,7 @@ function Doc(runner) {
4949
console.log('%s<dl>', indent());
5050
});
5151

52-
runner.on(RUNNER_EVENT_SUITE_END, function(suite) {
52+
runner.on(EVENT_SUITE_END, function(suite) {
5353
if (suite.root) {
5454
return;
5555
}
@@ -59,13 +59,13 @@ function Doc(runner) {
5959
--indents;
6060
});
6161

62-
runner.on(RUNNER_EVENT_PASS, function(test) {
62+
runner.on(EVENT_TEST_PASS, function(test) {
6363
console.log('%s <dt>%s</dt>', indent(), utils.escape(test.title));
6464
var code = utils.escape(utils.clean(test.body));
6565
console.log('%s <dd><pre><code>%s</code></pre></dd>', indent(), code);
6666
});
6767

68-
runner.on(RUNNER_EVENT_FAIL, function(test, err) {
68+
runner.on(EVENT_TEST_FAIL, function(test, err) {
6969
console.log(
7070
'%s <dt class="error">%s</dt>',
7171
indent(),

0 commit comments

Comments
 (0)