Skip to content

Commit

Permalink
refactor: use constants for event names instead of string literals
Browse files Browse the repository at this point in the history
- also a few other string literals got replaced, but not error messages nor codes
- a couple adjacent refactors:
  - move the suite "GC" function into the `Suite` prototype
  - move stats collector init to `Mocha#run` due to circular reference with `Runner`
  - rename a couple fixture files lacking proper extension
  - rename variable in integration test helpers
- specifically did NOT refactor event names from Node.js incl. `error` and `uncaughtException`
  • Loading branch information
boneskull committed Jan 4, 2019
1 parent cb1c6b0 commit f55b965
Show file tree
Hide file tree
Showing 34 changed files with 354 additions and 233 deletions.
3 changes: 2 additions & 1 deletion lib/browser/growl.js
Expand Up @@ -10,6 +10,7 @@
*/
var Date = global.Date;
var setTimeout = global.setTimeout;
var Runner = require('../runner');

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

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

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

/**
* @summary
Expand Down Expand Up @@ -41,7 +42,7 @@ exports.isCapable = () => {
* @param {Runner} runner - Runner instance.
*/
exports.notify = runner => {
runner.once('end', () => {
runner.once(Runner.constants.RUNNER_EVENT_END, () => {
display(runner);
});
};
Expand Down
7 changes: 6 additions & 1 deletion lib/interfaces/bdd.js
@@ -1,6 +1,7 @@
'use strict';

var Test = require('../test');
var Suite = require('../suite');

/**
* BDD-style interface:
Expand All @@ -22,7 +23,11 @@ var Test = require('../test');
module.exports = function bddInterface(suite) {
var suites = [suite];

suite.on('pre-require', function(context, file, mocha) {
suite.on(Suite.constants.SUITE_EVENT_PRE_REQUIRE, function(
context,
file,
mocha
) {
var common = require('./common')(suites, context, mocha);

context.before = common.before;
Expand Down
2 changes: 1 addition & 1 deletion lib/interfaces/exports.js
Expand Up @@ -22,7 +22,7 @@ var Test = require('../test');
module.exports = function(suite) {
var suites = [suite];

suite.on('require', visit);
suite.on(Suite.constants.SUITE_EVENT_REQUIRE, visit);

function visit(obj, file) {
var suite;
Expand Down
7 changes: 6 additions & 1 deletion lib/interfaces/qunit.js
@@ -1,6 +1,7 @@
'use strict';

var Test = require('../test');
var Suite = require('../suite');

/**
* QUnit-style interface:
Expand Down Expand Up @@ -30,7 +31,11 @@ var Test = require('../test');
module.exports = function qUnitInterface(suite) {
var suites = [suite];

suite.on('pre-require', function(context, file, mocha) {
suite.on(Suite.constants.SUITE_EVENT_PRE_REQUIRE, function(
context,
file,
mocha
) {
var common = require('./common')(suites, context, mocha);

context.before = common.before;
Expand Down
7 changes: 6 additions & 1 deletion lib/interfaces/tdd.js
@@ -1,6 +1,7 @@
'use strict';

var Test = require('../test');
var Suite = require('../suite');

/**
* TDD-style interface:
Expand Down Expand Up @@ -30,7 +31,11 @@ var Test = require('../test');
module.exports = function(suite) {
var suites = [suite];

suite.on('pre-require', function(context, file, mocha) {
suite.on(Suite.constants.SUITE_EVENT_PRE_REQUIRE, function(
context,
file,
mocha
) {
var common = require('./common')(suites, context, mocha);

context.setup = common.beforeEach;
Expand Down
13 changes: 8 additions & 5 deletions lib/mocha.js
Expand Up @@ -14,6 +14,8 @@ var utils = require('./utils');
var mocharc = require('./mocharc.json');
var assign = require('object.assign').getPolyfill();
var errors = require('./errors');
var Suite = require('./suite');
var createStatsCollector = require('./stats-collector');
var createInvalidReporterError = errors.createInvalidReporterError;
var createInvalidInterfaceError = errors.createInvalidInterfaceError;

Expand Down Expand Up @@ -51,7 +53,7 @@ exports.Context = require('./context');
* @memberof Mocha
*/
exports.Runner = require('./runner');
exports.Suite = require('./suite');
exports.Suite = Suite;
exports.Hook = require('./hook');
exports.Test = require('./test');

Expand Down Expand Up @@ -276,7 +278,7 @@ Mocha.prototype.ui = function(name) {
}
this._ui = this._ui(this.suite);

this.suite.on('pre-require', function(context) {
this.suite.on(Suite.constants.SUITE_EVENT_PRE_REQUIRE, function(context) {
exports.afterEach = context.afterEach || context.teardown;
exports.after = context.after || context.suiteTeardown;
exports.beforeEach = context.beforeEach || context.setup;
Expand Down Expand Up @@ -313,9 +315,9 @@ Mocha.prototype.loadFiles = function(fn) {
var suite = this.suite;
this.files.forEach(function(file) {
file = path.resolve(file);
suite.emit('pre-require', global, file, self);
suite.emit('require', require(file), file, self);
suite.emit('post-require', global, file, self);
suite.emit(Suite.constants.SUITE_EVENT_PRE_REQUIRE, global, file, self);
suite.emit(Suite.constants.SUITE_EVENT_REQUIRE, require(file), file, self);
suite.emit(Suite.constants.SUITE_EVENT_POST_REQUIRE, global, file, self);
});
fn && fn();
};
Expand Down Expand Up @@ -759,6 +761,7 @@ Mocha.prototype.run = function(fn) {
var options = this.options;
options.files = this.files;
var runner = new exports.Runner(suite, options.delay);
createStatsCollector(runner);
var reporter = new this._reporter(runner, options);
runner.ignoreLeaks = options.ignoreLeaks !== false;
runner.fullStackTrace = options.fullStackTrace;
Expand Down
5 changes: 3 additions & 2 deletions lib/reporters/base.js
Expand Up @@ -11,6 +11,7 @@ var diff = require('diff');
var milliseconds = require('ms');
var utils = require('../utils');
var supportsColor = process.browser ? null : require('supports-color');
var Runner = require('../runner');

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

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

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

var Base = require('./base');
var utils = require('../utils');
var Runner = require('../runner');

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

runner.on('suite', function(suite) {
runner.on(Runner.constants.RUNNER_EVENT_SUITE, function(suite) {
if (suite.root) {
return;
}
Expand All @@ -54,13 +55,13 @@ function Doc(runner) {
--indents;
});

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

var Base = require('./base');
var inherits = require('../utils').inherits;
var Runner = require('../runner');

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

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

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

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

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

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

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

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

runner.on('pass', function(test) {
runner.on(Runner.constants.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 @@ -152,7 +153,7 @@ function HTML(runner) {
updateStats();
});

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

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

var Base = require('./base');
var Runner = require('../runner');

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

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

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

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

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

var Base = require('./base');
var Runner = require('../runner');

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

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

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

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

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

runner.once('end', function() {
runner.once(Runner.constants.RUNNER_EVENT_END, function() {
var obj = {
stats: self.stats,
tests: tests.map(clean),
Expand Down

0 comments on commit f55b965

Please sign in to comment.