Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Commit

Permalink
recover gracefully if stack is not writable; closes mochajs#2528
Browse files Browse the repository at this point in the history
I have no idea what's causing the error to have a non-writable `stack` property. ¯\_(ツ)_/¯
  • Loading branch information
boneskull committed Oct 11, 2016
1 parent 574b096 commit 8816a2b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
10 changes: 7 additions & 3 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,13 @@ Runner.prototype.fail = function (test, err) {
err = new Error('the ' + type(err) + ' ' + stringify(err) + ' was thrown, throw an Error :)');
}

err.stack = (this.fullStackTrace || !err.stack)
? err.stack
: stackFilter(err.stack);
try {
err.stack = (this.fullStackTrace || !err.stack)
? err.stack
: stackFilter(err.stack);
} catch (ignored) {
// some environments do not take kindly to monkeying with the stack
}

this.emit('fail', test, err);
};
Expand Down
6 changes: 6 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -794,3 +794,9 @@ exports.stackTraceFilter = function () {
exports.isPromise = function isPromise (value) {
return typeof value === 'object' && typeof value.then === 'function';
};

/**
* It's a noop.
* @api
*/
exports.noop = function () {};
18 changes: 16 additions & 2 deletions test/runner.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ var Runner = mocha.Runner;
var Test = mocha.Test;
var Hook = mocha.Hook;
var path = require('path');

function noop () {}
var noop = mocha.utils.noop;

describe('Runner', function () {
var suite;
Expand Down Expand Up @@ -291,6 +290,21 @@ describe('Runner', function () {
});
runner.fail(test, err);
});

it('should recover if the error stack is not writable', function (done) {
var err = new Error('not evil');
Object.defineProperty(err, 'stack', {
value: err.stack
});
var test = new Test('a test', noop);

runner.on('fail', function (test, err) {
err.message.should.equal('not evil');
done();
});

runner.fail(test, err);
});
});

describe('.failHook(hook, err)', function () {
Expand Down

0 comments on commit 8816a2b

Please sign in to comment.