Skip to content

Commit

Permalink
Fix #1669: catch uncaught errors outside test suite execution
Browse files Browse the repository at this point in the history
  • Loading branch information
danielstjules committed Sep 11, 2015
1 parent f023018 commit f22ab53
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var Pending = require('./pending');
var utils = require('./utils');
var inherits = utils.inherits;
var debug = require('debug')('mocha:runner');
var Runnable = require('./runnable');
var filter = utils.filter;
var indexOf = utils.indexOf;
var keys = utils.keys;
Expand Down Expand Up @@ -64,6 +65,7 @@ function Runner(suite, delay) {
this._abort = false;
this._delay = delay;
this.suite = suite;
this.started = false;
this.total = suite.total();
this.failures = 0;
this.on('test end', function(test) {
Expand Down Expand Up @@ -653,7 +655,20 @@ Runner.prototype.uncaught = function(err) {
err.uncaught = true;

var runnable = this.currentRunnable;

if (!runnable) {
runnable = new Runnable('Uncaught error outside test suite');
runnable.parent = this.suite;

if (this.started) {
this.fail(runnable, err);
} else {
// Can't recover from this failure
this.emit('start');
this.fail(runnable, err);
this.emit('end');
}

return;
}

Expand Down Expand Up @@ -712,6 +727,7 @@ Runner.prototype.run = function(fn) {
}

function start() {
self.started = true;
self.emit('start');
self.runSuite(rootSuite, function() {
debug('finished running');
Expand Down
5 changes: 5 additions & 0 deletions test/integration/fixtures/options/delay-fail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
setTimeout(function() {
throw new Error('oops');
it('test', function() {});
run();
}, 100);
14 changes: 14 additions & 0 deletions test/integration/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,20 @@ describe('options', function() {
done();
});
});

it('should throw an error if the test suite failed to run', function(done) {
run('options/delay-fail.js', args, function(err, res) {
assert(!err);
assert.equal(res.stats.pending, 0);
assert.equal(res.stats.passes, 0);
assert.equal(res.stats.failures, 1);

assert.equal(res.failures[0].title,
'Uncaught error outside test suite');
assert.equal(res.code, 1);
done();
});
});
});

describe('--grep', function() {
Expand Down

0 comments on commit f22ab53

Please sign in to comment.