Skip to content

Commit c886fc5

Browse files
committed
test_runner: fix test runner hooks failure stack
1 parent a2dcc08 commit c886fc5

3 files changed

Lines changed: 104 additions & 4 deletions

File tree

lib/internal/test_runner/tap_stream.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ function jsToYaml(indent, name, value) {
167167
}
168168

169169
if (isErrorObj) {
170-
const { kTestCodeFailure } = lazyLoadTest();
170+
const { kTestCodeFailure, kHookFailure } = lazyLoadTest();
171171
const {
172172
cause,
173173
code,
@@ -181,10 +181,12 @@ function jsToYaml(indent, name, value) {
181181

182182
// If the ERR_TEST_FAILURE came from an error provided by user code,
183183
// then try to unwrap the original error message and stack.
184-
if (code === 'ERR_TEST_FAILURE' && failureType === kTestCodeFailure) {
185-
errMsg = cause?.message ?? errMsg;
184+
if (code === 'ERR_TEST_FAILURE' && (failureType === kTestCodeFailure || failureType === kHookFailure)) {
186185
errStack = cause?.stack ?? errStack;
187186
errCode = cause?.code ?? errCode;
187+
if (failureType === kTestCodeFailure) {
188+
errMsg = cause?.message ?? errMsg;
189+
}
188190
}
189191

190192
result += jsToYaml(indent, 'error', errMsg);

lib/internal/test_runner/test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,4 +691,6 @@ class Suite extends Test {
691691
}
692692
}
693693

694-
module.exports = { kCancelledByParent, kDefaultIndent, kSubtestsFailed, kTestCodeFailure, Test, Suite, ItTest };
694+
module.exports = {
695+
kCancelledByParent, kDefaultIndent, kSubtestsFailed, kTestCodeFailure, kHookFailure, Test, Suite, ItTest,
696+
};

test/message/test_runner_hooks.out

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ not ok 2 - before throws
5555
failureType: 'hookFailed'
5656
error: 'failed running before hook'
5757
code: 'ERR_TEST_FAILURE'
58+
stack: |-
59+
Object.<anonymous> (*)
60+
TestHook.runInAsyncScope (*)
61+
TestHook.run (*)
62+
TestHook.run (*)
63+
*
64+
*
65+
*
66+
*
67+
*
5868
...
5969
# Subtest: after throws
6070
# Subtest: 1
@@ -74,6 +84,16 @@ not ok 3 - after throws
7484
failureType: 'hookFailed'
7585
error: 'failed running after hook'
7686
code: 'ERR_TEST_FAILURE'
87+
stack: |-
88+
Object.<anonymous> (*)
89+
TestHook.runInAsyncScope (*)
90+
TestHook.run (*)
91+
TestHook.run (*)
92+
*
93+
*
94+
*
95+
*
96+
*
7797
...
7898
# Subtest: beforeEach throws
7999
# Subtest: 1
@@ -84,6 +104,15 @@ not ok 3 - after throws
84104
error: 'failed running beforeEach hook'
85105
code: 'ERR_TEST_FAILURE'
86106
stack: |-
107+
Object.<anonymous> (*)
108+
TestHook.runInAsyncScope (*)
109+
TestHook.run (*)
110+
TestHook.run (*)
111+
*
112+
*
113+
*
114+
*
115+
*
87116
*
88117
...
89118
# Subtest: 2
@@ -93,6 +122,17 @@ not ok 3 - after throws
93122
failureType: 'hookFailed'
94123
error: 'failed running beforeEach hook'
95124
code: 'ERR_TEST_FAILURE'
125+
stack: |-
126+
Object.<anonymous> (*)
127+
TestHook.runInAsyncScope (*)
128+
TestHook.run (*)
129+
TestHook.run (*)
130+
*
131+
*
132+
*
133+
*
134+
*
135+
*
96136
...
97137
1..2
98138
not ok 4 - beforeEach throws
@@ -111,6 +151,15 @@ not ok 4 - beforeEach throws
111151
error: 'failed running afterEach hook'
112152
code: 'ERR_TEST_FAILURE'
113153
stack: |-
154+
Object.<anonymous> (*)
155+
TestHook.runInAsyncScope (*)
156+
TestHook.run (*)
157+
TestHook.run (*)
158+
*
159+
*
160+
*
161+
*
162+
*
114163
*
115164
...
116165
# Subtest: 2
@@ -120,6 +169,17 @@ not ok 4 - beforeEach throws
120169
failureType: 'hookFailed'
121170
error: 'failed running afterEach hook'
122171
code: 'ERR_TEST_FAILURE'
172+
stack: |-
173+
Object.<anonymous> (*)
174+
TestHook.runInAsyncScope (*)
175+
TestHook.run (*)
176+
TestHook.run (*)
177+
*
178+
*
179+
*
180+
*
181+
*
182+
*
123183
...
124184
1..2
125185
not ok 5 - afterEach throws
@@ -170,6 +230,15 @@ ok 6 - test hooks
170230
error: 'failed running beforeEach hook'
171231
code: 'ERR_TEST_FAILURE'
172232
stack: |-
233+
TestContext.<anonymous> (*)
234+
TestHook.runInAsyncScope (*)
235+
TestHook.run (*)
236+
TestHook.run (*)
237+
*
238+
*
239+
*
240+
*
241+
*
173242
*
174243
...
175244
# Subtest: 2
@@ -180,6 +249,15 @@ ok 6 - test hooks
180249
error: 'failed running beforeEach hook'
181250
code: 'ERR_TEST_FAILURE'
182251
stack: |-
252+
TestContext.<anonymous> (*)
253+
TestHook.runInAsyncScope (*)
254+
TestHook.run (*)
255+
TestHook.run (*)
256+
*
257+
*
258+
*
259+
*
260+
*
183261
*
184262
...
185263
1..2
@@ -199,6 +277,15 @@ not ok 7 - t.beforeEach throws
199277
error: 'failed running afterEach hook'
200278
code: 'ERR_TEST_FAILURE'
201279
stack: |-
280+
TestContext.<anonymous> (*)
281+
TestHook.runInAsyncScope (*)
282+
TestHook.run (*)
283+
TestHook.run (*)
284+
*
285+
*
286+
*
287+
*
288+
*
202289
*
203290
...
204291
# Subtest: 2
@@ -209,6 +296,15 @@ not ok 7 - t.beforeEach throws
209296
error: 'failed running afterEach hook'
210297
code: 'ERR_TEST_FAILURE'
211298
stack: |-
299+
TestContext.<anonymous> (*)
300+
TestHook.runInAsyncScope (*)
301+
TestHook.run (*)
302+
TestHook.run (*)
303+
*
304+
*
305+
*
306+
*
307+
*
212308
*
213309
...
214310
1..2

0 commit comments

Comments
 (0)