Skip to content

Commit

Permalink
Remove user defined addAssertion handlers from stack
Browse files Browse the repository at this point in the history
  • Loading branch information
sunesimonsen committed Jan 31, 2018
1 parent cc66bf2 commit a559200
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/UnexpectedError.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,15 @@ UnexpectedError.prototype.serializeMessage = function (outputFormat) {
var stackStart = findStackStart(lines);

lines.forEach(function (line, i) {
if (stackStart <= i && (/node_modules\/unexpected(?:-[^\/]+)?\//).test(line)) {
removedFrames = true;
} else {
var removeFrame = (
stackStart <= i &&
((/node_modules\/unexpected(?:-[^\/]+)?\//).test(line) ||
(/executeExpect.*node_modules\/unexpected\//).test(lines[i + 1]))
);

removedFrames = removedFrames || removeFrame;

if (!removeFrame) {
newStack.push(line);
}
});
Expand Down
30 changes: 30 additions & 0 deletions test/api/UnexpectedError.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,36 @@ describe('UnexpectedError', function () {
});
});

it('trims the stack for custom assertions in the consuming code', () => {
expect(function () {
expect.fail('wat');
}, 'to throw', function (err) {
err.useFullStackTrace = false;
err._hasSerializedErrorMessage = false;
err.stack =
'wat\n' +
' at oathbreaker (node_modules/unexpected/lib/oathbreaker.js:46:19)\n' +
' at Function.Unexpected.withError (node_modules/unexpected-sinon/lib/unexpected-sinon.js:123:1)\n' +
' at Function.Unexpected.withError (node_modules/unexpected/lib/Unexpected.js:792:12)\n' +
' at Function.<anonymous> (node_modules/unexpected/lib/assertions.js:569:16)\n' +
' at functionCalledByCustomHandler (test/my.spec.js:42:666)\n' +
' at myCustomHandler (test/assertions.js:666:42)\n' +
' at executeExpect (node_modules/unexpected/lib/Unexpected.js:1103:50)\n' +
' at Unexpected.expect (node_modules/unexpected/lib/Unexpected.js:1111:22)\n' +
' at Context.<anonymous> (test/my.spec.js:48:17)';

err.serializeMessage('text');

expect(err, 'to satisfy', {
stack:
'wat\n' +
' at functionCalledByCustomHandler (test/my.spec.js:42:666)\n' +
' at Context.<anonymous> (test/my.spec.js:48:17)\n' +
' set UNEXPECTED_FULL_TRACE=true to see the full stack trace'
});
});
});

describe('and the output format is set to html', function () {
it('shows a helping message about how to turn of stack trace trimming', function () {
expect(function () {
Expand Down

0 comments on commit a559200

Please sign in to comment.