Skip to content

Commit

Permalink
[Tests] stackTrace: use the common getDiag utility
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Sep 18, 2022
1 parent b035590 commit 65df5a4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 53 deletions.
6 changes: 4 additions & 2 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var path = require('path');
var yaml = require('js-yaml');

module.exports.getDiag = function (body) {
module.exports.getDiag = function (body, includeStack) {
var yamlStart = body.indexOf(' ---');
var yamlEnd = body.indexOf(' ...\n');
var diag = body.slice(yamlStart, yamlEnd).split('\n').map(function (line) {
Expand All @@ -13,7 +13,9 @@ module.exports.getDiag = function (body) {
// The stack trace and at variable will vary depending on where the code
// is run, so just strip it out.
var withStack = yaml.safeLoad(diag);
delete withStack.stack;
if (!includeStack) {
delete withStack.stack;
}
delete withStack.at;
return withStack;
};
Expand Down
90 changes: 39 additions & 51 deletions test/stackTrace.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ var tape = require('../');
var tap = require('tap');
var concat = require('concat-stream');
var tapParser = require('tap-parser');
var yaml = require('js-yaml');
var common = require('./common');

var getDiag = common.getDiag;

tap.test('preserves stack trace with newlines', function (tt) {
tt.plan(3);
Expand Down Expand Up @@ -34,26 +36,26 @@ tap.test('preserves stack trace with newlines', function (tt) {
tt.equal(
strippedBody,
'TAP version 13\n'
+ '# multiline stack trace\n'
+ 'not ok 1 Error: Preserve stack\n'
+ ' ---\n'
+ ' operator: error\n'
+ ' expected: |-\n'
+ ' undefined\n'
+ ' actual: |-\n'
+ ' [Error: Preserve stack]\n'
+ ' stack: |-\n'
+ ' foo\n'
+ ' bar\n'
+ ' ...\n'
+ '\n'
+ '1..1\n'
+ '# tests 1\n'
+ '# pass 0\n'
+ '# fail 1\n'
+ '# multiline stack trace\n'
+ 'not ok 1 Error: Preserve stack\n'
+ ' ---\n'
+ ' operator: error\n'
+ ' expected: |-\n'
+ ' undefined\n'
+ ' actual: |-\n'
+ ' [Error: Preserve stack]\n'
+ ' stack: |-\n'
+ ' foo\n'
+ ' bar\n'
+ ' ...\n'
+ '\n'
+ '1..1\n'
+ '# tests 1\n'
+ '# pass 0\n'
+ '# fail 1\n'
);

tt.deepEqual(getDiag(strippedBody), {
tt.deepEqual(getDiag(strippedBody, true), {
stack: stackTrace,
operator: 'error',
expected: 'undefined',
Expand Down Expand Up @@ -198,25 +200,25 @@ tap.test('preserves stack trace for failed assertions', function (tt) {
tt.equal(
strippedBody,
'TAP version 13\n'
+ '# t.equal stack trace\n'
+ 'not ok 1 true should be false\n'
+ ' ---\n'
+ ' operator: equal\n'
+ ' expected: false\n'
+ ' actual: true\n'
+ ' stack: |-\n'
+ ' '
+ stack.replace(/\n/g, '\n ')
+ '\n'
+ ' ...\n'
+ '\n'
+ '1..1\n'
+ '# tests 1\n'
+ '# pass 0\n'
+ '# fail 1\n'
+ '# t.equal stack trace\n'
+ 'not ok 1 true should be false\n'
+ ' ---\n'
+ ' operator: equal\n'
+ ' expected: false\n'
+ ' actual: true\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(strippedBody), {
tt.deepEqual(getDiag(strippedBody, true), {
stack: stack,
operator: 'equal',
expected: false,
Expand Down Expand Up @@ -281,7 +283,7 @@ tap.test('preserves stack trace for failed assertions where actual===falsy', fun
+ '# fail 1\n'
);

tt.deepEqual(getDiag(strippedBody), {
tt.deepEqual(getDiag(strippedBody, true), {
stack: stack,
operator: 'equal',
expected: true,
Expand All @@ -295,20 +297,6 @@ tap.test('preserves stack trace for failed assertions where actual===falsy', fun
});
});

function getDiag(body) {
var yamlStart = body.indexOf(' ---');
var yamlEnd = body.indexOf(' ...\n');
var diag = body.slice(yamlStart, yamlEnd).split('\n').map(function (line) {
return line.slice(2);
}).join('\n');

// Get rid of 'at' variable (which has a line number / path of its own that's
// difficult to check).
var withStack = yaml.safeLoad(diag);
delete withStack.at;
return withStack;
}

function stripAt(body) {
return body.replace(/^\s*at:\s+Test.*$\n/m, '');
}

0 comments on commit 65df5a4

Please sign in to comment.