From f55b965e947aad1a2c5d8e985fe68c9ecb11c217 Mon Sep 17 00:00:00 2001 From: Christopher Hiller Date: Fri, 4 Jan 2019 15:55:18 -0800 Subject: [PATCH] refactor: use constants for event names instead of string literals - 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` --- lib/browser/growl.js | 3 +- lib/growl.js | 3 +- lib/interfaces/bdd.js | 7 +- lib/interfaces/exports.js | 2 +- lib/interfaces/qunit.js | 7 +- lib/interfaces/tdd.js | 7 +- lib/mocha.js | 13 +- lib/reporters/base.js | 5 +- lib/reporters/doc.js | 7 +- lib/reporters/dot.js | 11 +- lib/reporters/html.js | 9 +- lib/reporters/json-stream.js | 9 +- lib/reporters/json.js | 11 +- lib/reporters/landing.js | 7 +- lib/reporters/list.js | 11 +- lib/reporters/markdown.js | 7 +- lib/reporters/min.js | 5 +- lib/reporters/nyan.js | 11 +- lib/reporters/progress.js | 7 +- lib/reporters/spec.js | 13 +- lib/reporters/tap.js | 13 +- lib/reporters/xunit.js | 9 +- lib/runner.js | 166 +++++++++--------- lib/stats-collector.js | 16 +- lib/suite.js | 87 ++++++++- .../{simple-ui.js => simple-ui.fixture.js} | 17 +- .../fixtures/simple-reporter.fixture.js | 30 ++++ test/integration/fixtures/simple-reporter.js | 28 --- test/integration/helpers.js | 4 +- test/integration/regression.spec.js | 2 +- test/integration/reporters.spec.js | 4 +- test/jsapi/index.js | 3 +- test/unit/runner.spec.js | 35 ++-- test/unit/throw.spec.js | 18 +- 34 files changed, 354 insertions(+), 233 deletions(-) rename test/integration/fixtures/regression/1794/{simple-ui.js => simple-ui.fixture.js} (63%) create mode 100644 test/integration/fixtures/simple-reporter.fixture.js delete mode 100644 test/integration/fixtures/simple-reporter.js diff --git a/lib/browser/growl.js b/lib/browser/growl.js index a67648979c..d04f124c0e 100644 --- a/lib/browser/growl.js +++ b/lib/browser/growl.js @@ -10,6 +10,7 @@ */ var Date = global.Date; var setTimeout = global.setTimeout; +var Runner = require('../runner'); /** * Checks if browser notification support exists. @@ -53,7 +54,7 @@ exports.notify = function(runner) { .catch(notPermitted); }; - runner.once('end', sendNotification); + runner.once(Runner.constants.RUNNER_EVENT_END, sendNotification); }; /** diff --git a/lib/growl.js b/lib/growl.js index eae2281823..ba0e57f513 100644 --- a/lib/growl.js +++ b/lib/growl.js @@ -8,6 +8,7 @@ const os = require('os'); const path = require('path'); const {sync: which} = require('which'); +const Runner = require('./runner'); /** * @summary @@ -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); }); }; diff --git a/lib/interfaces/bdd.js b/lib/interfaces/bdd.js index 87408f8a8d..f091c20c91 100644 --- a/lib/interfaces/bdd.js +++ b/lib/interfaces/bdd.js @@ -1,6 +1,7 @@ 'use strict'; var Test = require('../test'); +var Suite = require('../suite'); /** * BDD-style interface: @@ -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; diff --git a/lib/interfaces/exports.js b/lib/interfaces/exports.js index da5e5d84d8..23fa813bd9 100644 --- a/lib/interfaces/exports.js +++ b/lib/interfaces/exports.js @@ -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; diff --git a/lib/interfaces/qunit.js b/lib/interfaces/qunit.js index becc110859..25e307cd42 100644 --- a/lib/interfaces/qunit.js +++ b/lib/interfaces/qunit.js @@ -1,6 +1,7 @@ 'use strict'; var Test = require('../test'); +var Suite = require('../suite'); /** * QUnit-style interface: @@ -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; diff --git a/lib/interfaces/tdd.js b/lib/interfaces/tdd.js index c36c7afc44..a4d7578de5 100644 --- a/lib/interfaces/tdd.js +++ b/lib/interfaces/tdd.js @@ -1,6 +1,7 @@ 'use strict'; var Test = require('../test'); +var Suite = require('../suite'); /** * TDD-style interface: @@ -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; diff --git a/lib/mocha.js b/lib/mocha.js index c703e09dee..9cec5ef5b3 100644 --- a/lib/mocha.js +++ b/lib/mocha.js @@ -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; @@ -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'); @@ -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; @@ -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(); }; @@ -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; diff --git a/lib/reporters/base.js b/lib/reporters/base.js index 16d4ca586e..5782ed95b4 100644 --- a/lib/reporters/base.js +++ b/lib/reporters/base.js @@ -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`. @@ -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) { @@ -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); } diff --git a/lib/reporters/doc.js b/lib/reporters/doc.js index ce183d6766..97f3e13b7a 100644 --- a/lib/reporters/doc.js +++ b/lib/reporters/doc.js @@ -8,6 +8,7 @@ var Base = require('./base'); var utils = require('../utils'); +var Runner = require('../runner'); /** * Expose `Doc`. @@ -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; } @@ -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
%s
', indent(), utils.escape(test.title)); var code = utils.escape(utils.clean(test.body)); console.log('%s
%s
', indent(), code); }); - runner.on('fail', function(test, err) { + runner.on(Runner.constants.RUNNER_EVENT_FAIL, function(test, err) { console.log( '%s
%s
', indent(), diff --git a/lib/reporters/dot.js b/lib/reporters/dot.js index 8479c31e6d..3fb9bc27c8 100644 --- a/lib/reporters/dot.js +++ b/lib/reporters/dot.js @@ -8,6 +8,7 @@ var Base = require('./base'); var inherits = require('../utils').inherits; +var Runner = require('../runner'); /** * Expose `Dot`. @@ -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 '); } @@ -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(); }); diff --git a/lib/reporters/html.js b/lib/reporters/html.js index 71bc21e113..efca0819c3 100644 --- a/lib/reporters/html.js +++ b/lib/reporters/html.js @@ -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; /** @@ -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; } @@ -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 = '
  • %e%ems ' + @@ -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( '
  • %e ' + playIcon + @@ -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( '
  • %e

  • ', test.title diff --git a/lib/reporters/json-stream.js b/lib/reporters/json-stream.js index c05050227b..f77dc1430e 100644 --- a/lib/reporters/json-stream.js +++ b/lib/reporters/json-stream.js @@ -7,6 +7,7 @@ */ var Base = require('./base'); +var Runner = require('../runner'); /** * Expose `JSONStream`. @@ -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]); }); } diff --git a/lib/reporters/json.js b/lib/reporters/json.js index c5106d3a65..d35c076c06 100644 --- a/lib/reporters/json.js +++ b/lib/reporters/json.js @@ -7,6 +7,7 @@ */ var Base = require('./base'); +var Runner = require('../runner'); /** * Expose `JSON`. @@ -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), diff --git a/lib/reporters/landing.js b/lib/reporters/landing.js index a33267adeb..b2f3f2df89 100644 --- a/lib/reporters/landing.js +++ b/lib/reporters/landing.js @@ -7,6 +7,7 @@ */ var Base = require('./base'); +var Runner = require('../runner'); var inherits = require('../utils').inherits; var cursor = Base.cursor; var color = Base.color; @@ -60,12 +61,12 @@ function Landing(runner) { return ' ' + color('runway', buf); } - runner.on('start', function() { + runner.on(Runner.constants.RUNNER_EVENT_START, function() { stream.write('\n\n\n '); cursor.hide(); }); - runner.on('test end', function(test) { + runner.on(Runner.constants.RUNNER_EVENT_TEST_END, function(test) { // check if the plane crashed var col = crashed === -1 ? ((width * ++n) / total) | 0 : crashed; @@ -86,7 +87,7 @@ function Landing(runner) { stream.write('\u001b[0m'); }); - runner.once('end', function() { + runner.once(Runner.constants.RUNNER_EVENT_END, function() { cursor.show(); console.log(); self.epilogue(); diff --git a/lib/reporters/list.js b/lib/reporters/list.js index 4e324b61c7..ba3bb678f5 100644 --- a/lib/reporters/list.js +++ b/lib/reporters/list.js @@ -7,6 +7,7 @@ */ var Base = require('./base'); +var Runner = require('../runner'); var inherits = require('../utils').inherits; var color = Base.color; var cursor = Base.cursor; @@ -32,7 +33,7 @@ function List(runner) { var self = this; var n = 0; - runner.on('start', function() { + runner.on(Runner.constants.RUNNER_EVENT_START, function() { console.log(); }); @@ -40,12 +41,12 @@ function List(runner) { process.stdout.write(color('pass', ' ' + test.fullTitle() + ': ')); }); - runner.on('pending', function(test) { + runner.on(Runner.constants.RUNNER_EVENT_PENDING, function(test) { var fmt = color('checkmark', ' -') + color('pending', ' %s'); console.log(fmt, test.fullTitle()); }); - runner.on('pass', function(test) { + runner.on(Runner.constants.RUNNER_EVENT_PASS, function(test) { var fmt = color('checkmark', ' ' + Base.symbols.ok) + color('pass', ' %s: ') + @@ -54,12 +55,12 @@ function List(runner) { console.log(fmt, test.fullTitle(), test.duration); }); - runner.on('fail', function(test) { + runner.on(Runner.constants.RUNNER_EVENT_FAIL, function(test) { cursor.CR(); console.log(color('fail', ' %d) %s'), ++n, test.fullTitle()); }); - runner.once('end', self.epilogue.bind(self)); + runner.once(Runner.constants.RUNNER_EVENT_END, self.epilogue.bind(self)); } /** diff --git a/lib/reporters/markdown.js b/lib/reporters/markdown.js index 70926ac617..a67f7cad43 100644 --- a/lib/reporters/markdown.js +++ b/lib/reporters/markdown.js @@ -8,6 +8,7 @@ var Base = require('./base'); var utils = require('../utils'); +var Runner = require('../runner'); /** * Constants @@ -77,7 +78,7 @@ function Markdown(runner) { generateTOC(runner.suite); - runner.on('suite', function(suite) { + runner.on(Runner.constants.RUNNER_EVENT_SUITE, function(suite) { ++level; var slug = utils.slug(suite.fullTitle()); buf += '
    ' + '\n'; @@ -88,7 +89,7 @@ function Markdown(runner) { --level; }); - runner.on('pass', function(test) { + runner.on(Runner.constants.RUNNER_EVENT_PASS, function(test) { var code = utils.clean(test.body); buf += test.title + '.\n'; buf += '\n```js\n'; @@ -96,7 +97,7 @@ function Markdown(runner) { buf += '```\n\n'; }); - runner.once('end', function() { + runner.once(Runner.constants.RUNNER_EVENT_END, function() { process.stdout.write('# TOC\n'); process.stdout.write(generateTOC(runner.suite)); process.stdout.write(buf); diff --git a/lib/reporters/min.js b/lib/reporters/min.js index 3277ed3442..d7f4469dd8 100644 --- a/lib/reporters/min.js +++ b/lib/reporters/min.js @@ -7,6 +7,7 @@ */ var Base = require('./base'); +var Runner = require('../runner'); var inherits = require('../utils').inherits; /** @@ -27,14 +28,14 @@ exports = module.exports = Min; function Min(runner) { Base.call(this, runner); - runner.on('start', function() { + runner.on(Runner.constants.RUNNER_EVENT_START, function() { // clear screen process.stdout.write('\u001b[2J'); // set cursor position process.stdout.write('\u001b[1;3H'); }); - runner.once('end', this.epilogue.bind(this)); + runner.once(Runner.constants.RUNNER_EVENT_END, this.epilogue.bind(this)); } /** diff --git a/lib/reporters/nyan.js b/lib/reporters/nyan.js index c91cd5ddf3..7f4707a93b 100644 --- a/lib/reporters/nyan.js +++ b/lib/reporters/nyan.js @@ -7,6 +7,7 @@ */ var Base = require('./base'); +var Runner = require('../runner'); var inherits = require('../utils').inherits; /** @@ -40,24 +41,24 @@ function NyanCat(runner) { this.trajectories = [[], [], [], []]; this.trajectoryWidthMax = width - nyanCatWidth; - runner.on('start', function() { + runner.on(Runner.constants.RUNNER_EVENT_START, function() { Base.cursor.hide(); self.draw(); }); - runner.on('pending', function() { + runner.on(Runner.constants.RUNNER_EVENT_PENDING, function() { self.draw(); }); - runner.on('pass', function() { + runner.on(Runner.constants.RUNNER_EVENT_PASS, function() { self.draw(); }); - runner.on('fail', function() { + runner.on(Runner.constants.RUNNER_EVENT_FAIL, function() { self.draw(); }); - runner.once('end', function() { + runner.once(Runner.constants.RUNNER_EVENT_END, function() { Base.cursor.show(); for (var i = 0; i < self.numberOfLines; i++) { write('\n'); diff --git a/lib/reporters/progress.js b/lib/reporters/progress.js index 5b174f0d5b..5ff9ac307b 100644 --- a/lib/reporters/progress.js +++ b/lib/reporters/progress.js @@ -7,6 +7,7 @@ */ var Base = require('./base'); +var Runner = require('../runner'); var inherits = require('../utils').inherits; var color = Base.color; var cursor = Base.cursor; @@ -53,13 +54,13 @@ function Progress(runner, options) { options.verbose = reporterOptions.verbose || false; // tests started - runner.on('start', function() { + runner.on(Runner.constants.RUNNER_EVENT_START, function() { console.log(); cursor.hide(); }); // tests complete - runner.on('test end', function() { + runner.on(Runner.constants.RUNNER_EVENT_TEST_END, function() { complete++; var percent = complete / total; @@ -85,7 +86,7 @@ function Progress(runner, options) { // tests are complete, output some stats // and the failures if any - runner.once('end', function() { + runner.once(Runner.constants.RUNNER_EVENT_END, function() { cursor.show(); console.log(); self.epilogue(); diff --git a/lib/reporters/spec.js b/lib/reporters/spec.js index 82a438fba7..8a4a6e14c7 100644 --- a/lib/reporters/spec.js +++ b/lib/reporters/spec.js @@ -7,6 +7,7 @@ */ var Base = require('./base'); +var Runner = require('../runner'); var inherits = require('../utils').inherits; var color = Base.color; @@ -36,11 +37,11 @@ function Spec(runner) { return Array(indents).join(' '); } - runner.on('start', function() { + runner.on(Runner.constants.RUNNER_EVENT_START, function() { console.log(); }); - runner.on('suite', function(suite) { + runner.on(Runner.constants.RUNNER_EVENT_SUITE, function(suite) { ++indents; console.log(color('suite', '%s%s'), indent(), suite.title); }); @@ -52,12 +53,12 @@ function Spec(runner) { } }); - runner.on('pending', function(test) { + runner.on(Runner.constants.RUNNER_EVENT_PENDING, function(test) { var fmt = indent() + color('pending', ' - %s'); console.log(fmt, test.title); }); - runner.on('pass', function(test) { + runner.on(Runner.constants.RUNNER_EVENT_PASS, function(test) { var fmt; if (test.speed === 'fast') { fmt = @@ -75,11 +76,11 @@ function Spec(runner) { } }); - runner.on('fail', function(test) { + runner.on(Runner.constants.RUNNER_EVENT_FAIL, function(test) { console.log(indent() + color('fail', ' %d) %s'), ++n, test.title); }); - runner.once('end', self.epilogue.bind(self)); + runner.once(Runner.constants.RUNNER_EVENT_END, self.epilogue.bind(self)); } /** diff --git a/lib/reporters/tap.js b/lib/reporters/tap.js index 4c7c15a864..277c5fa3ee 100644 --- a/lib/reporters/tap.js +++ b/lib/reporters/tap.js @@ -8,6 +8,7 @@ var util = require('util'); var Base = require('./base'); +var Runner = require('../runner'); var inherits = require('../utils').inherits; var sprintf = util.format; @@ -42,29 +43,29 @@ function TAP(runner, options) { this._producer = createProducer(tapVersion); - runner.once('start', function() { + runner.once(Runner.constants.RUNNER_EVENT_START, function() { var ntests = runner.grepTotal(runner.suite); self._producer.writeVersion(); self._producer.writePlan(ntests); }); - runner.on('test end', function() { + runner.on(Runner.constants.RUNNER_EVENT_TEST_END, function() { ++n; }); - runner.on('pending', function(test) { + runner.on(Runner.constants.RUNNER_EVENT_PENDING, function(test) { self._producer.writePending(n, test); }); - runner.on('pass', function(test) { + runner.on(Runner.constants.RUNNER_EVENT_PASS, function(test) { self._producer.writePass(n, test); }); - runner.on('fail', function(test, err) { + runner.on(Runner.constants.RUNNER_EVENT_FAIL, function(test, err) { self._producer.writeFail(n, test, err); }); - runner.once('end', function() { + runner.once(Runner.constants.RUNNER_EVENT_END, function() { self._producer.writeEpilogue(runner.stats); }); } diff --git a/lib/reporters/xunit.js b/lib/reporters/xunit.js index 643367aa9d..b23d10e436 100644 --- a/lib/reporters/xunit.js +++ b/lib/reporters/xunit.js @@ -8,6 +8,7 @@ var Base = require('./base'); var utils = require('../utils'); +var Runner = require('../runner'); var inherits = utils.inherits; var fs = require('fs'); var escape = utils.escape; @@ -65,19 +66,19 @@ function XUnit(runner, options) { // fall back to the default suite name suiteName = suiteName || DEFAULT_SUITE_NAME; - runner.on('pending', function(test) { + runner.on(Runner.constants.RUNNER_EVENT_PENDING, function(test) { tests.push(test); }); - runner.on('pass', function(test) { + runner.on(Runner.constants.RUNNER_EVENT_PASS, function(test) { tests.push(test); }); - runner.on('fail', function(test) { + runner.on(Runner.constants.RUNNER_EVENT_FAIL, function(test) { tests.push(test); }); - runner.once('end', function() { + runner.once(Runner.constants.RUNNER_EVENT_END, function() { self.write( tag( 'testsuite', diff --git a/lib/runner.js b/lib/runner.js index 604d79b495..2c5210c1c8 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -12,7 +12,7 @@ var utils = require('./utils'); var inherits = utils.inherits; var debug = require('debug')('mocha:runner'); var Runnable = require('./runnable'); -var createStatsCollector = require('./stats-collector'); +var Suite = require('./suite'); var stackFilter = utils.stackTraceFilter(); var stringify = utils.stringify; var type = utils.type; @@ -55,6 +55,7 @@ module.exports = Runner; * - `pass` (test) test passed * - `fail` (test, err) test failed * - `pending` (test) test pending + * - `retry` (test) test will retry after failure * * @memberof Mocha * @public @@ -72,16 +73,15 @@ function Runner(suite, delay) { this.started = false; this.total = suite.total(); this.failures = 0; - this.on('test end', function(test) { + this.on(constants.RUNNER_EVENT_TEST_END, function(test) { self.checkGlobals(test); }); - this.on('hook end', function(hook) { + this.on(constants.RUNNER_EVENT_HOOK_END, function(hook) { self.checkGlobals(hook); }); this._defaultGrep = /.*/; this.grep(this._defaultGrep); this.globals(this.globalProps().concat(extraGlobals())); - createStatsCollector(this); } /** @@ -227,7 +227,7 @@ Runner.prototype.fail = function(test, err) { } ++this.failures; - test.state = 'failed'; + test.state = constants.RUNNER_STATE_FAILED; if (!isError(err)) { err = thrown2Error(err); @@ -240,7 +240,7 @@ Runner.prototype.fail = function(test, err) { // some environments do not take kindly to monkeying with the stack } - this.emit('fail', test, err); + this.emit(constants.RUNNER_EVENT_FAIL, test, err); }; /** @@ -284,7 +284,7 @@ Runner.prototype.failHook = function(hook, err) { Runner.prototype.hook = function(name, fn) { var suite = this.suite; - var hooks = suite['_' + name]; + var hooks = suite.getHooks(name); var self = this; function next(i) { @@ -296,7 +296,7 @@ Runner.prototype.hook = function(name, fn) { hook.ctx.currentTest = self.test; - self.emit('hook', hook); + self.emit(constants.RUNNER_EVENT_HOOK, hook); if (!hook.listeners('error').length) { hook.on('error', function(err) { @@ -311,7 +311,10 @@ Runner.prototype.hook = function(name, fn) { } if (err) { if (err instanceof Pending) { - if (name === 'beforeEach' || name === 'afterEach') { + if ( + name === Suite.constants.SUITE_HOOK_BEFORE_EACH || + name === Suite.constants.SUITE_HOOK_AFTER_EACH + ) { self.test.pending = true; } else { suite.tests.forEach(function(test) { @@ -330,7 +333,7 @@ Runner.prototype.hook = function(name, fn) { return fn(err); } } - self.emit('hook end', hook); + self.emit(constants.RUNNER_EVENT_HOOK_END, hook); delete hook.ctx.currentTest; next(++i); }); @@ -473,7 +476,10 @@ Runner.prototype.runTests = function(suite, fn) { if (self.suite) { // call hookUp afterEach - self.hookUp('afterEach', function(err2, errSuite2) { + self.hookUp(Suite.constants.SUITE_HOOK_AFTER_EACH, function( + err2, + errSuite2 + ) { self.suite = orig; // some hooks may fail even now if (err2) { @@ -539,24 +545,27 @@ Runner.prototype.runTests = function(suite, fn) { self.fail(test, new Error('Pending test forbidden')); delete test.isPending; } else { - self.emit('pending', test); + self.emit(constants.RUNNER_EVENT_PENDING, test); } - self.emit('test end', test); + self.emit(constants.RUNNER_EVENT_TEST_END, test); return next(); } // execute test and hook(s) - self.emit('test', (self.test = test)); - self.hookDown('beforeEach', function(err, errSuite) { + self.emit(constants.RUNNER_EVENT_TEST, (self.test = test)); + self.hookDown(Suite.constants.SUITE_HOOK_BEFORE_EACH, function( + err, + errSuite + ) { if (test.isPending()) { if (self.forbidPending) { test.isPending = alwaysFalse; self.fail(test, new Error('Pending test forbidden')); delete test.isPending; } else { - self.emit('pending', test); + self.emit(constants.RUNNER_EVENT_PENDING, test); } - self.emit('test end', test); + self.emit(constants.RUNNER_EVENT_TEST_END, test); return next(); } if (err) { @@ -571,33 +580,33 @@ Runner.prototype.runTests = function(suite, fn) { self.fail(test, new Error('Pending test forbidden')); } else if (err instanceof Pending) { test.pending = true; - self.emit('pending', test); + self.emit(constants.RUNNER_EVENT_PENDING, test); } else if (retry < test.retries()) { var clonedTest = test.clone(); clonedTest.currentRetry(retry + 1); tests.unshift(clonedTest); - self.emit('retry', test, err); + self.emit(constants.RUNNER_EVENT_RETRY, test, err); // Early return + hook trigger so that it doesn't // increment the count wrong - return self.hookUp('afterEach', next); + return self.hookUp(Suite.constants.SUITE_HOOK_AFTER_EACH, next); } else { self.fail(test, err); } - self.emit('test end', test); + self.emit(constants.RUNNER_EVENT_TEST_END, test); if (err instanceof Pending) { return next(); } - return self.hookUp('afterEach', next); + return self.hookUp(Suite.constants.SUITE_HOOK_AFTER_EACH, next); } - test.state = 'passed'; - self.emit('pass', test); - self.emit('test end', test); - self.hookUp('afterEach', next); + test.state = constants.RUNNER_STATE_PASSED; + self.emit(constants.RUNNER_EVENT_PASS, test); + self.emit(constants.RUNNER_EVENT_TEST_END, test); + self.hookUp(Suite.constants.SUITE_HOOK_AFTER_EACH, next); }); }); } @@ -630,7 +639,7 @@ Runner.prototype.runSuite = function(suite, fn) { return fn(); } - this.emit('suite', (this.suite = suite)); + this.emit(constants.RUNNER_EVENT_SUITE, (this.suite = suite)); function next(errSuite) { if (errSuite) { @@ -680,8 +689,8 @@ Runner.prototype.runSuite = function(suite, fn) { // remove reference to test delete self.test; - self.hook('afterAll', function() { - self.emit('suite end', suite); + self.hook(Suite.constants.SUITE_HOOK_AFTER_ALL, function() { + self.emit(constants.RUNNER_EVENT_SUITE_END, suite); fn(errSuite); }); } @@ -689,7 +698,7 @@ Runner.prototype.runSuite = function(suite, fn) { this.nextSuite = next; - this.hook('beforeAll', function(err) { + this.hook(Suite.constants.SUITE_HOOK_BEFORE_ALL, function(err) { if (err) { return done(); } @@ -734,9 +743,9 @@ Runner.prototype.uncaught = function(err) { this.fail(runnable, err); } else { // Can't recover from this failure - this.emit('start'); + this.emit(constants.RUNNER_EVENT_START); this.fail(runnable, err); - this.emit('end'); + this.emit(constants.RUNNER_EVENT_END); } return; @@ -756,14 +765,16 @@ Runner.prototype.uncaught = function(err) { this.fail(runnable, err); if (!alreadyPassed) { // recover from test - if (runnable.type === 'test') { - this.emit('test end', runnable); - this.hookUp('afterEach', this.next); + if (runnable.type === constants.RUNNER_EVENT_TEST) { + this.emit(constants.RUNNER_EVENT_TEST_END, runnable); + this.hookUp(Suite.constants.SUITE_HOOK_AFTER_EACH, this.next); return; } // recover from hooks var errSuite = this.suite; + + // XXX how about a less awful way to determine this? // if hook failure is in afterEach block if (runnable.fullTitle().indexOf('after each') > -1) { return this.hookErr(err, errSuite, true); @@ -777,47 +788,9 @@ Runner.prototype.uncaught = function(err) { } // bail - this.emit('end'); + this.emit(constants.RUNNER_EVENT_END); }; -/** - * Cleans up the references to all the deferred functions - * (before/after/beforeEach/afterEach) and tests of a Suite. - * These must be deleted otherwise a memory leak can happen, - * as those functions may reference variables from closures, - * thus those variables can never be garbage collected as long - * as the deferred functions exist. - * - * @param {Suite} suite - */ -function cleanSuiteReferences(suite) { - function cleanArrReferences(arr) { - for (var i = 0; i < arr.length; i++) { - delete arr[i].fn; - } - } - - if (Array.isArray(suite._beforeAll)) { - cleanArrReferences(suite._beforeAll); - } - - if (Array.isArray(suite._beforeEach)) { - cleanArrReferences(suite._beforeEach); - } - - if (Array.isArray(suite._afterAll)) { - cleanArrReferences(suite._afterAll); - } - - if (Array.isArray(suite._afterEach)) { - cleanArrReferences(suite._afterEach); - } - - for (var i = 0; i < suite.tests.length; i++) { - delete suite.tests[i].fn; - } -} - /** * Run the root suite and invoke `fn(failures)` * on completion. @@ -844,25 +817,27 @@ Runner.prototype.run = function(fn) { } self.started = true; Runner.immediately(function() { - self.emit('start'); + self.emit(constants.RUNNER_EVENT_START); }); self.runSuite(rootSuite, function() { debug('finished running'); Runner.immediately(function() { - self.emit('end'); + self.emit(constants.RUNNER_EVENT_END); }); }); } - debug('start'); + debug(constants.RUNNER_EVENT_START); // references cleanup to avoid memory leaks - this.on('suite end', cleanSuiteReferences); + this.on(constants.RUNNER_EVENT_SUITE_END, function(suite) { + suite.cleanReferences(); + }); // callback - this.on('end', function() { - debug('end'); + this.on(constants.RUNNER_EVENT_END, function() { + debug(constants.RUNNER_EVENT_END); process.removeListener('uncaughtException', uncaught); fn(self.failures); }); @@ -873,8 +848,8 @@ Runner.prototype.run = function(fn) { if (this._delay) { // for reporters, I guess. // might be nice to debounce some dots while we wait. - this.emit('waiting', rootSuite); - rootSuite.once('run', start); + this.emit(constants.RUNNER_EVENT_WAITING, rootSuite); + rootSuite.once(Suite.constants.SUITE_EVENT_RUN, start); } else { start(); } @@ -1017,6 +992,8 @@ function thrown2Error(err) { * Array of globals dependent on the environment. * * @return {Array} + * @deprecated + * @todo remove; long since unsupported * @private */ function extraGlobals() { @@ -1027,7 +1004,6 @@ function extraGlobals() { }); // 'errno' was renamed to process._errno in v0.9.11. - if (nodeVersion < 0x00090b) { return ['errno']; } @@ -1035,3 +1011,27 @@ function extraGlobals() { return []; } + +/** + * Constants used by Runner + * @public + */ +var constants = Object.freeze({ + RUNNER_EVENT_END: 'end', + RUNNER_EVENT_FAIL: 'fail', + RUNNER_EVENT_HOOK: 'hook', + RUNNER_EVENT_HOOK_END: 'hook end', + RUNNER_EVENT_PASS: 'pass', + RUNNER_EVENT_PENDING: 'pending', + RUNNER_EVENT_RETRY: 'retry', + RUNNER_EVENT_START: 'start', + RUNNER_EVENT_SUITE: 'suite', + RUNNER_EVENT_SUITE_END: 'suite end', + RUNNER_EVENT_TEST: 'test', + RUNNER_EVENT_TEST_END: 'test end', + RUNNER_EVENT_WAITING: 'waiting', + RUNNER_STATE_FAILED: 'failed', + RUNNER_STATE_PASSED: 'passed' +}); + +module.exports.constants = constants; diff --git a/lib/stats-collector.js b/lib/stats-collector.js index 7a5b3fc61f..0c4347fc45 100644 --- a/lib/stats-collector.js +++ b/lib/stats-collector.js @@ -6,6 +6,8 @@ * @module */ +var Runner = require('./runner'); + /** * Test statistics collector. * @@ -48,31 +50,31 @@ function createStatsCollector(runner) { runner.stats = stats; - runner.once('start', function() { + runner.once(Runner.constants.RUNNER_EVENT_START, function() { stats.start = new Date(); }); - runner.on('suite', function(suite) { + runner.on(Runner.constants.RUNNER_EVENT_SUITE, function(suite) { suite.root || stats.suites++; }); - runner.on('pass', function() { + runner.on(Runner.constants.RUNNER_EVENT_PASS, function() { stats.passes++; }); - runner.on('fail', function() { + runner.on(Runner.constants.RUNNER_EVENT_FAIL, function() { stats.failures++; }); - runner.on('pending', function() { + runner.on(Runner.constants.RUNNER_EVENT_PENDING, function() { stats.pending++; }); - runner.on('test end', function() { + runner.on(Runner.constants.RUNNER_EVENT_TEST_END, function() { stats.tests++; }); - runner.once('end', function() { + runner.once(Runner.constants.RUNNER_EVENT_END, function() { stats.end = new Date(); stats.duration = stats.end - stats.start; }); diff --git a/lib/suite.js b/lib/suite.js index 64e7db4b24..230d1dde20 100644 --- a/lib/suite.js +++ b/lib/suite.js @@ -241,7 +241,7 @@ Suite.prototype.beforeAll = function(title, fn) { var hook = this._createHook(title, fn); this._beforeAll.push(hook); - this.emit('beforeAll', hook); + this.emit(constants.SUITE_EVENT_BEFORE_ALL, hook); return this; }; @@ -265,7 +265,7 @@ Suite.prototype.afterAll = function(title, fn) { var hook = this._createHook(title, fn); this._afterAll.push(hook); - this.emit('afterAll', hook); + this.emit(constants.SUITE_EVENT_AFTER_ALL, hook); return this; }; @@ -289,7 +289,7 @@ Suite.prototype.beforeEach = function(title, fn) { var hook = this._createHook(title, fn); this._beforeEach.push(hook); - this.emit('beforeEach', hook); + this.emit(constants.SUITE_EVENT_BEFORE_EACH, hook); return this; }; @@ -313,7 +313,7 @@ Suite.prototype.afterEach = function(title, fn) { var hook = this._createHook(title, fn); this._afterEach.push(hook); - this.emit('afterEach', hook); + this.emit(constants.SUITE_EVENT_AFTER_EACH, hook); return this; }; @@ -332,7 +332,7 @@ Suite.prototype.addSuite = function(suite) { suite.slow(this.slow()); suite.bail(this.bail()); this.suites.push(suite); - this.emit('suite', suite); + this.emit(constants.SUITE_EVENT_SUITE, suite); return this; }; @@ -351,7 +351,7 @@ Suite.prototype.addTest = function(test) { test.slow(this.slow()); test.ctx = this.ctx; this.tests.push(test); - this.emit('test', test); + this.emit(constants.SUITE_EVENT_TEST, test); return this; }; @@ -422,6 +422,79 @@ Suite.prototype.eachTest = function(fn) { */ Suite.prototype.run = function run() { if (this.root) { - this.emit('run'); + this.emit(constants.SUITE_EVENT_RUN); } }; + +/** + * Returns the array of hooks by hook name; see `SUITE_HOOK_*` constants. + * @private + */ +Suite.prototype.getHooks = function getHooks(name) { + return this['_' + name]; +}; + +/** + * Cleans up the references to all the deferred functions + * (before/after/beforeEach/afterEach) and tests of a Suite. + * These must be deleted otherwise a memory leak can happen, + * as those functions may reference variables from closures, + * thus those variables can never be garbage collected as long + * as the deferred functions exist. + * + * @private + */ +Suite.prototype.cleanReferences = function cleanReferences() { + function cleanArrReferences(arr) { + for (var i = 0; i < arr.length; i++) { + delete arr[i].fn; + } + } + + if (Array.isArray(this._beforeAll)) { + cleanArrReferences(this._beforeAll); + } + + if (Array.isArray(this._beforeEach)) { + cleanArrReferences(this._beforeEach); + } + + if (Array.isArray(this._afterAll)) { + cleanArrReferences(this._afterAll); + } + + if (Array.isArray(this._afterEach)) { + cleanArrReferences(this._afterEach); + } + + for (var i = 0; i < this.tests.length; i++) { + delete this.tests[i].fn; + } +}; + +/** + * Constants used by Suite + * @public + */ +var constants = Object.freeze({ + SUITE_EVENT_POST_REQUIRE: 'post-require', + SUITE_EVENT_PRE_REQUIRE: 'pre-require', + SUITE_EVENT_REQUIRE: 'require', + SUITE_EVENT_RUN: 'run', + SUITE_HOOK_BEFORE_ALL: 'beforeAll', + SUITE_HOOK_BEFORE_EACH: 'beforeEach', + SUITE_HOOK_AFTER_ALL: 'afterAll', + SUITE_HOOK_AFTER_EACH: 'afterEach', + + // XXX these are all unused and need to be removed. + // they've existed since time immemorial, so probably safe to consider + // removal to be a breaking change. + SUITE_EVENT_TEST: 'test', + SUITE_EVENT_SUITE: 'suite', + SUITE_EVENT_BEFORE_ALL: 'beforeAll', + SUITE_EVENT_BEFORE_EACH: 'beforeEach', + SUITE_EVENT_AFTER_ALL: 'afterAll', + SUITE_EVENT_AFTER_EACH: 'afterEach' +}); + +module.exports.constants = constants; diff --git a/test/integration/fixtures/regression/1794/simple-ui.js b/test/integration/fixtures/regression/1794/simple-ui.fixture.js similarity index 63% rename from test/integration/fixtures/regression/1794/simple-ui.js rename to test/integration/fixtures/regression/1794/simple-ui.fixture.js index b27c623003..70d18b0196 100644 --- a/test/integration/fixtures/regression/1794/simple-ui.js +++ b/test/integration/fixtures/regression/1794/simple-ui.fixture.js @@ -1,15 +1,22 @@ 'use strict'; -var path = '../../../../../lib/'; -var Mocha = require(path + 'mocha'); -var Test = require(path + 'test'); +var Mocha = require('../../../../../lib/mocha'); +var Test = Mocha.Test; +var Suite = Mocha.Suite; /** * A simple UI that only exposes a single function: test */ module.exports = Mocha.interfaces['simple-ui'] = function(suite) { - suite.on('pre-require', function(context, file, mocha) { - var common = require(path + 'interfaces/common')([suite], context); + suite.on(Suite.constants.SUITE_EVENT_PRE_REQUIRE, function( + context, + file, + mocha + ) { + var common = require('../../../../../lib/interfaces/common')( + [suite], + context + ); context.run = mocha.options.delay && common.runWithSuite(suite); diff --git a/test/integration/fixtures/simple-reporter.fixture.js b/test/integration/fixtures/simple-reporter.fixture.js new file mode 100644 index 0000000000..f5b5b5d76f --- /dev/null +++ b/test/integration/fixtures/simple-reporter.fixture.js @@ -0,0 +1,30 @@ +'use strict'; + +var Base = require('../../../lib/reporters/base'); +var Runner = require('../../../lib/runner'); + +module.exports = SimpleReporter; + +function SimpleReporter(runner) { + Base.call(this, runner); + + runner.on(Runner.constants.RUNNER_EVENT_SUITE, function(suite) { + console.log("on('suite') called"); + }); + + runner.on(Runner.constants.RUNNER_EVENT_FAIL, function(test, err) { + console.log("on('fail') called"); + }); + + runner.on(Runner.constants.RUNNER_EVENT_PASS, function(test) { + console.log("on('pass') called"); + }); + + runner.on(Runner.constants.RUNNER_EVENT_TEST_END, function(test, err) { + console.log("on('test end') called"); + }); + + runner.on(Runner.constants.RUNNER_EVENT_END, function() { + console.log("on('end') called"); + }); +} diff --git a/test/integration/fixtures/simple-reporter.js b/test/integration/fixtures/simple-reporter.js deleted file mode 100644 index d066a84304..0000000000 --- a/test/integration/fixtures/simple-reporter.js +++ /dev/null @@ -1,28 +0,0 @@ -'use strict'; - -var baseReporter = require('../../../lib/reporters/base'); -module.exports = simplereporter; - -function simplereporter(runner) { - baseReporter.call(this, runner); - - runner.on('suite', function(suite) { - console.log("on('suite') called"); - }); - - runner.on('fail', function(test, err) { - console.log("on('fail') called"); - }); - - runner.on('pass', function(test) { - console.log("on('pass') called"); - }); - - runner.on('test end', function(test, err) { - console.log("on('test end') called"); - }); - - runner.on('end', function() { - console.log("on('end') called"); - }); -} diff --git a/test/integration/helpers.js b/test/integration/helpers.js index 0680faa017..06baa0c2fb 100644 --- a/test/integration/helpers.js +++ b/test/integration/helpers.js @@ -3,7 +3,7 @@ var format = require('util').format; var spawn = require('cross-spawn').spawn; var path = require('path'); -var baseReporter = require('../../lib/reporters/base'); +var Base = require('../../lib/reporters/base'); module.exports = { /** @@ -132,7 +132,7 @@ module.exports = { /** * regular expression used for splitting lines based on new line / dot symbol. */ - splitRegExp: new RegExp('[\\n' + baseReporter.symbols.dot + ']+'), + splitRegExp: new RegExp('[\\n' + Base.symbols.dot + ']+'), /** * Invokes the mocha binary. Accepts an array of additional command line args diff --git a/test/integration/regression.spec.js b/test/integration/regression.spec.js index 566ccc7cd8..b122311383 100644 --- a/test/integration/regression.spec.js +++ b/test/integration/regression.spec.js @@ -31,7 +31,7 @@ describe('regressions', function() { 'fixtures', 'regression', '1794', - 'simple-ui.js' + 'simple-ui.fixture.js' ); var args = ['--require', simpleUiPath, '--ui', 'simple-ui']; run('regression/1794/issue-1794.fixture.js', args, function(err, res) { diff --git a/test/integration/reporters.spec.js b/test/integration/reporters.spec.js index c78da8ab6b..3d864091fe 100644 --- a/test/integration/reporters.spec.js +++ b/test/integration/reporters.spec.js @@ -66,7 +66,7 @@ describe('reporters', function() { describe('loader', function() { it('loads a reporter from a path relative to the current working directory', function(done) { var reporterAtARelativePath = - 'test/integration/fixtures/simple-reporter.js'; + 'test/integration/fixtures/simple-reporter.fixture.js'; var args = ['--reporter=' + reporterAtARelativePath]; @@ -84,7 +84,7 @@ describe('reporters', function() { // Generates an absolute path string var reporterAtAnAbsolutePath = path.join( process.cwd(), - 'test/integration/fixtures/simple-reporter.js' + 'test/integration/fixtures/simple-reporter.fixture.js' ); var args = ['--reporter=' + reporterAtAnAbsolutePath]; diff --git a/test/jsapi/index.js b/test/jsapi/index.js index beac56864b..fb2d4b5c5e 100644 --- a/test/jsapi/index.js +++ b/test/jsapi/index.js @@ -1,6 +1,7 @@ 'use strict'; var Mocha = require('../../'); +var Runner = Mocha.Runner; var mocha = new Mocha({ ui: 'bdd', @@ -25,6 +26,6 @@ mocha .run(function() { console.log('done'); }) - .on('pass', function(test) { + .on(Runner.constants.RUNNER_EVENT_PASS, function(test) { // console.log('... %s', test.title); }); diff --git a/test/unit/runner.spec.js b/test/unit/runner.spec.js index e89ef2b2d6..78dc188279 100644 --- a/test/unit/runner.spec.js +++ b/test/unit/runner.spec.js @@ -7,6 +7,7 @@ var Test = mocha.Test; var Hook = mocha.Hook; var path = require('path'); var noop = mocha.utils.noop; +var constants = Runner.constants; describe('Runner', function() { var suite; @@ -103,7 +104,7 @@ describe('Runner', function() { var test = new Test('im a test', noop); runner.checkGlobals(); global.foo = 'bar'; - runner.on('fail', function(_test, err) { + runner.on(constants.RUNNER_EVENT_FAIL, function(_test, err) { expect(_test, 'to be', test); expect(err.message, 'to be', 'global leak detected: foo'); delete global.foo; @@ -116,7 +117,7 @@ describe('Runner', function() { var doneCalled = false; runner.globals('good'); global.bad = 1; - runner.on('fail', function() { + runner.on(constants.RUNNER_EVENT_FAIL, function() { delete global.bad; done(); doneCalled = true; @@ -157,7 +158,7 @@ describe('Runner', function() { runner.checkGlobals(); global.foo = 'bar'; global.bar = 'baz'; - runner.on('fail', function(_test, err) { + runner.on(constants.RUNNER_EVENT_FAIL, function(_test, err) { expect(_test, 'to be', test); expect(err.message, 'to be', 'global leaks detected: foo, bar'); delete global.foo; @@ -191,7 +192,7 @@ describe('Runner', function() { global.foo = 'bar'; global.bar = 'baz'; - runner.on('fail', function(test, err) { + runner.on(constants.RUNNER_EVENT_FAIL, function(test, err) { expect(test.title, 'to be', 'im a test about lions'); expect(err.message, 'to be', 'global leak detected: bar'); delete global.foo; @@ -202,7 +203,7 @@ describe('Runner', function() { it('should emit "fail" when a global beginning with d is introduced', function(done) { global.derp = 'bar'; - runner.on('fail', function() { + runner.on(constants.RUNNER_EVENT_FAIL, function() { delete global.derp; done(); }); @@ -242,7 +243,7 @@ describe('Runner', function() { it('should emit "fail"', function(done) { var test = new Test('some other test', noop); var err = {}; - runner.on('fail', function(test, err) { + runner.on(constants.RUNNER_EVENT_FAIL, function(test, err) { expect(test, 'to be', test); expect(err, 'to be', err); done(); @@ -253,7 +254,7 @@ describe('Runner', function() { it('should emit a helpful message when failed with a string', function(done) { var test = new Test('helpful test', noop); var err = 'string'; - runner.on('fail', function(test, err) { + runner.on(constants.RUNNER_EVENT_FAIL, function(test, err) { expect( err.message, 'to be', @@ -267,7 +268,7 @@ describe('Runner', function() { it('should emit a the error when failed with an Error instance', function(done) { var test = new Test('a test', noop); var err = new Error('an error message'); - runner.on('fail', function(test, err) { + runner.on(constants.RUNNER_EVENT_FAIL, function(test, err) { expect(err.message, 'to be', 'an error message'); done(); }); @@ -277,7 +278,7 @@ describe('Runner', function() { it('should emit the error when failed with an Error-like object', function(done) { var test = new Test('a test', noop); var err = {message: 'an error message'}; - runner.on('fail', function(test, err) { + runner.on(constants.RUNNER_EVENT_FAIL, function(test, err) { expect(err.message, 'to be', 'an error message'); done(); }); @@ -287,7 +288,7 @@ describe('Runner', function() { it('should emit a helpful message when failed with an Object', function(done) { var test = new Test('a test', noop); var err = {x: 1}; - runner.on('fail', function(test, err) { + runner.on(constants.RUNNER_EVENT_FAIL, function(test, err) { expect( err.message, 'to be', @@ -301,7 +302,7 @@ describe('Runner', function() { it('should emit a helpful message when failed with an Array', function(done) { var test = new Test('a test', noop); var err = [1, 2]; - runner.on('fail', function(test, err) { + runner.on(constants.RUNNER_EVENT_FAIL, function(test, err) { expect( err.message, 'to be', @@ -324,7 +325,7 @@ describe('Runner', function() { }); var test = new Test('a test', noop); - runner.on('fail', function(test, err) { + runner.on(constants.RUNNER_EVENT_FAIL, function(test, err) { expect(err.message, 'to be', 'not evil'); done(); }); @@ -365,7 +366,7 @@ describe('Runner', function() { it('should emit "fail"', function(done) { var hook = new Hook(); var err = {}; - runner.on('fail', function(hook, err) { + runner.on(constants.RUNNER_EVENT_FAIL, function(hook, err) { expect(hook, 'to be', hook); expect(err, 'to be', err); done(); @@ -377,7 +378,7 @@ describe('Runner', function() { var hook = new Hook(); var err = {}; suite.bail(false); - runner.on('end', function() { + runner.on(constants.RUNNER_EVENT_END, function() { throw new Error('"end" was emit, but the bail is false'); }); runner.failHook(hook, err); @@ -400,7 +401,7 @@ describe('Runner', function() { suite.retries(retries); suite.addTest(test); - runner.on('retry', function(testClone, testErr) { + runner.on(constants.RUNNER_EVENT_RETRY, function(testClone, testErr) { retryableFails += 1; expect(testClone.title, 'to be', test.title); expect(testErr, 'to be', err); @@ -458,7 +459,7 @@ describe('Runner', function() { // Fake stack-trace err.stack = stack.join('\n'); - runner.on('fail', function(hook, err) { + runner.on(constants.RUNNER_EVENT_FAIL, function(hook, err) { expect(err.stack, 'to be', stack.slice(0, 3).join('\n')); done(); }); @@ -481,7 +482,7 @@ describe('Runner', function() { // Add --stack-trace option runner.fullStackTrace = true; - runner.on('fail', function(hook, err) { + runner.on(constants.RUNNER_EVENT_FAIL, function(hook, err) { expect(err.stack, 'to be', stack.join('\n')); done(); }); diff --git a/test/unit/throw.spec.js b/test/unit/throw.spec.js index 66021d3d38..404f9b8b83 100644 --- a/test/unit/throw.spec.js +++ b/test/unit/throw.spec.js @@ -33,7 +33,7 @@ describe('a test that throws', function() { }); suite.addTest(test); runner = new Runner(suite); - runner.on('end', function() { + runner.on(Runner.constants.RUNNER_EVENT_END, function() { expect(runner.failures, 'to be', 1); expect(test.state, 'to be', 'failed'); done(); @@ -47,7 +47,7 @@ describe('a test that throws', function() { }); suite.addTest(test); runner = new Runner(suite); - runner.on('end', function() { + runner.on(Runner.constants.RUNNER_EVENT_END, function() { expect(runner.failures, 'to be', 1); expect(test.state, 'to be', 'failed'); done(); @@ -63,7 +63,7 @@ describe('a test that throws', function() { }); suite.addTest(test); runner = new Runner(suite); - runner.on('end', function() { + runner.on(Runner.constants.RUNNER_EVENT_END, function() { expect(runner.failures, 'to be', 1); expect(test.state, 'to be', 'failed'); done(); @@ -79,7 +79,7 @@ describe('a test that throws', function() { }); suite.addTest(test); runner = new Runner(suite); - runner.on('end', function() { + runner.on(Runner.constants.RUNNER_EVENT_END, function() { expect(runner.failures, 'to be', 1); expect(test.state, 'to be', 'failed'); done(); @@ -93,7 +93,7 @@ describe('a test that throws', function() { }); suite.addTest(test); runner = new Runner(suite); - runner.on('end', function() { + runner.on(Runner.constants.RUNNER_EVENT_END, function() { expect(runner.failures, 'to be', 1); expect(test.state, 'to be', 'failed'); done(); @@ -111,7 +111,7 @@ describe('a test that throws', function() { }); suite.addTest(test); runner = new Runner(suite); - runner.on('end', function() { + runner.on(Runner.constants.RUNNER_EVENT_END, function() { expect(runner.failures, 'to be', 1); expect(test.state, 'to be', 'failed'); done(); @@ -127,7 +127,7 @@ describe('a test that throws', function() { }); suite.addTest(test); runner = new Runner(suite); - runner.on('end', function() { + runner.on(Runner.constants.RUNNER_EVENT_END, function() { expect(runner.failures, 'to be', 1); expect(test.state, 'to be', 'failed'); done(); @@ -141,7 +141,7 @@ describe('a test that throws', function() { }); suite.addTest(test); runner = new Runner(suite); - runner.on('end', function() { + runner.on(Runner.constants.RUNNER_EVENT_END, function() { expect(runner.failures, 'to be', 1); expect(test.state, 'to be', 'failed'); done(); @@ -157,7 +157,7 @@ describe('a test that throws', function() { }); suite.addTest(test); runner = new Runner(suite); - runner.on('end', function() { + runner.on(Runner.constants.RUNNER_EVENT_END, function() { expect(runner.failures, 'to be', 1); expect(test.state, 'to be', 'failed'); done();