Skip to content

Commit

Permalink
test_runner: fix test runner hooks failure stack
Browse files Browse the repository at this point in the history
PR-URL: #44284
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
MoLow committed Aug 20, 2022
1 parent 1ecc6c0 commit 0d46cf6
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 4 deletions.
8 changes: 5 additions & 3 deletions lib/internal/test_runner/tap_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ function jsToYaml(indent, name, value) {
}

if (isErrorObj) {
const { kTestCodeFailure } = lazyLoadTest();
const { kTestCodeFailure, kHookFailure } = lazyLoadTest();
const {
cause,
code,
Expand All @@ -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);
Expand Down
11 changes: 10 additions & 1 deletion lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
96 changes: 96 additions & 0 deletions test/message/test_runner_hooks.out
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -85,6 +105,15 @@ not ok 3 - after throws
code: 'ERR_TEST_FAILURE'
stack: |-
*
*
*
*
*
*
*
*
*
*
...
# Subtest: 2
not ok 2 - 2
Expand All @@ -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
Expand All @@ -112,6 +152,15 @@ not ok 4 - beforeEach throws
code: 'ERR_TEST_FAILURE'
stack: |-
*
*
*
*
*
*
*
*
*
*
...
# Subtest: 2
not ok 2 - 2
Expand All @@ -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
Expand Down Expand Up @@ -171,6 +231,15 @@ ok 6 - test hooks
code: 'ERR_TEST_FAILURE'
stack: |-
*
*
*
*
*
*
*
*
*
*
...
# Subtest: 2
not ok 2 - 2
Expand All @@ -181,6 +250,15 @@ ok 6 - test hooks
code: 'ERR_TEST_FAILURE'
stack: |-
*
*
*
*
*
*
*
*
*
*
...
1..2
not ok 7 - t.beforeEach throws
Expand All @@ -200,6 +278,15 @@ not ok 7 - t.beforeEach throws
code: 'ERR_TEST_FAILURE'
stack: |-
*
*
*
*
*
*
*
*
*
*
...
# Subtest: 2
not ok 2 - 2
Expand All @@ -210,6 +297,15 @@ not ok 7 - t.beforeEach throws
code: 'ERR_TEST_FAILURE'
stack: |-
*
*
*
*
*
*
*
*
*
*
...
1..2
not ok 8 - t.afterEach throws
Expand Down

0 comments on commit 0d46cf6

Please sign in to comment.