Skip to content

Commit 10b7dcd

Browse files
nhamerljharb
authored andcommitted
[Fix] fix stack where actual is falsy
Fixes tape-testing#399.
1 parent 13173a5 commit 10b7dcd

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

lib/results.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ function encodeResult (res, count) {
157157
output += inner + 'at: ' + res.at + '\n';
158158
}
159159

160-
var actualStack = res.actual && res.actual.stack;
160+
var actualStack = res.actual && (typeof res.actual === 'object' || typeof res.actual === 'function') ? res.actual.stack : undefined;
161161
var errorStack = res.error && res.error.stack;
162162
var stack = defined(actualStack, errorStack);
163163
if (stack) {

test/stackTrace.js

+69-4
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ tap.test('preserves stack trace for failed assertions', function (tt) {
8181
tt.equal(typeof data.diag.stack, 'string');
8282
at = data.diag.at || '';
8383
stack = data.diag.stack || '';
84-
tt.ok(/^Error: false should be true(\n at .+)+/.exec(stack), 'stack should be a stack')
84+
tt.ok(/^Error: true should be false(\n at .+)+/.exec(stack), 'stack should be a stack')
8585
tt.deepEqual(data, {
8686
ok: false,
8787
id: 1,
88-
name: "false should be true",
88+
name: "true should be false",
8989
diag: {
9090
at: at,
9191
stack: stack,
@@ -103,7 +103,7 @@ tap.test('preserves stack trace for failed assertions', function (tt) {
103103
body,
104104
'TAP version 13\n'
105105
+ '# t.equal stack trace\n'
106-
+ 'not ok 1 false should be true\n'
106+
+ 'not ok 1 true should be false\n'
107107
+ ' ---\n'
108108
+ ' operator: equal\n'
109109
+ ' expected: false\n'
@@ -129,7 +129,72 @@ tap.test('preserves stack trace for failed assertions', function (tt) {
129129

130130
test('t.equal stack trace', function (t) {
131131
t.plan(1);
132-
t.equal(true, false, 'false should be true');
132+
t.equal(true, false, 'true should be false');
133+
});
134+
});
135+
136+
tap.test('preserves stack trace for failed assertions where actual===falsy', function (tt) {
137+
tt.plan(6);
138+
139+
var test = tape.createHarness();
140+
var stream = test.createStream();
141+
var parser = stream.pipe(tapParser());
142+
143+
var stack = ''
144+
parser.once('assert', function (data) {
145+
tt.equal(typeof data.diag.at, 'string');
146+
tt.equal(typeof data.diag.stack, 'string');
147+
at = data.diag.at || '';
148+
stack = data.diag.stack || '';
149+
tt.ok(/^Error: false should be true(\n at .+)+/.exec(stack), 'stack should be a stack')
150+
tt.deepEqual(data, {
151+
ok: false,
152+
id: 1,
153+
name: "false should be true",
154+
diag: {
155+
at: at,
156+
stack: stack,
157+
operator: 'equal',
158+
expected: true,
159+
actual: false
160+
}
161+
});
162+
});
163+
164+
stream.pipe(concat(function (body) {
165+
var body = body.toString('utf8');
166+
body = stripAt(body);
167+
tt.equal(
168+
body,
169+
'TAP version 13\n'
170+
+ '# t.equal stack trace\n'
171+
+ 'not ok 1 false should be true\n'
172+
+ ' ---\n'
173+
+ ' operator: equal\n'
174+
+ ' expected: true\n'
175+
+ ' actual: false\n'
176+
+ ' stack: |-\n'
177+
+ ' '
178+
+ stack.replace(/\n/g, '\n ') + '\n'
179+
+ ' ...\n'
180+
+ '\n'
181+
+ '1..1\n'
182+
+ '# tests 1\n'
183+
+ '# pass 0\n'
184+
+ '# fail 1\n'
185+
);
186+
187+
tt.deepEqual(getDiag(body), {
188+
stack: stack,
189+
operator: 'equal',
190+
expected: true,
191+
actual: false
192+
});
193+
}));
194+
195+
test('t.equal stack trace', function (t) {
196+
t.plan(1);
197+
t.equal(false, true, 'false should be true');
133198
});
134199
});
135200

0 commit comments

Comments
 (0)