Skip to content

Commit bc1b9ef

Browse files
committed
recover gracefully if stack is not writable; closes #2528
I have no idea what's causing the error to have a non-writable `stack` property. ¯\_(ツ)_/¯
1 parent 5b83625 commit bc1b9ef

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

lib/runner.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,13 @@ Runner.prototype.fail = function (test, err) {
233233
err = new Error('the ' + type(err) + ' ' + stringify(err) + ' was thrown, throw an Error :)');
234234
}
235235

236-
err.stack = (this.fullStackTrace || !err.stack)
237-
? err.stack
238-
: stackFilter(err.stack);
236+
try {
237+
err.stack = (this.fullStackTrace || !err.stack)
238+
? err.stack
239+
: stackFilter(err.stack);
240+
} catch (ignored) {
241+
// some environments do not take kindly to monkeying with the stack
242+
}
239243

240244
this.emit('fail', test, err);
241245
};
@@ -819,11 +823,13 @@ Runner.prototype.run = function (fn) {
819823
this.on('end', function () {
820824
debug('end');
821825
process.removeListener('uncaughtException', uncaught);
826+
process.removeListener('unhandledRejection', uncaught);
822827
fn(self.failures);
823828
});
824829

825830
// uncaught exception
826831
process.on('uncaughtException', uncaught);
832+
// process.on('unhandledRejection', uncaught);
827833

828834
if (this._delay) {
829835
// for reporters, I guess.

lib/utils.js

+6
Original file line numberDiff line numberDiff line change
@@ -794,3 +794,9 @@ exports.stackTraceFilter = function () {
794794
exports.isPromise = function isPromise (value) {
795795
return typeof value === 'object' && typeof value.then === 'function';
796796
};
797+
798+
/**
799+
* It's a noop.
800+
* @api
801+
*/
802+
exports.noop = function () {};

test/runner.spec.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ var Runner = mocha.Runner;
66
var Test = mocha.Test;
77
var Hook = mocha.Hook;
88
var path = require('path');
9-
10-
function noop () {}
9+
var noop = mocha.utils.noop;
1110

1211
describe('Runner', function () {
1312
var suite;
@@ -291,6 +290,21 @@ describe('Runner', function () {
291290
});
292291
runner.fail(test, err);
293292
});
293+
294+
it('should recover if the error stack is not writable', function (done) {
295+
var err = new Error('not evil');
296+
Object.defineProperty(err, 'stack', {
297+
value: err.stack
298+
});
299+
var test = new Test('a test', noop);
300+
301+
runner.on('fail', function (test, err) {
302+
err.message.should.equal('not evil');
303+
done();
304+
});
305+
306+
runner.fail(test, err);
307+
});
294308
});
295309

296310
describe('.failHook(hook, err)', function () {

0 commit comments

Comments
 (0)