diff --git a/lib/mocha.js b/lib/mocha.js index 687f5c77a3..a0a84ed7c3 100644 --- a/lib/mocha.js +++ b/lib/mocha.js @@ -182,6 +182,21 @@ Mocha.prototype.addFile = function(file) { return this; }; +/** + * Adds EventListener to `mocha.runnerEvents []` which is used in `mocha.run()` + * for registering EventListeners to the `runner` instance. + * + * @public + * @param {string} event - Event name. + * @param {function} fn - Function to be called on emitted event. + */ +Mocha.prototype.runnerOn = function(event, fn) { + if (!this.runnerEvents) { + this.runnerEvents = []; + } + this.runnerEvents.push([event, fn]); +}; + /** * Sets reporter to `reporter`, defaults to "spec". * @@ -759,6 +774,11 @@ Mocha.prototype.run = function(fn) { var options = this.options; options.files = this.files; var runner = new exports.Runner(suite, options.delay); + if (this.runnerEvents) { + this.runnerEvents.forEach(function(evt) { + runner.on(evt[0], evt[1]); + }); + } var reporter = new this._reporter(runner, options); runner.ignoreLeaks = options.ignoreLeaks !== false; runner.fullStackTrace = options.fullStackTrace; diff --git a/lib/runner.js b/lib/runner.js index 604d79b495..52f061c9a1 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -843,15 +843,11 @@ Runner.prototype.run = function(fn) { filterOnly(rootSuite); } self.started = true; - Runner.immediately(function() { - self.emit('start'); - }); + self.emit('start'); self.runSuite(rootSuite, function() { debug('finished running'); - Runner.immediately(function() { - self.emit('end'); - }); + self.emit('end'); }); } diff --git a/test/integration/fixtures/runner/events-bail.fixture.js b/test/integration/fixtures/runner/events-bail.fixture.js index 3a345f8169..950e6154e9 100644 --- a/test/integration/fixtures/runner/events-bail.fixture.js +++ b/test/integration/fixtures/runner/events-bail.fixture.js @@ -3,7 +3,7 @@ var Runner = require('../../../../lib/runner.js'); var assert = require('assert'); var emitOrder = [ - 'suite'/* incorrect order*/, 'start', 'suite', + 'start', 'suite', 'suite', 'hook', 'hook end', 'test', 'hook', 'hook end', 'fail', 'test end', 'hook', 'hook end', 'hook', 'hook end', 'suite end', 'suite end', 'end' ]; diff --git a/test/integration/fixtures/runner/events-basic.fixture.js b/test/integration/fixtures/runner/events-basic.fixture.js index 81c55a8bb4..a36c00a903 100644 --- a/test/integration/fixtures/runner/events-basic.fixture.js +++ b/test/integration/fixtures/runner/events-basic.fixture.js @@ -3,7 +3,7 @@ var Runner = require('../../../../lib/runner.js'); var assert = require('assert'); var emitOrder = [ - 'suite'/* incorrect order*/, 'start', 'suite', + 'start', 'suite', 'suite', 'hook', 'hook end', 'test', 'hook', 'hook end', 'pass', 'test end', 'hook', 'hook end', 'suite', 'test', 'hook', 'hook end', 'pass', 'test end', 'hook', 'hook end', 'suite end', 'hook', 'hook end', 'suite end', 'suite end', 'end' diff --git a/test/integration/fixtures/runner/events-retries.fixture.js b/test/integration/fixtures/runner/events-retries.fixture.js index a4547c8fa8..b727a2ce3f 100644 --- a/test/integration/fixtures/runner/events-retries.fixture.js +++ b/test/integration/fixtures/runner/events-retries.fixture.js @@ -3,7 +3,7 @@ var Runner = require('../../../../lib/runner.js'); var assert = require('assert'); var emitOrder = [ - 'suite'/* incorrect order*/, 'start', 'suite', + 'start', 'suite', 'suite', 'hook', 'hook end', 'test', 'hook', 'hook end', 'retry', 'hook', 'hook end', 'test', 'hook', 'hook end', 'fail', 'test end', 'hook', 'hook end', 'hook', 'hook end', 'suite end', 'suite end', 'end'