Skip to content

Commit

Permalink
Mocha.prototype.runnerOn()
Browse files Browse the repository at this point in the history
  • Loading branch information
juergba committed Jan 6, 2019
1 parent 3388c9b commit 34d1d2b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 9 deletions.
20 changes: 20 additions & 0 deletions lib/mocha.js
Expand Up @@ -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".
*
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 2 additions & 6 deletions lib/runner.js
Expand Up @@ -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');
});
}

Expand Down
2 changes: 1 addition & 1 deletion test/integration/fixtures/runner/events-bail.fixture.js
Expand Up @@ -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'
];
Expand Down
2 changes: 1 addition & 1 deletion test/integration/fixtures/runner/events-basic.fixture.js
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion test/integration/fixtures/runner/events-retries.fixture.js
Expand Up @@ -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'
Expand Down

0 comments on commit 34d1d2b

Please sign in to comment.