From 56dc4b3848f66048cde665f874da16f97223858f Mon Sep 17 00:00:00 2001 From: Moshe Atlow Date: Sun, 21 Aug 2022 02:31:23 +0300 Subject: [PATCH] test_runner: fix test runner hooks failure stack PR-URL: https://github.com/nodejs/node/pull/44284 Reviewed-By: Antoine du Hamel Reviewed-By: Benjamin Gruenbaum --- lib/internal/test_runner/tap_stream.js | 8 ++- lib/internal/test_runner/test.js | 11 ++- test/message/test_runner_hooks.out | 96 ++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 4 deletions(-) diff --git a/lib/internal/test_runner/tap_stream.js b/lib/internal/test_runner/tap_stream.js index ca0c101aa9260c..124bbfc2d95565 100644 --- a/lib/internal/test_runner/tap_stream.js +++ b/lib/internal/test_runner/tap_stream.js @@ -167,7 +167,7 @@ function jsToYaml(indent, name, value) { } if (isErrorObj) { - const { kTestCodeFailure } = lazyLoadTest(); + const { kTestCodeFailure, kHookFailure } = lazyLoadTest(); const { cause, code, @@ -181,10 +181,12 @@ function jsToYaml(indent, name, value) { // If the ERR_TEST_FAILURE came from an error provided by user code, // then try to unwrap the original error message and stack. - if (code === 'ERR_TEST_FAILURE' && failureType === kTestCodeFailure) { - errMsg = cause?.message ?? errMsg; + if (code === 'ERR_TEST_FAILURE' && (failureType === kTestCodeFailure || failureType === kHookFailure)) { errStack = cause?.stack ?? errStack; errCode = cause?.code ?? errCode; + if (failureType === kTestCodeFailure) { + errMsg = cause?.message ?? errMsg; + } } result += jsToYaml(indent, 'error', errMsg); diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index 3ebb4bc0a2dff9..4f0f94d4d45974 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -691,4 +691,13 @@ class Suite extends Test { } } -module.exports = { kCancelledByParent, kDefaultIndent, kSubtestsFailed, kTestCodeFailure, Test, Suite, ItTest }; +module.exports = { + ItTest, + kCancelledByParent, + kDefaultIndent, + kHookFailure, + kSubtestsFailed, + kTestCodeFailure, + Suite, + Test, +}; diff --git a/test/message/test_runner_hooks.out b/test/message/test_runner_hooks.out index cd85fe9798a2a3..57008a44bbb141 100644 --- a/test/message/test_runner_hooks.out +++ b/test/message/test_runner_hooks.out @@ -55,6 +55,16 @@ not ok 2 - before throws failureType: 'hookFailed' error: 'failed running before hook' code: 'ERR_TEST_FAILURE' + stack: |- + * + * + * + * + * + * + * + * + * ... # Subtest: after throws # Subtest: 1 @@ -74,6 +84,16 @@ not ok 3 - after throws failureType: 'hookFailed' error: 'failed running after hook' code: 'ERR_TEST_FAILURE' + stack: |- + * + * + * + * + * + * + * + * + * ... # Subtest: beforeEach throws # Subtest: 1 @@ -85,6 +105,15 @@ not ok 3 - after throws code: 'ERR_TEST_FAILURE' stack: |- * + * + * + * + * + * + * + * + * + * ... # Subtest: 2 not ok 2 - 2 @@ -93,6 +122,17 @@ not ok 3 - after throws failureType: 'hookFailed' error: 'failed running beforeEach hook' code: 'ERR_TEST_FAILURE' + stack: |- + * + * + * + * + * + * + * + * + * + * ... 1..2 not ok 4 - beforeEach throws @@ -112,6 +152,15 @@ not ok 4 - beforeEach throws code: 'ERR_TEST_FAILURE' stack: |- * + * + * + * + * + * + * + * + * + * ... # Subtest: 2 not ok 2 - 2 @@ -120,6 +169,17 @@ not ok 4 - beforeEach throws failureType: 'hookFailed' error: 'failed running afterEach hook' code: 'ERR_TEST_FAILURE' + stack: |- + * + * + * + * + * + * + * + * + * + * ... 1..2 not ok 5 - afterEach throws @@ -171,6 +231,15 @@ ok 6 - test hooks code: 'ERR_TEST_FAILURE' stack: |- * + * + * + * + * + * + * + * + * + * ... # Subtest: 2 not ok 2 - 2 @@ -181,6 +250,15 @@ ok 6 - test hooks code: 'ERR_TEST_FAILURE' stack: |- * + * + * + * + * + * + * + * + * + * ... 1..2 not ok 7 - t.beforeEach throws @@ -200,6 +278,15 @@ not ok 7 - t.beforeEach throws code: 'ERR_TEST_FAILURE' stack: |- * + * + * + * + * + * + * + * + * + * ... # Subtest: 2 not ok 2 - 2 @@ -210,6 +297,15 @@ not ok 7 - t.beforeEach throws code: 'ERR_TEST_FAILURE' stack: |- * + * + * + * + * + * + * + * + * + * ... 1..2 not ok 8 - t.afterEach throws