Skip to content

Commit 65df5a4

Browse files
committed
[Tests] stackTrace: use the common getDiag utility
1 parent b035590 commit 65df5a4

File tree

2 files changed

+43
-53
lines changed

2 files changed

+43
-53
lines changed

test/common.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
var path = require('path');
44
var yaml = require('js-yaml');
55

6-
module.exports.getDiag = function (body) {
6+
module.exports.getDiag = function (body, includeStack) {
77
var yamlStart = body.indexOf(' ---');
88
var yamlEnd = body.indexOf(' ...\n');
99
var diag = body.slice(yamlStart, yamlEnd).split('\n').map(function (line) {
@@ -13,7 +13,9 @@ module.exports.getDiag = function (body) {
1313
// The stack trace and at variable will vary depending on where the code
1414
// is run, so just strip it out.
1515
var withStack = yaml.safeLoad(diag);
16-
delete withStack.stack;
16+
if (!includeStack) {
17+
delete withStack.stack;
18+
}
1719
delete withStack.at;
1820
return withStack;
1921
};

test/stackTrace.js

Lines changed: 39 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ var tape = require('../');
44
var tap = require('tap');
55
var concat = require('concat-stream');
66
var tapParser = require('tap-parser');
7-
var yaml = require('js-yaml');
7+
var common = require('./common');
8+
9+
var getDiag = common.getDiag;
810

911
tap.test('preserves stack trace with newlines', function (tt) {
1012
tt.plan(3);
@@ -34,26 +36,26 @@ tap.test('preserves stack trace with newlines', function (tt) {
3436
tt.equal(
3537
strippedBody,
3638
'TAP version 13\n'
37-
+ '# multiline stack trace\n'
38-
+ 'not ok 1 Error: Preserve stack\n'
39-
+ ' ---\n'
40-
+ ' operator: error\n'
41-
+ ' expected: |-\n'
42-
+ ' undefined\n'
43-
+ ' actual: |-\n'
44-
+ ' [Error: Preserve stack]\n'
45-
+ ' stack: |-\n'
46-
+ ' foo\n'
47-
+ ' bar\n'
48-
+ ' ...\n'
49-
+ '\n'
50-
+ '1..1\n'
51-
+ '# tests 1\n'
52-
+ '# pass 0\n'
53-
+ '# fail 1\n'
39+
+ '# multiline stack trace\n'
40+
+ 'not ok 1 Error: Preserve stack\n'
41+
+ ' ---\n'
42+
+ ' operator: error\n'
43+
+ ' expected: |-\n'
44+
+ ' undefined\n'
45+
+ ' actual: |-\n'
46+
+ ' [Error: Preserve stack]\n'
47+
+ ' stack: |-\n'
48+
+ ' foo\n'
49+
+ ' bar\n'
50+
+ ' ...\n'
51+
+ '\n'
52+
+ '1..1\n'
53+
+ '# tests 1\n'
54+
+ '# pass 0\n'
55+
+ '# fail 1\n'
5456
);
5557

56-
tt.deepEqual(getDiag(strippedBody), {
58+
tt.deepEqual(getDiag(strippedBody, true), {
5759
stack: stackTrace,
5860
operator: 'error',
5961
expected: 'undefined',
@@ -198,25 +200,25 @@ tap.test('preserves stack trace for failed assertions', function (tt) {
198200
tt.equal(
199201
strippedBody,
200202
'TAP version 13\n'
201-
+ '# t.equal stack trace\n'
202-
+ 'not ok 1 true should be false\n'
203-
+ ' ---\n'
204-
+ ' operator: equal\n'
205-
+ ' expected: false\n'
206-
+ ' actual: true\n'
207-
+ ' stack: |-\n'
208-
+ ' '
209-
+ stack.replace(/\n/g, '\n ')
210-
+ '\n'
211-
+ ' ...\n'
212-
+ '\n'
213-
+ '1..1\n'
214-
+ '# tests 1\n'
215-
+ '# pass 0\n'
216-
+ '# fail 1\n'
203+
+ '# t.equal stack trace\n'
204+
+ 'not ok 1 true should be false\n'
205+
+ ' ---\n'
206+
+ ' operator: equal\n'
207+
+ ' expected: false\n'
208+
+ ' actual: true\n'
209+
+ ' stack: |-\n'
210+
+ ' '
211+
+ stack.replace(/\n/g, '\n ')
212+
+ '\n'
213+
+ ' ...\n'
214+
+ '\n'
215+
+ '1..1\n'
216+
+ '# tests 1\n'
217+
+ '# pass 0\n'
218+
+ '# fail 1\n'
217219
);
218220

219-
tt.deepEqual(getDiag(strippedBody), {
221+
tt.deepEqual(getDiag(strippedBody, true), {
220222
stack: stack,
221223
operator: 'equal',
222224
expected: false,
@@ -281,7 +283,7 @@ tap.test('preserves stack trace for failed assertions where actual===falsy', fun
281283
+ '# fail 1\n'
282284
);
283285

284-
tt.deepEqual(getDiag(strippedBody), {
286+
tt.deepEqual(getDiag(strippedBody, true), {
285287
stack: stack,
286288
operator: 'equal',
287289
expected: true,
@@ -295,20 +297,6 @@ tap.test('preserves stack trace for failed assertions where actual===falsy', fun
295297
});
296298
});
297299

298-
function getDiag(body) {
299-
var yamlStart = body.indexOf(' ---');
300-
var yamlEnd = body.indexOf(' ...\n');
301-
var diag = body.slice(yamlStart, yamlEnd).split('\n').map(function (line) {
302-
return line.slice(2);
303-
}).join('\n');
304-
305-
// Get rid of 'at' variable (which has a line number / path of its own that's
306-
// difficult to check).
307-
var withStack = yaml.safeLoad(diag);
308-
delete withStack.at;
309-
return withStack;
310-
}
311-
312300
function stripAt(body) {
313301
return body.replace(/^\s*at:\s+Test.*$\n/m, '');
314302
}

0 commit comments

Comments
 (0)