Skip to content

Commit

Permalink
Merge pull request #324 from execjosh/full-paths-in-assertion-error-s…
Browse files Browse the repository at this point in the history
…tack-traces

Full paths in assertion error stack traces
  • Loading branch information
indexzero committed Nov 21, 2014
2 parents 068e027 + e9b4d76 commit 96578a9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/assert/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ function errorDiff(err, type) {
}).join('');
}

function extractPathFromStack(stack) {
var regex = /\((.*?[a-zA-Z0-9._-]+\.(?:js|coffee))(:\d+):\d+\)/;
return stack.match(regex);
}

/*
Do not override .toString() when this.stack is used,
otherwise this will end in an endless recursive call...
Expand All @@ -85,7 +90,7 @@ require('assert').AssertionError.prototype.toStringEx = function () {
source;

if (this.stack) {
source = this.stack.match(/([a-zA-Z0-9._-]+\.(?:js|coffee))(:\d+):\d+/);
source = extractPathFromStack(this.stack);
}

function parse(str) {
Expand Down
16 changes: 15 additions & 1 deletion test/assert-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,21 @@ vows.describe('vows/assert').addBatch({
assert.notIncludes([1, 2, 3], 4);
assert.notIncludes({"red": 1, "blue": 2}, "green");
}
}
},

'An AssertionError': {
topic: function generateAssertionError() {
try {
assert.isFalse(true);
} catch (e) {
return e.toString();
}
},
'should have full path in stack trace': function(topic) {
var regexp = new RegExp("// " + __filename + ":\\d+");
assert.isTrue(regexp.test(topic));
}
},
}).export(module);

function assertError(assertion, args, fail) {
Expand Down

0 comments on commit 96578a9

Please sign in to comment.