Skip to content

Commit

Permalink
test_runner: fix test runner hooks failure stack
Browse files Browse the repository at this point in the history
  • Loading branch information
MoLow committed Aug 18, 2022
1 parent a2dcc08 commit c886fc5
Show file tree
Hide file tree
Showing 3 changed files with 104 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
4 changes: 3 additions & 1 deletion lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -691,4 +691,6 @@ class Suite extends Test {
}
}

module.exports = { kCancelledByParent, kDefaultIndent, kSubtestsFailed, kTestCodeFailure, Test, Suite, ItTest };
module.exports = {
kCancelledByParent, kDefaultIndent, kSubtestsFailed, kTestCodeFailure, kHookFailure, Test, Suite, ItTest,
};
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: |-
Object.<anonymous> (*)
TestHook.runInAsyncScope (*)
TestHook.run (*)
TestHook.run (*)
*
*
*
*
*
...
# 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: |-
Object.<anonymous> (*)
TestHook.runInAsyncScope (*)
TestHook.run (*)
TestHook.run (*)
*
*
*
*
*
...
# Subtest: beforeEach throws
# Subtest: 1
Expand All @@ -84,6 +104,15 @@ not ok 3 - after throws
error: 'failed running beforeEach hook'
code: 'ERR_TEST_FAILURE'
stack: |-
Object.<anonymous> (*)
TestHook.runInAsyncScope (*)
TestHook.run (*)
TestHook.run (*)
*
*
*
*
*
*
...
# Subtest: 2
Expand All @@ -93,6 +122,17 @@ not ok 3 - after throws
failureType: 'hookFailed'
error: 'failed running beforeEach hook'
code: 'ERR_TEST_FAILURE'
stack: |-
Object.<anonymous> (*)
TestHook.runInAsyncScope (*)
TestHook.run (*)
TestHook.run (*)
*
*
*
*
*
*
...
1..2
not ok 4 - beforeEach throws
Expand All @@ -111,6 +151,15 @@ not ok 4 - beforeEach throws
error: 'failed running afterEach hook'
code: 'ERR_TEST_FAILURE'
stack: |-
Object.<anonymous> (*)
TestHook.runInAsyncScope (*)
TestHook.run (*)
TestHook.run (*)
*
*
*
*
*
*
...
# Subtest: 2
Expand All @@ -120,6 +169,17 @@ not ok 4 - beforeEach throws
failureType: 'hookFailed'
error: 'failed running afterEach hook'
code: 'ERR_TEST_FAILURE'
stack: |-
Object.<anonymous> (*)
TestHook.runInAsyncScope (*)
TestHook.run (*)
TestHook.run (*)
*
*
*
*
*
*
...
1..2
not ok 5 - afterEach throws
Expand Down Expand Up @@ -170,6 +230,15 @@ ok 6 - test hooks
error: 'failed running beforeEach hook'
code: 'ERR_TEST_FAILURE'
stack: |-
TestContext.<anonymous> (*)
TestHook.runInAsyncScope (*)
TestHook.run (*)
TestHook.run (*)
*
*
*
*
*
*
...
# Subtest: 2
Expand All @@ -180,6 +249,15 @@ ok 6 - test hooks
error: 'failed running beforeEach hook'
code: 'ERR_TEST_FAILURE'
stack: |-
TestContext.<anonymous> (*)
TestHook.runInAsyncScope (*)
TestHook.run (*)
TestHook.run (*)
*
*
*
*
*
*
...
1..2
Expand All @@ -199,6 +277,15 @@ not ok 7 - t.beforeEach throws
error: 'failed running afterEach hook'
code: 'ERR_TEST_FAILURE'
stack: |-
TestContext.<anonymous> (*)
TestHook.runInAsyncScope (*)
TestHook.run (*)
TestHook.run (*)
*
*
*
*
*
*
...
# Subtest: 2
Expand All @@ -209,6 +296,15 @@ not ok 7 - t.beforeEach throws
error: 'failed running afterEach hook'
code: 'ERR_TEST_FAILURE'
stack: |-
TestContext.<anonymous> (*)
TestHook.runInAsyncScope (*)
TestHook.run (*)
TestHook.run (*)
*
*
*
*
*
*
...
1..2
Expand Down

0 comments on commit c886fc5

Please sign in to comment.