From 10b7dcd581a3b1f07e34cd63957926b9ce6e6bab Mon Sep 17 00:00:00 2001 From: Norman Date: Sat, 7 Oct 2017 13:44:36 -0700 Subject: [PATCH] [Fix] fix stack where actual is falsy Fixes #399. --- lib/results.js | 2 +- test/stackTrace.js | 73 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 70 insertions(+), 5 deletions(-) diff --git a/lib/results.js b/lib/results.js index 36586530..76ee75de 100644 --- a/lib/results.js +++ b/lib/results.js @@ -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) { diff --git a/test/stackTrace.js b/test/stackTrace.js index 90cc39f6..14241ff6 100644 --- a/test/stackTrace.js +++ b/test/stackTrace.js @@ -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, @@ -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' @@ -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'); }); });