From 298cb806ef4a438792cb533675b6faa4b02a1d92 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 | 23 ++++++----------------- 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/test/common.js b/test/common.js index 5856cf3d..f2098225 100644 --- a/test/common.js +++ b/test/common.js @@ -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) { @@ -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; }; diff --git a/test/stackTrace.js b/test/stackTrace.js index 89829e05..a761d767 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); @@ -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' }); @@ -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, @@ -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, @@ -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, ''); }