Skip to content

Commit 34d1d2b

Browse files
committed
Mocha.prototype.runnerOn()
1 parent 3388c9b commit 34d1d2b

File tree

5 files changed

+25
-9
lines changed

5 files changed

+25
-9
lines changed

lib/mocha.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,21 @@ Mocha.prototype.addFile = function(file) {
182182
return this;
183183
};
184184

185+
/**
186+
* Adds EventListener to `mocha.runnerEvents []` which is used in `mocha.run()`
187+
* for registering EventListeners to the `runner` instance.
188+
*
189+
* @public
190+
* @param {string} event - Event name.
191+
* @param {function} fn - Function to be called on emitted event.
192+
*/
193+
Mocha.prototype.runnerOn = function(event, fn) {
194+
if (!this.runnerEvents) {
195+
this.runnerEvents = [];
196+
}
197+
this.runnerEvents.push([event, fn]);
198+
};
199+
185200
/**
186201
* Sets reporter to `reporter`, defaults to "spec".
187202
*
@@ -759,6 +774,11 @@ Mocha.prototype.run = function(fn) {
759774
var options = this.options;
760775
options.files = this.files;
761776
var runner = new exports.Runner(suite, options.delay);
777+
if (this.runnerEvents) {
778+
this.runnerEvents.forEach(function(evt) {
779+
runner.on(evt[0], evt[1]);
780+
});
781+
}
762782
var reporter = new this._reporter(runner, options);
763783
runner.ignoreLeaks = options.ignoreLeaks !== false;
764784
runner.fullStackTrace = options.fullStackTrace;

lib/runner.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -843,15 +843,11 @@ Runner.prototype.run = function(fn) {
843843
filterOnly(rootSuite);
844844
}
845845
self.started = true;
846-
Runner.immediately(function() {
847-
self.emit('start');
848-
});
846+
self.emit('start');
849847

850848
self.runSuite(rootSuite, function() {
851849
debug('finished running');
852-
Runner.immediately(function() {
853-
self.emit('end');
854-
});
850+
self.emit('end');
855851
});
856852
}
857853

test/integration/fixtures/runner/events-bail.fixture.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var Runner = require('../../../../lib/runner.js');
33
var assert = require('assert');
44

55
var emitOrder = [
6-
'suite'/* incorrect order*/, 'start', 'suite',
6+
'start', 'suite', 'suite',
77
'hook', 'hook end', 'test', 'hook', 'hook end', 'fail', 'test end', 'hook', 'hook end',
88
'hook', 'hook end', 'suite end', 'suite end', 'end'
99
];

test/integration/fixtures/runner/events-basic.fixture.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var Runner = require('../../../../lib/runner.js');
33
var assert = require('assert');
44

55
var emitOrder = [
6-
'suite'/* incorrect order*/, 'start', 'suite',
6+
'start', 'suite', 'suite',
77
'hook', 'hook end', 'test', 'hook', 'hook end', 'pass', 'test end', 'hook', 'hook end',
88
'suite', 'test', 'hook', 'hook end', 'pass', 'test end', 'hook', 'hook end',
99
'suite end', 'hook', 'hook end', 'suite end', 'suite end', 'end'

test/integration/fixtures/runner/events-retries.fixture.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var Runner = require('../../../../lib/runner.js');
33
var assert = require('assert');
44

55
var emitOrder = [
6-
'suite'/* incorrect order*/, 'start', 'suite',
6+
'start', 'suite', 'suite',
77
'hook', 'hook end', 'test', 'hook', 'hook end', 'retry', 'hook', 'hook end',
88
'test', 'hook', 'hook end', 'fail', 'test end', 'hook', 'hook end', 'hook', 'hook end',
99
'suite end', 'suite end', 'end'

0 commit comments

Comments
 (0)