From 5e55bc8d558db4c60cff4ac0309dc267d98f761e Mon Sep 17 00:00:00 2001 From: Christopher Hiller Date: Thu, 10 Jan 2019 14:44:06 -0800 Subject: [PATCH] peer review: dereference constants where applicable --- docs/api-tutorials/custom-reporter.md | 6 +- lib/browser/growl.js | 4 +- lib/growl.js | 4 +- lib/reporters/base.js | 8 ++- lib/reporters/doc.js | 14 +++-- lib/reporters/dot.js | 17 ++++-- lib/reporters/html.js | 14 +++-- lib/reporters/json-stream.js | 14 +++-- lib/reporters/json.js | 17 ++++-- lib/reporters/landing.js | 12 ++-- lib/reporters/list.js | 17 ++++-- lib/reporters/markdown.js | 11 ++-- lib/reporters/min.js | 8 ++- lib/reporters/nyan.js | 17 ++++-- lib/reporters/progress.js | 11 ++-- lib/reporters/spec.js | 20 ++++--- lib/reporters/tap.js | 20 ++++--- lib/reporters/xunit.js | 19 +++--- lib/runner.js | 8 +-- lib/stats-collector.js | 60 ++++++++++--------- .../fixtures/simple-reporter.fixture.js | 17 ++++-- test/jsapi/index.js | 11 +--- test/unit/runner.spec.js | 37 ++++++------ test/unit/throw.spec.js | 17 +++--- 24 files changed, 226 insertions(+), 157 deletions(-) diff --git a/docs/api-tutorials/custom-reporter.md b/docs/api-tutorials/custom-reporter.md index 68b605f50a..6769a49ce5 100644 --- a/docs/api-tutorials/custom-reporter.md +++ b/docs/api-tutorials/custom-reporter.md @@ -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, () => { @@ -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 | @@ -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 diff --git a/lib/browser/growl.js b/lib/browser/growl.js index d04f124c0e..83ccd7403a 100644 --- a/lib/browser/growl.js +++ b/lib/browser/growl.js @@ -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. @@ -54,7 +54,7 @@ exports.notify = function(runner) { .catch(notPermitted); }; - runner.once(Runner.constants.RUNNER_EVENT_END, sendNotification); + runner.once(RUNNER_EVENT_END, sendNotification); }; /** diff --git a/lib/growl.js b/lib/growl.js index ba0e57f513..0abbd58b35 100644 --- a/lib/growl.js +++ b/lib/growl.js @@ -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 @@ -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); }); }; diff --git a/lib/reporters/base.js b/lib/reporters/base.js index 5782ed95b4..23d3e20bae 100644 --- a/lib/reporters/base.js +++ b/lib/reporters/base.js @@ -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`. @@ -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) { @@ -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); } diff --git a/lib/reporters/doc.js b/lib/reporters/doc.js index 97f3e13b7a..ff8cc70f94 100644 --- a/lib/reporters/doc.js +++ b/lib/reporters/doc.js @@ -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`. @@ -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; } @@ -45,7 +49,7 @@ function Doc(runner) { console.log('%s
', indent()); }); - runner.on('suite end', function(suite) { + runner.on(RUNNER_EVENT_SUITE_END, function(suite) { if (suite.root) { return; } @@ -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
%s
', indent(), utils.escape(test.title)); var code = utils.escape(utils.clean(test.body)); console.log('%s
%s
', indent(), code); }); - runner.on(Runner.constants.RUNNER_EVENT_FAIL, function(test, err) { + runner.on(RUNNER_EVENT_FAIL, function(test, err) { console.log( '%s
%s
', indent(), diff --git a/lib/reporters/dot.js b/lib/reporters/dot.js index 3fb9bc27c8..468b5c2b2a 100644 --- a/lib/reporters/dot.js +++ b/lib/reporters/dot.js @@ -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`. @@ -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 '); } @@ -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(); }); diff --git a/lib/reporters/html.js b/lib/reporters/html.js index efca0819c3..f553ee9547 100644 --- a/lib/reporters/html.js +++ b/lib/reporters/html.js @@ -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; /** @@ -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; } @@ -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 = '
  • %e%ems ' + @@ -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( '
  • %e ' + playIcon + @@ -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( '
  • %e

  • ', test.title diff --git a/lib/reporters/json-stream.js b/lib/reporters/json-stream.js index f77dc1430e..2774f975a6 100644 --- a/lib/reporters/json-stream.js +++ b/lib/reporters/json-stream.js @@ -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`. @@ -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]); }); } diff --git a/lib/reporters/json.js b/lib/reporters/json.js index d35c076c06..b134b1cd9c 100644 --- a/lib/reporters/json.js +++ b/lib/reporters/json.js @@ -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`. @@ -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), diff --git a/lib/reporters/landing.js b/lib/reporters/landing.js index b2f3f2df89..7f4c53cb72 100644 --- a/lib/reporters/landing.js +++ b/lib/reporters/landing.js @@ -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; @@ -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; @@ -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(); diff --git a/lib/reporters/list.js b/lib/reporters/list.js index ba3bb678f5..0ff86f60d0 100644 --- a/lib/reporters/list.js +++ b/lib/reporters/list.js @@ -7,8 +7,13 @@ */ var Base = require('./base'); -var Runner = require('../runner'); var inherits = require('../utils').inherits; +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; var color = Base.color; var cursor = Base.cursor; @@ -33,7 +38,7 @@ function List(runner) { var self = this; var n = 0; - runner.on(Runner.constants.RUNNER_EVENT_START, function() { + runner.on(RUNNER_EVENT_BEGIN, function() { console.log(); }); @@ -41,12 +46,12 @@ function List(runner) { process.stdout.write(color('pass', ' ' + test.fullTitle() + ': ')); }); - runner.on(Runner.constants.RUNNER_EVENT_PENDING, function(test) { + runner.on(RUNNER_EVENT_PENDING, function(test) { var fmt = color('checkmark', ' -') + color('pending', ' %s'); console.log(fmt, test.fullTitle()); }); - runner.on(Runner.constants.RUNNER_EVENT_PASS, function(test) { + runner.on(RUNNER_EVENT_PASS, function(test) { var fmt = color('checkmark', ' ' + Base.symbols.ok) + color('pass', ' %s: ') + @@ -55,12 +60,12 @@ function List(runner) { console.log(fmt, test.fullTitle(), test.duration); }); - runner.on(Runner.constants.RUNNER_EVENT_FAIL, function(test) { + runner.on(RUNNER_EVENT_FAIL, function(test) { cursor.CR(); console.log(color('fail', ' %d) %s'), ++n, test.fullTitle()); }); - runner.once(Runner.constants.RUNNER_EVENT_END, self.epilogue.bind(self)); + runner.once(RUNNER_EVENT_END, self.epilogue.bind(self)); } /** diff --git a/lib/reporters/markdown.js b/lib/reporters/markdown.js index a67f7cad43..009695ec2d 100644 --- a/lib/reporters/markdown.js +++ b/lib/reporters/markdown.js @@ -8,7 +8,10 @@ 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_END = constants.RUNNER_EVENT_END; +var RUNNER_EVENT_SUITE = constants.RUNNER_EVENT_SUITE; /** * Constants @@ -78,7 +81,7 @@ function Markdown(runner) { generateTOC(runner.suite); - runner.on(Runner.constants.RUNNER_EVENT_SUITE, function(suite) { + runner.on(RUNNER_EVENT_SUITE, function(suite) { ++level; var slug = utils.slug(suite.fullTitle()); buf += '
    ' + '\n'; @@ -89,7 +92,7 @@ function Markdown(runner) { --level; }); - runner.on(Runner.constants.RUNNER_EVENT_PASS, function(test) { + runner.on(RUNNER_EVENT_PASS, function(test) { var code = utils.clean(test.body); buf += test.title + '.\n'; buf += '\n```js\n'; @@ -97,7 +100,7 @@ function Markdown(runner) { buf += '```\n\n'; }); - runner.once(Runner.constants.RUNNER_EVENT_END, function() { + runner.once(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 d7f4469dd8..72797e8b45 100644 --- a/lib/reporters/min.js +++ b/lib/reporters/min.js @@ -7,8 +7,10 @@ */ var Base = require('./base'); -var Runner = require('../runner'); var inherits = require('../utils').inherits; +var constants = require('../runner').constants; +var RUNNER_EVENT_END = constants.RUNNER_EVENT_END; +var RUNNER_EVENT_BEGIN = constants.RUNNER_EVENT_BEGIN; /** * Expose `Min`. @@ -28,14 +30,14 @@ exports = module.exports = Min; function Min(runner) { Base.call(this, runner); - runner.on(Runner.constants.RUNNER_EVENT_START, function() { + runner.on(RUNNER_EVENT_BEGIN, function() { // clear screen process.stdout.write('\u001b[2J'); // set cursor position process.stdout.write('\u001b[1;3H'); }); - runner.once(Runner.constants.RUNNER_EVENT_END, this.epilogue.bind(this)); + runner.once(RUNNER_EVENT_END, this.epilogue.bind(this)); } /** diff --git a/lib/reporters/nyan.js b/lib/reporters/nyan.js index 7f4707a93b..58fbb4e1a8 100644 --- a/lib/reporters/nyan.js +++ b/lib/reporters/nyan.js @@ -7,8 +7,13 @@ */ var Base = require('./base'); -var Runner = require('../runner'); +var constants = require('../runner').constants; var inherits = require('../utils').inherits; +var RUNNER_EVENT_BEGIN = constants.RUNNER_EVENT_BEGIN; +var RUNNER_EVENT_PENDING = constants.RUNNER_EVENT_PENDING; +var RUNNER_EVENT_PASS = constants.RUNNER_EVENT_PASS; +var RUNNER_EVENT_END = constants.RUNNER_EVENT_END; +var RUNNER_EVENT_FAIL = constants.RUNNER_EVENT_FAIL; /** * Expose `Dot`. @@ -41,24 +46,24 @@ function NyanCat(runner) { this.trajectories = [[], [], [], []]; this.trajectoryWidthMax = width - nyanCatWidth; - runner.on(Runner.constants.RUNNER_EVENT_START, function() { + runner.on(RUNNER_EVENT_BEGIN, function() { Base.cursor.hide(); self.draw(); }); - runner.on(Runner.constants.RUNNER_EVENT_PENDING, function() { + runner.on(RUNNER_EVENT_PENDING, function() { self.draw(); }); - runner.on(Runner.constants.RUNNER_EVENT_PASS, function() { + runner.on(RUNNER_EVENT_PASS, function() { self.draw(); }); - runner.on(Runner.constants.RUNNER_EVENT_FAIL, function() { + runner.on(RUNNER_EVENT_FAIL, function() { self.draw(); }); - runner.once(Runner.constants.RUNNER_EVENT_END, function() { + runner.once(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 5ff9ac307b..0aedb5bc1f 100644 --- a/lib/reporters/progress.js +++ b/lib/reporters/progress.js @@ -7,7 +7,10 @@ */ var Base = require('./base'); -var Runner = require('../runner'); +var constants = require('../runner').constants; +var RUNNER_EVENT_BEGIN = constants.RUNNER_EVENT_BEGIN; +var RUNNER_EVENT_TEST_END = constants.RUNNER_EVENT_TEST_END; +var RUNNER_EVENT_END = constants.RUNNER_EVENT_END; var inherits = require('../utils').inherits; var color = Base.color; var cursor = Base.cursor; @@ -54,13 +57,13 @@ function Progress(runner, options) { options.verbose = reporterOptions.verbose || false; // tests started - runner.on(Runner.constants.RUNNER_EVENT_START, function() { + runner.on(RUNNER_EVENT_BEGIN, function() { console.log(); cursor.hide(); }); // tests complete - runner.on(Runner.constants.RUNNER_EVENT_TEST_END, function() { + runner.on(RUNNER_EVENT_TEST_END, function() { complete++; var percent = complete / total; @@ -86,7 +89,7 @@ function Progress(runner, options) { // tests are complete, output some stats // and the failures if any - runner.once(Runner.constants.RUNNER_EVENT_END, function() { + runner.once(RUNNER_EVENT_END, function() { cursor.show(); console.log(); self.epilogue(); diff --git a/lib/reporters/spec.js b/lib/reporters/spec.js index 8a4a6e14c7..5dc0de6a0b 100644 --- a/lib/reporters/spec.js +++ b/lib/reporters/spec.js @@ -7,7 +7,13 @@ */ 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_SUITE = constants.RUNNER_EVENT_SUITE; +var RUNNER_EVENT_BEGIN = constants.RUNNER_EVENT_BEGIN; +var RUNNER_EVENT_END = constants.RUNNER_EVENT_END; +var RUNNER_EVENT_PENDING = constants.RUNNER_EVENT_PENDING; var inherits = require('../utils').inherits; var color = Base.color; @@ -37,11 +43,11 @@ function Spec(runner) { return Array(indents).join(' '); } - runner.on(Runner.constants.RUNNER_EVENT_START, function() { + runner.on(RUNNER_EVENT_BEGIN, function() { console.log(); }); - runner.on(Runner.constants.RUNNER_EVENT_SUITE, function(suite) { + runner.on(RUNNER_EVENT_SUITE, function(suite) { ++indents; console.log(color('suite', '%s%s'), indent(), suite.title); }); @@ -53,12 +59,12 @@ function Spec(runner) { } }); - runner.on(Runner.constants.RUNNER_EVENT_PENDING, function(test) { + runner.on(RUNNER_EVENT_PENDING, function(test) { var fmt = indent() + color('pending', ' - %s'); console.log(fmt, test.title); }); - runner.on(Runner.constants.RUNNER_EVENT_PASS, function(test) { + runner.on(RUNNER_EVENT_PASS, function(test) { var fmt; if (test.speed === 'fast') { fmt = @@ -76,11 +82,11 @@ function Spec(runner) { } }); - runner.on(Runner.constants.RUNNER_EVENT_FAIL, function(test) { + runner.on(RUNNER_EVENT_FAIL, function(test) { console.log(indent() + color('fail', ' %d) %s'), ++n, test.title); }); - runner.once(Runner.constants.RUNNER_EVENT_END, self.epilogue.bind(self)); + runner.once(RUNNER_EVENT_END, self.epilogue.bind(self)); } /** diff --git a/lib/reporters/tap.js b/lib/reporters/tap.js index 277c5fa3ee..bf4bb56902 100644 --- a/lib/reporters/tap.js +++ b/lib/reporters/tap.js @@ -8,7 +8,13 @@ var util = require('util'); 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; +var RUNNER_EVENT_PENDING = constants.RUNNER_EVENT_PENDING; +var RUNNER_EVENT_TEST_END = constants.RUNNER_EVENT_TEST_END; var inherits = require('../utils').inherits; var sprintf = util.format; @@ -43,29 +49,29 @@ function TAP(runner, options) { this._producer = createProducer(tapVersion); - runner.once(Runner.constants.RUNNER_EVENT_START, function() { + runner.once(RUNNER_EVENT_BEGIN, function() { var ntests = runner.grepTotal(runner.suite); self._producer.writeVersion(); self._producer.writePlan(ntests); }); - runner.on(Runner.constants.RUNNER_EVENT_TEST_END, function() { + runner.on(RUNNER_EVENT_TEST_END, function() { ++n; }); - runner.on(Runner.constants.RUNNER_EVENT_PENDING, function(test) { + runner.on(RUNNER_EVENT_PENDING, function(test) { self._producer.writePending(n, test); }); - runner.on(Runner.constants.RUNNER_EVENT_PASS, function(test) { + runner.on(RUNNER_EVENT_PASS, function(test) { self._producer.writePass(n, test); }); - runner.on(Runner.constants.RUNNER_EVENT_FAIL, function(test, err) { + runner.on(RUNNER_EVENT_FAIL, function(test, err) { self._producer.writeFail(n, test, err); }); - runner.once(Runner.constants.RUNNER_EVENT_END, function() { + runner.once(RUNNER_EVENT_END, function() { self._producer.writeEpilogue(runner.stats); }); } diff --git a/lib/reporters/xunit.js b/lib/reporters/xunit.js index b23d10e436..8b601994ec 100644 --- a/lib/reporters/xunit.js +++ b/lib/reporters/xunit.js @@ -8,14 +8,19 @@ var Base = require('./base'); var utils = require('../utils'); -var Runner = require('../runner'); -var inherits = utils.inherits; var fs = require('fs'); -var escape = utils.escape; var mkdirp = require('mkdirp'); var path = require('path'); var errors = require('../errors'); +var constants = require('../runner').constants; +var RUNNER_EVENT_PASS = constants.RUNNER_EVENT_PASS; +var RUNNER_EVENT_FAIL = constants.RUNNER_EVENT_FAIL; +var RUNNER_EVENT_END = constants.RUNNER_EVENT_END; +var RUNNER_EVENT_PENDING = constants.RUNNER_EVENT_PENDING; +var inherits = utils.inherits; +var escape = utils.escape; var createNotSupportedError = errors.createNotSupportedError; + /** * Save timer references to avoid Sinon interfering (see GH-237). */ @@ -66,19 +71,19 @@ function XUnit(runner, options) { // fall back to the default suite name suiteName = suiteName || DEFAULT_SUITE_NAME; - runner.on(Runner.constants.RUNNER_EVENT_PENDING, function(test) { + runner.on(RUNNER_EVENT_PENDING, function(test) { tests.push(test); }); - runner.on(Runner.constants.RUNNER_EVENT_PASS, function(test) { + runner.on(RUNNER_EVENT_PASS, function(test) { tests.push(test); }); - runner.on(Runner.constants.RUNNER_EVENT_FAIL, function(test) { + runner.on(RUNNER_EVENT_FAIL, function(test) { tests.push(test); }); - runner.once(Runner.constants.RUNNER_EVENT_END, function() { + runner.once(RUNNER_EVENT_END, function() { self.write( tag( 'testsuite', diff --git a/lib/runner.js b/lib/runner.js index 2c5210c1c8..37f89a7280 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -743,7 +743,7 @@ Runner.prototype.uncaught = function(err) { this.fail(runnable, err); } else { // Can't recover from this failure - this.emit(constants.RUNNER_EVENT_START); + this.emit(constants.RUNNER_EVENT_BEGIN); this.fail(runnable, err); this.emit(constants.RUNNER_EVENT_END); } @@ -817,7 +817,7 @@ Runner.prototype.run = function(fn) { } self.started = true; Runner.immediately(function() { - self.emit(constants.RUNNER_EVENT_START); + self.emit(constants.RUNNER_EVENT_BEGIN); }); self.runSuite(rootSuite, function() { @@ -828,7 +828,7 @@ Runner.prototype.run = function(fn) { }); } - debug(constants.RUNNER_EVENT_START); + debug(constants.RUNNER_EVENT_BEGIN); // references cleanup to avoid memory leaks this.on(constants.RUNNER_EVENT_SUITE_END, function(suite) { @@ -1024,7 +1024,7 @@ var constants = Object.freeze({ RUNNER_EVENT_PASS: 'pass', RUNNER_EVENT_PENDING: 'pending', RUNNER_EVENT_RETRY: 'retry', - RUNNER_EVENT_START: 'start', + RUNNER_EVENT_BEGIN: 'start', RUNNER_EVENT_SUITE: 'suite', RUNNER_EVENT_SUITE_END: 'suite end', RUNNER_EVENT_TEST: 'test', diff --git a/lib/stats-collector.js b/lib/stats-collector.js index 0c4347fc45..f754371001 100644 --- a/lib/stats-collector.js +++ b/lib/stats-collector.js @@ -6,7 +6,14 @@ * @module */ -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_BEGIN = constants.RUNNER_EVENT_BEGIN; +var RUNNER_EVENT_PENDING = constants.RUNNER_EVENT_PENDING; +var RUNNER_EVENT_END = constants.RUNNER_EVENT_END; +var RUNNER_EVENT_TEST_END = constants.RUNNER_EVENT_TEST_END; /** * Test statistics collector. @@ -50,34 +57,29 @@ function createStatsCollector(runner) { runner.stats = stats; - runner.once(Runner.constants.RUNNER_EVENT_START, function() { - stats.start = new Date(); - }); - - runner.on(Runner.constants.RUNNER_EVENT_SUITE, function(suite) { - suite.root || stats.suites++; - }); - - runner.on(Runner.constants.RUNNER_EVENT_PASS, function() { - stats.passes++; - }); - - runner.on(Runner.constants.RUNNER_EVENT_FAIL, function() { - stats.failures++; - }); - - runner.on(Runner.constants.RUNNER_EVENT_PENDING, function() { - stats.pending++; - }); - - runner.on(Runner.constants.RUNNER_EVENT_TEST_END, function() { - stats.tests++; - }); - - runner.once(Runner.constants.RUNNER_EVENT_END, function() { - stats.end = new Date(); - stats.duration = stats.end - stats.start; - }); + runner + .once(RUNNER_EVENT_BEGIN, function() { + stats.start = new Date(); + }) + .on(RUNNER_EVENT_SUITE, function(suite) { + suite.root || stats.suites++; + }) + .on(RUNNER_EVENT_PASS, function() { + stats.passes++; + }) + .on(RUNNER_EVENT_FAIL, function() { + stats.failures++; + }) + .on(RUNNER_EVENT_PENDING, function() { + stats.pending++; + }) + .on(RUNNER_EVENT_TEST_END, function() { + stats.tests++; + }) + .once(RUNNER_EVENT_END, function() { + stats.end = new Date(); + stats.duration = stats.end - stats.start; + }); } module.exports = createStatsCollector; diff --git a/test/integration/fixtures/simple-reporter.fixture.js b/test/integration/fixtures/simple-reporter.fixture.js index f5b5b5d76f..b8cf68482b 100644 --- a/test/integration/fixtures/simple-reporter.fixture.js +++ b/test/integration/fixtures/simple-reporter.fixture.js @@ -1,30 +1,35 @@ 'use strict'; var Base = require('../../../lib/reporters/base'); -var Runner = require('../../../lib/runner'); +var constants = require('../../../lib/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_END = constants.RUNNER_EVENT_END; +var RUNNER_EVENT_TEST_END = constants.RUNNER_EVENT_TEST_END; module.exports = SimpleReporter; function SimpleReporter(runner) { Base.call(this, runner); - runner.on(Runner.constants.RUNNER_EVENT_SUITE, function(suite) { + runner.on(RUNNER_EVENT_SUITE, function(suite) { console.log("on('suite') called"); }); - runner.on(Runner.constants.RUNNER_EVENT_FAIL, function(test, err) { + runner.on(RUNNER_EVENT_FAIL, function(test, err) { console.log("on('fail') called"); }); - runner.on(Runner.constants.RUNNER_EVENT_PASS, function(test) { + runner.on(RUNNER_EVENT_PASS, function(test) { console.log("on('pass') called"); }); - runner.on(Runner.constants.RUNNER_EVENT_TEST_END, function(test, err) { + runner.on(RUNNER_EVENT_TEST_END, function(test, err) { console.log("on('test end') called"); }); - runner.on(Runner.constants.RUNNER_EVENT_END, function() { + runner.on(RUNNER_EVENT_END, function() { console.log("on('end') called"); }); } diff --git a/test/jsapi/index.js b/test/jsapi/index.js index fb2d4b5c5e..231c52fedf 100644 --- a/test/jsapi/index.js +++ b/test/jsapi/index.js @@ -1,7 +1,6 @@ 'use strict'; var Mocha = require('../../'); -var Runner = Mocha.Runner; var mocha = new Mocha({ ui: 'bdd', @@ -22,10 +21,6 @@ mocha.addFile('test/unit/duration.spec.js'); mocha.addFile('test/unit/globals.spec.js'); mocha.addFile('test/unit/timeout.spec.js'); -mocha - .run(function() { - console.log('done'); - }) - .on(Runner.constants.RUNNER_EVENT_PASS, function(test) { - // console.log('... %s', test.title); - }); +mocha.run(function() { + console.log('done'); +}); diff --git a/test/unit/runner.spec.js b/test/unit/runner.spec.js index 78dc188279..511c0a2e22 100644 --- a/test/unit/runner.spec.js +++ b/test/unit/runner.spec.js @@ -8,6 +8,9 @@ var Hook = mocha.Hook; var path = require('path'); var noop = mocha.utils.noop; var constants = Runner.constants; +var RUNNER_EVENT_FAIL = constants.RUNNER_EVENT_FAIL; +var RUNNER_EVENT_RETRY = constants.RUNNER_EVENT_RETRY; +var RUNNER_EVENT_END = constants.RUNNER_EVENT_END; describe('Runner', function() { var suite; @@ -104,7 +107,7 @@ describe('Runner', function() { var test = new Test('im a test', noop); runner.checkGlobals(); global.foo = 'bar'; - runner.on(constants.RUNNER_EVENT_FAIL, function(_test, err) { + runner.on(RUNNER_EVENT_FAIL, function(_test, err) { expect(_test, 'to be', test); expect(err.message, 'to be', 'global leak detected: foo'); delete global.foo; @@ -117,7 +120,7 @@ describe('Runner', function() { var doneCalled = false; runner.globals('good'); global.bad = 1; - runner.on(constants.RUNNER_EVENT_FAIL, function() { + runner.on(RUNNER_EVENT_FAIL, function() { delete global.bad; done(); doneCalled = true; @@ -158,7 +161,7 @@ describe('Runner', function() { runner.checkGlobals(); global.foo = 'bar'; global.bar = 'baz'; - runner.on(constants.RUNNER_EVENT_FAIL, function(_test, err) { + runner.on(RUNNER_EVENT_FAIL, function(_test, err) { expect(_test, 'to be', test); expect(err.message, 'to be', 'global leaks detected: foo, bar'); delete global.foo; @@ -192,7 +195,7 @@ describe('Runner', function() { global.foo = 'bar'; global.bar = 'baz'; - runner.on(constants.RUNNER_EVENT_FAIL, function(test, err) { + runner.on(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; @@ -203,7 +206,7 @@ describe('Runner', function() { it('should emit "fail" when a global beginning with d is introduced', function(done) { global.derp = 'bar'; - runner.on(constants.RUNNER_EVENT_FAIL, function() { + runner.on(RUNNER_EVENT_FAIL, function() { delete global.derp; done(); }); @@ -243,7 +246,7 @@ describe('Runner', function() { it('should emit "fail"', function(done) { var test = new Test('some other test', noop); var err = {}; - runner.on(constants.RUNNER_EVENT_FAIL, function(test, err) { + runner.on(RUNNER_EVENT_FAIL, function(test, err) { expect(test, 'to be', test); expect(err, 'to be', err); done(); @@ -254,7 +257,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(constants.RUNNER_EVENT_FAIL, function(test, err) { + runner.on(RUNNER_EVENT_FAIL, function(test, err) { expect( err.message, 'to be', @@ -268,7 +271,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(constants.RUNNER_EVENT_FAIL, function(test, err) { + runner.on(RUNNER_EVENT_FAIL, function(test, err) { expect(err.message, 'to be', 'an error message'); done(); }); @@ -278,7 +281,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(constants.RUNNER_EVENT_FAIL, function(test, err) { + runner.on(RUNNER_EVENT_FAIL, function(test, err) { expect(err.message, 'to be', 'an error message'); done(); }); @@ -288,7 +291,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(constants.RUNNER_EVENT_FAIL, function(test, err) { + runner.on(RUNNER_EVENT_FAIL, function(test, err) { expect( err.message, 'to be', @@ -302,7 +305,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(constants.RUNNER_EVENT_FAIL, function(test, err) { + runner.on(RUNNER_EVENT_FAIL, function(test, err) { expect( err.message, 'to be', @@ -325,7 +328,7 @@ describe('Runner', function() { }); var test = new Test('a test', noop); - runner.on(constants.RUNNER_EVENT_FAIL, function(test, err) { + runner.on(RUNNER_EVENT_FAIL, function(test, err) { expect(err.message, 'to be', 'not evil'); done(); }); @@ -366,7 +369,7 @@ describe('Runner', function() { it('should emit "fail"', function(done) { var hook = new Hook(); var err = {}; - runner.on(constants.RUNNER_EVENT_FAIL, function(hook, err) { + runner.on(RUNNER_EVENT_FAIL, function(hook, err) { expect(hook, 'to be', hook); expect(err, 'to be', err); done(); @@ -378,7 +381,7 @@ describe('Runner', function() { var hook = new Hook(); var err = {}; suite.bail(false); - runner.on(constants.RUNNER_EVENT_END, function() { + runner.on(RUNNER_EVENT_END, function() { throw new Error('"end" was emit, but the bail is false'); }); runner.failHook(hook, err); @@ -401,7 +404,7 @@ describe('Runner', function() { suite.retries(retries); suite.addTest(test); - runner.on(constants.RUNNER_EVENT_RETRY, function(testClone, testErr) { + runner.on(RUNNER_EVENT_RETRY, function(testClone, testErr) { retryableFails += 1; expect(testClone.title, 'to be', test.title); expect(testErr, 'to be', err); @@ -459,7 +462,7 @@ describe('Runner', function() { // Fake stack-trace err.stack = stack.join('\n'); - runner.on(constants.RUNNER_EVENT_FAIL, function(hook, err) { + runner.on(RUNNER_EVENT_FAIL, function(hook, err) { expect(err.stack, 'to be', stack.slice(0, 3).join('\n')); done(); }); @@ -482,7 +485,7 @@ describe('Runner', function() { // Add --stack-trace option runner.fullStackTrace = true; - runner.on(constants.RUNNER_EVENT_FAIL, function(hook, err) { + runner.on(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 404f9b8b83..817512fe44 100644 --- a/test/unit/throw.spec.js +++ b/test/unit/throw.spec.js @@ -5,6 +5,7 @@ var Suite = require('../../lib/suite'); var Test = require('../../lib/test'); var Runner = require('../../lib/runner'); +var RUNNER_EVENT_END = Runner.constants.RUNNER_EVENT_END; describe('a test that throws', function() { var suite; @@ -33,7 +34,7 @@ describe('a test that throws', function() { }); suite.addTest(test); runner = new Runner(suite); - runner.on(Runner.constants.RUNNER_EVENT_END, function() { + runner.on(RUNNER_EVENT_END, function() { expect(runner.failures, 'to be', 1); expect(test.state, 'to be', 'failed'); done(); @@ -47,7 +48,7 @@ describe('a test that throws', function() { }); suite.addTest(test); runner = new Runner(suite); - runner.on(Runner.constants.RUNNER_EVENT_END, function() { + runner.on(RUNNER_EVENT_END, function() { expect(runner.failures, 'to be', 1); expect(test.state, 'to be', 'failed'); done(); @@ -63,7 +64,7 @@ describe('a test that throws', function() { }); suite.addTest(test); runner = new Runner(suite); - runner.on(Runner.constants.RUNNER_EVENT_END, function() { + runner.on(RUNNER_EVENT_END, function() { expect(runner.failures, 'to be', 1); expect(test.state, 'to be', 'failed'); done(); @@ -79,7 +80,7 @@ describe('a test that throws', function() { }); suite.addTest(test); runner = new Runner(suite); - runner.on(Runner.constants.RUNNER_EVENT_END, function() { + runner.on(RUNNER_EVENT_END, function() { expect(runner.failures, 'to be', 1); expect(test.state, 'to be', 'failed'); done(); @@ -93,7 +94,7 @@ describe('a test that throws', function() { }); suite.addTest(test); runner = new Runner(suite); - runner.on(Runner.constants.RUNNER_EVENT_END, function() { + runner.on(RUNNER_EVENT_END, function() { expect(runner.failures, 'to be', 1); expect(test.state, 'to be', 'failed'); done(); @@ -127,7 +128,7 @@ describe('a test that throws', function() { }); suite.addTest(test); runner = new Runner(suite); - runner.on(Runner.constants.RUNNER_EVENT_END, function() { + runner.on(RUNNER_EVENT_END, function() { expect(runner.failures, 'to be', 1); expect(test.state, 'to be', 'failed'); done(); @@ -141,7 +142,7 @@ describe('a test that throws', function() { }); suite.addTest(test); runner = new Runner(suite); - runner.on(Runner.constants.RUNNER_EVENT_END, function() { + runner.on(RUNNER_EVENT_END, function() { expect(runner.failures, 'to be', 1); expect(test.state, 'to be', 'failed'); done(); @@ -157,7 +158,7 @@ describe('a test that throws', function() { }); suite.addTest(test); runner = new Runner(suite); - runner.on(Runner.constants.RUNNER_EVENT_END, function() { + runner.on(RUNNER_EVENT_END, function() { expect(runner.failures, 'to be', 1); expect(test.state, 'to be', 'failed'); done();