From 9f122be15ad489fdfa0f2d90dfba9f4cc1bb2379 Mon Sep 17 00:00:00 2001 From: Colin Ihrig Date: Sat, 29 Jul 2023 14:36:55 -0400 Subject: [PATCH] test_runner: unwrap error message in TAP reporter The TAP reporter unwraps errors that come from user code. It was not properly unwrapping the error message when the failure originated from a test hook. This lead to difficult to debug errors when TAP output was used. This commit updates the TAP reporter to properly unwrap the error message. Fixes: https://github.com/nodejs/node/issues/48941 PR-URL: https://github.com/nodejs/node/pull/48942 Reviewed-By: Chemi Atlow Reviewed-By: Moshe Atlow Reviewed-By: Matteo Collina Reviewed-By: Benjamin Gruenbaum --- lib/internal/test_runner/reporter/tap.js | 7 ++--- .../test-runner/output/hooks.snapshot | 28 +++++++++---------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/lib/internal/test_runner/reporter/tap.js b/lib/internal/test_runner/reporter/tap.js index 9c737efef35b96..de8188c58dd31e 100644 --- a/lib/internal/test_runner/reporter/tap.js +++ b/lib/internal/test_runner/reporter/tap.js @@ -171,7 +171,7 @@ function jsToYaml(indent, name, value, seen) { } if (isErrorObj) { - const { kTestCodeFailure, kUnwrapErrors } = lazyLoadTest(); + const { kUnwrapErrors } = lazyLoadTest(); const { cause, code, @@ -198,15 +198,14 @@ function jsToYaml(indent, name, value, seen) { errStack = cause?.stack ?? errStack; errCode = cause?.code ?? errCode; errName = cause?.name ?? errName; + errMsg = cause?.message ?? errMsg; + if (isAssertionLike(cause)) { errExpected = cause.expected; errActual = cause.actual; errOperator = cause.operator ?? errOperator; errIsAssertion = true; } - if (failureType === kTestCodeFailure) { - errMsg = cause?.message ?? errMsg; - } } result += jsToYaml(indent, 'error', errMsg, seen); diff --git a/test/fixtures/test-runner/output/hooks.snapshot b/test/fixtures/test-runner/output/hooks.snapshot index 5b16957ba24dc6..b9fd23640373de 100644 --- a/test/fixtures/test-runner/output/hooks.snapshot +++ b/test/fixtures/test-runner/output/hooks.snapshot @@ -56,7 +56,7 @@ not ok 2 - before throws duration_ms: * type: 'suite' failureType: 'hookFailed' - error: 'failed running before hook' + error: 'before' code: 'ERR_TEST_FAILURE' stack: |- * @@ -86,7 +86,7 @@ not ok 3 - after throws duration_ms: * type: 'suite' failureType: 'hookFailed' - error: 'failed running after hook' + error: 'after' code: 'ERR_TEST_FAILURE' stack: |- * @@ -105,7 +105,7 @@ not ok 3 - after throws --- duration_ms: * failureType: 'hookFailed' - error: 'failed running beforeEach hook' + error: 'beforeEach' code: 'ERR_TEST_FAILURE' stack: |- * @@ -124,7 +124,7 @@ not ok 3 - after throws --- duration_ms: * failureType: 'hookFailed' - error: 'failed running beforeEach hook' + error: 'beforeEach' code: 'ERR_TEST_FAILURE' stack: |- * @@ -153,7 +153,7 @@ not ok 4 - beforeEach throws --- duration_ms: * failureType: 'hookFailed' - error: 'failed running afterEach hook' + error: 'afterEach' code: 'ERR_TEST_FAILURE' stack: |- * @@ -172,7 +172,7 @@ not ok 4 - beforeEach throws --- duration_ms: * failureType: 'hookFailed' - error: 'failed running afterEach hook' + error: 'afterEach' code: 'ERR_TEST_FAILURE' stack: |- * @@ -254,7 +254,7 @@ not ok 6 - afterEach when test fails --- duration_ms: * failureType: 'hookFailed' - error: 'failed running afterEach hook' + error: 'afterEach' code: 'ERR_TEST_FAILURE' stack: |- * @@ -315,7 +315,7 @@ ok 8 - test hooks --- duration_ms: * failureType: 'hookFailed' - error: 'failed running before hook' + error: 'before' code: 'ERR_TEST_FAILURE' stack: |- * @@ -334,7 +334,7 @@ ok 8 - test hooks --- duration_ms: * failureType: 'hookFailed' - error: 'failed running before hook' + error: 'before' code: 'ERR_TEST_FAILURE' stack: |- * @@ -362,7 +362,7 @@ not ok 9 - t.before throws --- duration_ms: * failureType: 'hookFailed' - error: 'failed running beforeEach hook' + error: 'beforeEach' code: 'ERR_TEST_FAILURE' stack: |- * @@ -381,7 +381,7 @@ not ok 9 - t.before throws --- duration_ms: * failureType: 'hookFailed' - error: 'failed running beforeEach hook' + error: 'beforeEach' code: 'ERR_TEST_FAILURE' stack: |- * @@ -409,7 +409,7 @@ not ok 10 - t.beforeEach throws --- duration_ms: * failureType: 'hookFailed' - error: 'failed running afterEach hook' + error: 'afterEach' code: 'ERR_TEST_FAILURE' stack: |- * @@ -428,7 +428,7 @@ not ok 10 - t.beforeEach throws --- duration_ms: * failureType: 'hookFailed' - error: 'failed running afterEach hook' + error: 'afterEach' code: 'ERR_TEST_FAILURE' stack: |- * @@ -506,7 +506,7 @@ not ok 12 - afterEach when test fails --- duration_ms: * failureType: 'hookFailed' - error: 'failed running afterEach hook' + error: 'afterEach' code: 'ERR_TEST_FAILURE' stack: |- *