From 65df5a4f194cf01c3872c713d129ac968342181c Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Sat, 17 Sep 2022 21:38:27 -0700 Subject: [PATCH] [Tests] `stackTrace`: use the common `getDiag` utility --- test/common.js | 6 ++-- test/stackTrace.js | 90 ++++++++++++++++++++-------------------------- 2 files changed, 43 insertions(+), 53 deletions(-) diff --git a/test/common.js b/test/common.js index c5b9241b..a30302aa 100644 --- a/test/common.js +++ b/test/common.js @@ -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) { @@ -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; }; diff --git a/test/stackTrace.js b/test/stackTrace.js index b55afff4..0d0723e8 100644 --- a/test/stackTrace.js +++ b/test/stackTrace.js @@ -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); @@ -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', @@ -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, @@ -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, @@ -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, ''); }