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

Stop truncating the stack of thrown errors #169

Merged
merged 1 commit into from
Jun 15, 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
7 changes: 1 addition & 6 deletions lib/Unexpected.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ var Assertion = require('./Assertion');
var createStandardErrorMessage = require('./createStandardErrorMessage');
var utils = require('./utils');
var magicpen = require('magicpen');
var truncateStack = utils.truncateStack;
var extend = utils.extend;
var leven = require('leven');
var makePromise = require('./makePromise');
Expand Down Expand Up @@ -591,9 +590,7 @@ Unexpected.prototype.expect = function expect(subject, testDescriptionString) {
testFrameworkPatch.promiseCreated();
return result.then(undefined, function (e) {
if (e && e._isUnexpected) {
var newError = new UnexpectedError(that.expect, assertion, e);
truncateStack(newError, wrappedExpect);
throw newError;
throw new UnexpectedError(that.expect, assertion, e);
}
throw e;
});
Expand All @@ -602,7 +599,6 @@ Unexpected.prototype.expect = function expect(subject, testDescriptionString) {
}
} catch (e) {
if (e && e._isUnexpected) {
truncateStack(e, wrappedExpect);
var wrappedError = new UnexpectedError(that.expect, assertion, e);
if (serializeErrorsFromWrappedExpect) {
that.setErrorMessage(wrappedError);
Expand Down Expand Up @@ -746,7 +742,6 @@ Unexpected.prototype.expect = function expect(subject, testDescriptionString) {
if (typeof mochaPhantomJS !== 'undefined') {
newError = e.clone();
}
truncateStack(newError, that.expect);
that.setErrorMessage(newError);
throw newError;
}
Expand Down
17 changes: 0 additions & 17 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,6 @@ var utils = module.exports = {
return target;
},

truncateStack: function (err, fn) {
if (Error.captureStackTrace) {
Error.captureStackTrace(err, fn);
} else if ('stack' in err) {
// Excludes IE<10, and fn cannot be anonymous for this backup plan to work:
var stackEntries = err.stack.split(/\r\n?|\n\r?/),
needle = 'at ' + fn.name + ' ';
for (var i = 0 ; i < stackEntries.length ; i += 1) {
if (stackEntries[i].indexOf(needle) !== -1) {
stackEntries.splice(1, i);
err.stack = stackEntries.join("\n");
}
}
}
return err;
},

findFirst: function (arr, predicate, thisObj) {
var scope = thisObj || null;
for (var i = 0 ; i < arr.length ; i += 1) {
Expand Down
49 changes: 0 additions & 49 deletions test/unexpected.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,6 @@ describe('unexpected', function () {
"})");
});
});

it('throws with a stack trace that has the calling function as the top frame when the assertion fails (if the environment supports it)', function () {
if (Error.captureStackTrace || 'stack' in new Error()) {
expect(function TheCallingFunction() {
expect(4 < 4, 'to be ok');
}, 'to throw exception', function (err) {
expect(err.stack.split('\n')[2], 'to match', /TheCallingFunction/);
});
}
});
});

describe('be assertion', function () {
Expand Down Expand Up @@ -4102,45 +4092,6 @@ describe('unexpected', function () {
});
});

it('truncates the stack when a custom assertion throws a regular assertion error', function () {
var clonedExpect = expect.clone().addAssertion('to equal foo', function theCustomAssertion(expect, subject) {
expect(subject, 'to equal', 'foo');
});
expect(function () {
clonedExpect('bar', 'to equal foo');
}, 'to throw', function (err) {
expect(err.stack, 'not to contain', 'theCustomAssertion');
});
});

it('does not truncate the stack or replace the error message when the assertion function itself contains an error', function () {
var clonedExpect = expect.clone().addAssertion('to equal foo', function theCustomAssertion(expect, subject) {
expect(subject, 'to equal', 'foo');
this(); // Will throw a TypeError
});
expect(function () {
clonedExpect('foo', 'to equal foo');
}, 'to throw', function (err) {
expect(err.stack, 'to contain', 'theCustomAssertion');
expect(err.message, 'to match', /is not a function|Function expected/);
});
});

it('does not truncate the stack or replace the error message when a nested custom assertion contains an error', function () {
var clonedExpect = expect.clone().addAssertion('to equal foo', function theCustomAssertion(expect, subject) {
expect(subject, 'to equal', 'foo');
this(); // Will throw a TypeError
}).addAssertion('to foo', function (expect, subject) {
expect(subject, 'to equal foo');
});
expect(function () {
clonedExpect('foo', 'to foo');
}, 'to throw', function (err) {
expect(err.stack, 'not to contain', "expected 'foo' to foo");
expect(err.message, 'not to contain', "expected 'foo' to foo");
});
});

it('nested expects throws if the assertion does not exists', function () {
var clonedExpect = expect.clone().addAssertion('to be foo', function theCustomAssertion(expect, subject) {
expect(subject, 'to bee', 'foo');
Expand Down
24 changes: 1 addition & 23 deletions unexpected.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ var Assertion = require(1);
var createStandardErrorMessage = require(5);
var utils = require(13);
var magicpen = require(30);
var truncateStack = utils.truncateStack;
var extend = utils.extend;
var leven = require(26);
var makePromise = require(7);
Expand Down Expand Up @@ -653,9 +652,7 @@ Unexpected.prototype.expect = function expect(subject, testDescriptionString) {
testFrameworkPatch.promiseCreated();
return result.then(undefined, function (e) {
if (e && e._isUnexpected) {
var newError = new UnexpectedError(that.expect, assertion, e);
truncateStack(newError, wrappedExpect);
throw newError;
throw new UnexpectedError(that.expect, assertion, e);
}
throw e;
});
Expand All @@ -664,7 +661,6 @@ Unexpected.prototype.expect = function expect(subject, testDescriptionString) {
}
} catch (e) {
if (e && e._isUnexpected) {
truncateStack(e, wrappedExpect);
var wrappedError = new UnexpectedError(that.expect, assertion, e);
if (serializeErrorsFromWrappedExpect) {
that.setErrorMessage(wrappedError);
Expand Down Expand Up @@ -808,7 +804,6 @@ Unexpected.prototype.expect = function expect(subject, testDescriptionString) {
if (typeof mochaPhantomJS !== 'undefined') {
newError = e.clone();
}
truncateStack(newError, that.expect);
that.setErrorMessage(newError);
throw newError;
}
Expand Down Expand Up @@ -3608,23 +3603,6 @@ var utils = module.exports = {
return target;
},

truncateStack: function (err, fn) {
if (Error.captureStackTrace) {
Error.captureStackTrace(err, fn);
} else if ('stack' in err) {
// Excludes IE<10, and fn cannot be anonymous for this backup plan to work:
var stackEntries = err.stack.split(/\r\n?|\n\r?/),
needle = 'at ' + fn.name + ' ';
for (var i = 0 ; i < stackEntries.length ; i += 1) {
if (stackEntries[i].indexOf(needle) !== -1) {
stackEntries.splice(1, i);
err.stack = stackEntries.join("\n");
}
}
}
return err;
},

findFirst: function (arr, predicate, thisObj) {
var scope = thisObj || null;
for (var i = 0 ; i < arr.length ; i += 1) {
Expand Down