diff --git a/lib/runner.js b/lib/runner.js index 3340567f26..23954449f3 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -800,7 +800,7 @@ Runner.prototype.runSuite = function(suite, fn) { }; /** - * Handle uncaught exceptions. + * Handle uncaught exceptions within runner * * @param {Error} err * @private @@ -893,6 +893,17 @@ Runner.prototype.uncaught = function(err) { this.abort(); }; +/** + * 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. @@ -940,16 +951,12 @@ Runner.prototype.run = function(fn) { this.on(constants.EVENT_RUN_END, function() { debug(constants.EVENT_RUN_END); process.removeListener('uncaughtException', uncaught); - process.on('uncaughtException', function(err) { - if (err instanceof Pending) { - return; - } - throw err; - }); + process.on('uncaughtException', self.uncaughtEnd); fn(self.failures); }); // uncaught exception + process.removeListener('uncaughtException', self.uncaughtEnd); process.on('uncaughtException', uncaught); if (this._delay) {