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 7, 2019
1 parent 9d62030 commit d9cf38a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
22 changes: 22 additions & 0 deletions lib/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,23 @@ Mocha.prototype.addFile = function(file) {
return this;
};

/**
* @description
* 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.
* @example mocha.runnerOn('start', function() {...})
*/
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 +776,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 d9cf38a

Please sign in to comment.