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 2151e06 commit 298cb80
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 19 deletions.
6 changes: 4 additions & 2 deletions test/common.js
Expand Up @@ -5,7 +5,7 @@ var spawn = require('child_process').spawn;
var concat = require('concat-stream');
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 @@ -15,7 +15,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
23 changes: 6 additions & 17 deletions test/stackTrace.js
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 @@ -47,7 +49,7 @@ tap.test('preserves stack trace with newlines', function (tt) {
''
]);

tt.deepEqual(getDiag(strippedBody), {
tt.deepEqual(getDiag(strippedBody, true), {
stack: stackTrace,
operator: 'error'
});
Expand Down Expand Up @@ -206,7 +208,7 @@ tap.test('preserves stack trace for failed assertions', function (tt) {
''
));

tt.deepEqual(getDiag(strippedBody), {
tt.deepEqual(getDiag(strippedBody, true), {
stack: stack,
operator: 'equal',
expected: false,
Expand Down Expand Up @@ -269,7 +271,7 @@ tap.test('preserves stack trace for failed assertions where actual===falsy', fun
''
));

tt.deepEqual(getDiag(strippedBody), {
tt.deepEqual(getDiag(strippedBody, true), {
stack: stack,
operator: 'equal',
expected: true,
Expand All @@ -283,19 +285,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 298cb80

Please sign in to comment.