Skip to content

Commit

Permalink
remove Runner#uncaughtEnd
Browse files Browse the repository at this point in the history
move logic to `Runner#uncaught`, since we can now rely on the value of `Runner#state`.

Signed-off-by: Christopher Hiller <boneskull@boneskull.com>
  • Loading branch information
boneskull committed May 14, 2020
1 parent 8768dd7 commit 9f0ecd5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 17 deletions.
20 changes: 6 additions & 14 deletions lib/runner.js
Expand Up @@ -878,6 +878,11 @@ Runner.prototype.uncaught = function(err) {
throw err;
}

if (this.state === constants.STATE_STOPPED) {
debug('uncaught(): throwing after run has completed!');
throw err;
}

if (err) {
debug('uncaught(): got truthy exception %O', err);
} else {
Expand Down Expand Up @@ -940,17 +945,6 @@ Runner.prototype.uncaught = function(err) {
}
};

/**
* Handle uncaught exceptions after runner's end event.
*
* @param {Error} err
* @private
*/
Runner.prototype.uncaughtEnd = function uncaughtEnd(err) {
if (err instanceof Pending) return;
throw err;
};

/**
* Run the root suite and invoke `fn(failures)`
* on completion.
Expand Down Expand Up @@ -1007,14 +1001,12 @@ Runner.prototype.run = function(fn) {
this.on(constants.EVENT_RUN_END, function() {
self.state = constants.STATE_STOPPED;
debug(constants.EVENT_RUN_END);
self._removeEventListener(process, 'uncaughtException', uncaught);
self._addEventListener(process, 'uncaughtException', self.uncaughtEnd);
debug('run(): emitted %s', constants.EVENT_RUN_END);
fn(self.failures);
});

// uncaught exception
self._removeEventListener(process, 'uncaughtException', self.uncaughtEnd);
self._removeEventListener(process, 'uncaughtException', uncaught);
self._addEventListener(process, 'uncaughtException', uncaught);

if (this._delay) {
Expand Down
6 changes: 3 additions & 3 deletions test/integration/fixtures/uncaught/listeners.fixture.js
@@ -1,13 +1,13 @@
'use strict';

const assert = require('assert');
const mocha = require("../../../../lib/mocha");
const mocha = require('../../../../lib/mocha');

// keep this low to avoid warning
for (let i = 0; i < 5; i++) {
const r = new mocha.Runner(new mocha.Suite("" + i, undefined));
const r = new mocha.Runner(new mocha.Suite('' + i, undefined));
r.run();
}

assert.equal(process.listenerCount('uncaughtException'), 1);
assert.equal(process.listeners('uncaughtException')[0].name, 'uncaughtEnd');
assert.equal(process.listeners('uncaughtException')[0].name, 'uncaught');

0 comments on commit 9f0ecd5

Please sign in to comment.