Skip to content

Commit

Permalink
[Fix] fix stack where actual is falsy
Browse files Browse the repository at this point in the history
Fixes #399.
  • Loading branch information
nhamer authored and ljharb committed Oct 7, 2017
1 parent 13173a5 commit 10b7dcd
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/results.js
Expand Up @@ -157,7 +157,7 @@ function encodeResult (res, count) {
output += inner + 'at: ' + res.at + '\n';
}

var actualStack = res.actual && res.actual.stack;
var actualStack = res.actual && (typeof res.actual === 'object' || typeof res.actual === 'function') ? res.actual.stack : undefined;
var errorStack = res.error && res.error.stack;
var stack = defined(actualStack, errorStack);
if (stack) {
Expand Down
73 changes: 69 additions & 4 deletions test/stackTrace.js
Expand Up @@ -81,11 +81,11 @@ tap.test('preserves stack trace for failed assertions', function (tt) {
tt.equal(typeof data.diag.stack, 'string');
at = data.diag.at || '';
stack = data.diag.stack || '';
tt.ok(/^Error: false should be true(\n at .+)+/.exec(stack), 'stack should be a stack')
tt.ok(/^Error: true should be false(\n at .+)+/.exec(stack), 'stack should be a stack')
tt.deepEqual(data, {
ok: false,
id: 1,
name: "false should be true",
name: "true should be false",
diag: {
at: at,
stack: stack,
Expand All @@ -103,7 +103,7 @@ tap.test('preserves stack trace for failed assertions', function (tt) {
body,
'TAP version 13\n'
+ '# t.equal stack trace\n'
+ 'not ok 1 false should be true\n'
+ 'not ok 1 true should be false\n'
+ ' ---\n'
+ ' operator: equal\n'
+ ' expected: false\n'
Expand All @@ -129,7 +129,72 @@ tap.test('preserves stack trace for failed assertions', function (tt) {

test('t.equal stack trace', function (t) {
t.plan(1);
t.equal(true, false, 'false should be true');
t.equal(true, false, 'true should be false');
});
});

tap.test('preserves stack trace for failed assertions where actual===falsy', function (tt) {
tt.plan(6);

var test = tape.createHarness();
var stream = test.createStream();
var parser = stream.pipe(tapParser());

var stack = ''
parser.once('assert', function (data) {
tt.equal(typeof data.diag.at, 'string');
tt.equal(typeof data.diag.stack, 'string');
at = data.diag.at || '';
stack = data.diag.stack || '';
tt.ok(/^Error: false should be true(\n at .+)+/.exec(stack), 'stack should be a stack')
tt.deepEqual(data, {
ok: false,
id: 1,
name: "false should be true",
diag: {
at: at,
stack: stack,
operator: 'equal',
expected: true,
actual: false
}
});
});

stream.pipe(concat(function (body) {
var body = body.toString('utf8');
body = stripAt(body);
tt.equal(
body,
'TAP version 13\n'
+ '# t.equal stack trace\n'
+ 'not ok 1 false should be true\n'
+ ' ---\n'
+ ' operator: equal\n'
+ ' expected: true\n'
+ ' actual: false\n'
+ ' stack: |-\n'
+ ' '
+ stack.replace(/\n/g, '\n ') + '\n'
+ ' ...\n'
+ '\n'
+ '1..1\n'
+ '# tests 1\n'
+ '# pass 0\n'
+ '# fail 1\n'
);

tt.deepEqual(getDiag(body), {
stack: stack,
operator: 'equal',
expected: true,
actual: false
});
}));

test('t.equal stack trace', function (t) {
t.plan(1);
t.equal(false, true, 'false should be true');
});
});

Expand Down

0 comments on commit 10b7dcd

Please sign in to comment.