Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1669: catch uncaught errors outside test suite execution #1886

Merged
merged 1 commit into from
Sep 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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