Skip to content

Commit

Permalink
peer review: dereference constants where applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
boneskull committed Jan 10, 2019
1 parent c0d1d92 commit 6297486
Show file tree
Hide file tree
Showing 24 changed files with 226 additions and 157 deletions.
6 changes: 3 additions & 3 deletions docs/api-tutorials/custom-reporter.md
Expand Up @@ -27,7 +27,7 @@ class MyReporter extends Base {
this._indents = 0;

runner
.once(RUNNER_EVENT_START, () => {
.once(RUNNER_EVENT_BEGIN, () => {
console.log('start');
})
.on(RUNNER_EVENT_SUITE, () => {
Expand Down Expand Up @@ -92,7 +92,7 @@ The event names are exported from the `constants` property of `Mocha.Runner`:
| `RUNNER_EVENT_PASS` | `pass` | `Test` | A [Test] has passed |
| `RUNNER_EVENT_PENDING` | `pending` | `Test` | A [Test] was skipped |
| `RUNNER_EVENT_RETRY` | `retry` | `Test`, `Error` | A [Test] failed, but is about to be retried; never emitted unless `retry` option is nonzero |
| `RUNNER_EVENT_START` | `start` | _(n/a)_ | Execution will begin |
| `RUNNER_EVENT_BEGIN` | `start` | _(n/a)_ | Execution will begin |
| `RUNNER_EVENT_SUITE` | `suite` | `Suite` | The [Hook]s and [Test]s within a [Suite] are about to be executed |
| `RUNNER_EVENT_SUITE_END` | `suite end` | `Suite` | The [Hook]s and [Test]s within a [Suite] (and any children [Suite]s) completed execution |
| `RUNNER_EVENT_TEST` | `test` | `Test` | A [Test] is about to be executed |
Expand All @@ -101,7 +101,7 @@ The event names are exported from the `constants` property of `Mocha.Runner`:

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

> 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`.
> 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`.
## Custom Reporter for Browser

Expand Down
4 changes: 2 additions & 2 deletions lib/browser/growl.js
Expand Up @@ -10,7 +10,7 @@
*/
var Date = global.Date;
var setTimeout = global.setTimeout;
var Runner = require('../runner');
var RUNNER_EVENT_END = require('../runner').constants.RUNNER_EVENT_END;

/**
* Checks if browser notification support exists.
Expand Down Expand Up @@ -54,7 +54,7 @@ exports.notify = function(runner) {
.catch(notPermitted);
};

runner.once(Runner.constants.RUNNER_EVENT_END, sendNotification);
runner.once(RUNNER_EVENT_END, sendNotification);
};

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/growl.js
Expand Up @@ -8,7 +8,7 @@
const os = require('os');
const path = require('path');
const {sync: which} = require('which');
const Runner = require('./runner');
const {RUNNER_EVENT_END} = require('./runner').constants;

/**
* @summary
Expand Down Expand Up @@ -42,7 +42,7 @@ exports.isCapable = () => {
* @param {Runner} runner - Runner instance.
*/
exports.notify = runner => {
runner.once(Runner.constants.RUNNER_EVENT_END, () => {
runner.once(RUNNER_EVENT_END, () => {
display(runner);
});
};
Expand Down
8 changes: 5 additions & 3 deletions lib/reporters/base.js
Expand Up @@ -11,7 +11,9 @@ var diff = require('diff');
var milliseconds = require('ms');
var utils = require('../utils');
var supportsColor = process.browser ? null : require('supports-color');
var Runner = require('../runner');
var constants = require('../runner').constants;
var RUNNER_EVENT_PASS = constants.RUNNER_EVENT_PASS;
var RUNNER_EVENT_FAIL = constants.RUNNER_EVENT_FAIL;

/**
* Expose `Base`.
Expand Down Expand Up @@ -275,7 +277,7 @@ function Base(runner) {
this.stats = runner.stats; // assigned so Reporters keep a closer reference
this.runner = runner;

runner.on(Runner.constants.RUNNER_EVENT_PASS, function(test) {
runner.on(RUNNER_EVENT_PASS, function(test) {
if (test.duration > test.slow()) {
test.speed = 'slow';
} else if (test.duration > test.slow() / 2) {
Expand All @@ -285,7 +287,7 @@ function Base(runner) {
}
});

runner.on(Runner.constants.RUNNER_EVENT_FAIL, function(test, err) {
runner.on(RUNNER_EVENT_FAIL, function(test, err) {
if (showDiff(err)) {
stringifyDiffObjs(err);
}
Expand Down
14 changes: 9 additions & 5 deletions lib/reporters/doc.js
Expand Up @@ -8,7 +8,11 @@

var Base = require('./base');
var utils = require('../utils');
var Runner = require('../runner');
var constants = require('../runner').constants;
var RUNNER_EVENT_PASS = constants.RUNNER_EVENT_PASS;
var RUNNER_EVENT_FAIL = constants.RUNNER_EVENT_FAIL;
var RUNNER_EVENT_SUITE = constants.RUNNER_EVENT_SUITE;
var RUNNER_EVENT_SUITE_END = constants.RUNNER_EVENT_SUITE_END;

/**
* Expose `Doc`.
Expand All @@ -34,7 +38,7 @@ function Doc(runner) {
return Array(indents).join(' ');
}

runner.on(Runner.constants.RUNNER_EVENT_SUITE, function(suite) {
runner.on(RUNNER_EVENT_SUITE, function(suite) {
if (suite.root) {
return;
}
Expand All @@ -45,7 +49,7 @@ function Doc(runner) {
console.log('%s<dl>', indent());
});

runner.on('suite end', function(suite) {
runner.on(RUNNER_EVENT_SUITE_END, function(suite) {
if (suite.root) {
return;
}
Expand All @@ -55,13 +59,13 @@ function Doc(runner) {
--indents;
});

runner.on(Runner.constants.RUNNER_EVENT_PASS, function(test) {
runner.on(RUNNER_EVENT_PASS, function(test) {
console.log('%s <dt>%s</dt>', indent(), utils.escape(test.title));
var code = utils.escape(utils.clean(test.body));
console.log('%s <dd><pre><code>%s</code></pre></dd>', indent(), code);
});

runner.on(Runner.constants.RUNNER_EVENT_FAIL, function(test, err) {
runner.on(RUNNER_EVENT_FAIL, function(test, err) {
console.log(
'%s <dt class="error">%s</dt>',
indent(),
Expand Down
17 changes: 11 additions & 6 deletions lib/reporters/dot.js
Expand Up @@ -8,7 +8,12 @@

var Base = require('./base');
var inherits = require('../utils').inherits;
var Runner = require('../runner');
var constants = require('../runner').constants;
var RUNNER_EVENT_PASS = constants.RUNNER_EVENT_PASS;
var RUNNER_EVENT_FAIL = constants.RUNNER_EVENT_FAIL;
var RUNNER_EVENT_BEGIN = constants.RUNNER_EVENT_BEGIN;
var RUNNER_EVENT_PENDING = constants.RUNNER_EVENT_PENDING;
var RUNNER_EVENT_END = constants.RUNNER_EVENT_END;

/**
* Expose `Dot`.
Expand All @@ -32,18 +37,18 @@ function Dot(runner) {
var width = (Base.window.width * 0.75) | 0;
var n = -1;

runner.on(Runner.constants.RUNNER_EVENT_START, function() {
runner.on(RUNNER_EVENT_BEGIN, function() {
process.stdout.write('\n');
});

runner.on(Runner.constants.RUNNER_EVENT_PENDING, function() {
runner.on(RUNNER_EVENT_PENDING, function() {
if (++n % width === 0) {
process.stdout.write('\n ');
}
process.stdout.write(Base.color('pending', Base.symbols.comma));
});

runner.on(Runner.constants.RUNNER_EVENT_PASS, function(test) {
runner.on(RUNNER_EVENT_PASS, function(test) {
if (++n % width === 0) {
process.stdout.write('\n ');
}
Expand All @@ -54,14 +59,14 @@ function Dot(runner) {
}
});

runner.on(Runner.constants.RUNNER_EVENT_FAIL, function() {
runner.on(RUNNER_EVENT_FAIL, function() {
if (++n % width === 0) {
process.stdout.write('\n ');
}
process.stdout.write(Base.color('fail', Base.symbols.bang));
});

runner.once(Runner.constants.RUNNER_EVENT_END, function() {
runner.once(RUNNER_EVENT_END, function() {
console.log();
self.epilogue();
});
Expand Down
14 changes: 9 additions & 5 deletions lib/reporters/html.js
Expand Up @@ -12,7 +12,11 @@ var Base = require('./base');
var utils = require('../utils');
var Progress = require('../browser/progress');
var escapeRe = require('escape-string-regexp');
var Runner = require('../runner');
var constants = require('../runner').constants;
var RUNNER_EVENT_PASS = constants.RUNNER_EVENT_PASS;
var RUNNER_EVENT_FAIL = constants.RUNNER_EVENT_FAIL;
var RUNNER_EVENT_SUITE = constants.RUNNER_EVENT_SUITE;
var RUNNER_EVENT_PENDING = constants.RUNNER_EVENT_PENDING;
var escape = utils.escape;

/**
Expand Down Expand Up @@ -113,7 +117,7 @@ function HTML(runner) {
progress.size(40);
}

runner.on(Runner.constants.RUNNER_EVENT_SUITE, function(suite) {
runner.on(RUNNER_EVENT_SUITE, function(suite) {
if (suite.root) {
return;
}
Expand All @@ -140,7 +144,7 @@ function HTML(runner) {
stack.shift();
});

runner.on(Runner.constants.RUNNER_EVENT_PASS, function(test) {
runner.on(RUNNER_EVENT_PASS, function(test) {
var url = self.testURL(test);
var markup =
'<li class="test pass %e"><h2>%e<span class="duration">%ems</span> ' +
Expand All @@ -153,7 +157,7 @@ function HTML(runner) {
updateStats();
});

runner.on(Runner.constants.RUNNER_EVENT_FAIL, function(test) {
runner.on(RUNNER_EVENT_FAIL, function(test) {
var el = fragment(
'<li class="test fail"><h2>%e <a href="%e" class="replay">' +
playIcon +
Expand Down Expand Up @@ -209,7 +213,7 @@ function HTML(runner) {
updateStats();
});

runner.on(Runner.constants.RUNNER_EVENT_PENDING, function(test) {
runner.on(RUNNER_EVENT_PENDING, function(test) {
var el = fragment(
'<li class="test pass pending"><h2>%e</h2></li>',
test.title
Expand Down
14 changes: 9 additions & 5 deletions lib/reporters/json-stream.js
Expand Up @@ -7,7 +7,11 @@
*/

var Base = require('./base');
var Runner = require('../runner');
var constants = require('../runner').constants;
var RUNNER_EVENT_PASS = constants.RUNNER_EVENT_PASS;
var RUNNER_EVENT_FAIL = constants.RUNNER_EVENT_FAIL;
var RUNNER_EVENT_BEGIN = constants.RUNNER_EVENT_BEGIN;
var RUNNER_EVENT_END = constants.RUNNER_EVENT_END;

/**
* Expose `JSONStream`.
Expand All @@ -30,22 +34,22 @@ function JSONStream(runner) {
var self = this;
var total = runner.total;

runner.once(Runner.constants.RUNNER_EVENT_START, function() {
runner.once(RUNNER_EVENT_BEGIN, function() {
writeEvent(['start', {total: total}]);
});

runner.on(Runner.constants.RUNNER_EVENT_PASS, function(test) {
runner.on(RUNNER_EVENT_PASS, function(test) {
writeEvent(['pass', clean(test)]);
});

runner.on(Runner.constants.RUNNER_EVENT_FAIL, function(test, err) {
runner.on(RUNNER_EVENT_FAIL, function(test, err) {
test = clean(test);
test.err = err.message;
test.stack = err.stack || null;
writeEvent(['fail', test]);
});

runner.once(Runner.constants.RUNNER_EVENT_END, function() {
runner.once(RUNNER_EVENT_END, function() {
writeEvent(['end', self.stats]);
});
}
Expand Down
17 changes: 11 additions & 6 deletions lib/reporters/json.js
Expand Up @@ -7,7 +7,12 @@
*/

var Base = require('./base');
var Runner = require('../runner');
var constants = require('../runner').constants;
var RUNNER_EVENT_PASS = constants.RUNNER_EVENT_PASS;
var RUNNER_EVENT_FAIL = constants.RUNNER_EVENT_FAIL;
var RUNNER_EVENT_TEST_END = constants.RUNNER_EVENT_TEST_END;
var RUNNER_EVENT_END = constants.RUNNER_EVENT_END;
var RUNNER_EVENT_PENDING = constants.RUNNER_EVENT_PENDING;

/**
* Expose `JSON`.
Expand All @@ -33,23 +38,23 @@ function JSONReporter(runner) {
var failures = [];
var passes = [];

runner.on(Runner.constants.RUNNER_EVENT_TEST_END, function(test) {
runner.on(RUNNER_EVENT_TEST_END, function(test) {
tests.push(test);
});

runner.on(Runner.constants.RUNNER_EVENT_PASS, function(test) {
runner.on(RUNNER_EVENT_PASS, function(test) {
passes.push(test);
});

runner.on(Runner.constants.RUNNER_EVENT_FAIL, function(test) {
runner.on(RUNNER_EVENT_FAIL, function(test) {
failures.push(test);
});

runner.on(Runner.constants.RUNNER_EVENT_PENDING, function(test) {
runner.on(RUNNER_EVENT_PENDING, function(test) {
pending.push(test);
});

runner.once(Runner.constants.RUNNER_EVENT_END, function() {
runner.once(RUNNER_EVENT_END, function() {
var obj = {
stats: self.stats,
tests: tests.map(clean),
Expand Down
12 changes: 8 additions & 4 deletions lib/reporters/landing.js
Expand Up @@ -7,8 +7,12 @@
*/

var Base = require('./base');
var Runner = require('../runner');
var inherits = require('../utils').inherits;
var constants = require('../runner').constants;
var RUNNER_EVENT_BEGIN = constants.RUNNER_EVENT_BEGIN;
var RUNNER_EVENT_END = constants.RUNNER_EVENT_END;
var RUNNER_EVENT_TEST_END = constants.RUNNER_EVENT_TEST_END;

var cursor = Base.cursor;
var color = Base.color;

Expand Down Expand Up @@ -61,12 +65,12 @@ function Landing(runner) {
return ' ' + color('runway', buf);
}

runner.on(Runner.constants.RUNNER_EVENT_START, function() {
runner.on(RUNNER_EVENT_BEGIN, function() {
stream.write('\n\n\n ');
cursor.hide();
});

runner.on(Runner.constants.RUNNER_EVENT_TEST_END, function(test) {
runner.on(RUNNER_EVENT_TEST_END, function(test) {
// check if the plane crashed
var col = crashed === -1 ? ((width * ++n) / total) | 0 : crashed;

Expand All @@ -87,7 +91,7 @@ function Landing(runner) {
stream.write('\u001b[0m');
});

runner.once(Runner.constants.RUNNER_EVENT_END, function() {
runner.once(RUNNER_EVENT_END, function() {
cursor.show();
console.log();
self.epilogue();
Expand Down

0 comments on commit 6297486

Please sign in to comment.