Skip to content

Commit

Permalink
Native types exceptions can crash Mocha
Browse files Browse the repository at this point in the history
 - Any non-extensible type thrown in an async callback raises an exception in
 Mocha runner, stopping tests.
  • Loading branch information
fargies committed Apr 12, 2018
1 parent 1acea30 commit 41fe763
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,9 @@ Runner.prototype.uncaught = function (err) {
debug('uncaught undefined exception');
err = undefinedError();
}
if (!(err instanceof Error || (err && typeof err.message === 'string'))) {
err = new Error('the ' + type(err) + ' ' + stringify(err) + ' was thrown, throw an Error :)');
}
err.uncaught = true;

var runnable = this.currentRunnable;
Expand Down
18 changes: 18 additions & 0 deletions test/unit/throw.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ describe('a test that throws', function () {
});
});

describe('extensible', function () {
it('should not crash if throwing non-extensible type', function (done) {
var test = new Test('im async and throw string async', function (done2) {
process.nextTick(function () {
throw 'error';
});
});
suite.addTest(test);
runner = new Runner(suite);
runner.on('end', function () {
expect(runner.failures).to.equal(1);
expect(test.state).to.equal('failed');
done();
});
runner.run();
});
});

describe('undefined', function () {
it('should not pass if throwing sync and test is sync', function (done) {
var test = new Test('im sync and throw undefined sync', function () {
Expand Down

0 comments on commit 41fe763

Please sign in to comment.