Skip to content

Commit c0b160c

Browse files
MoLowruyadorno
authored andcommitted
test_runner: fix test runner hooks failure stack
PR-URL: #44284 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent a933712 commit c0b160c

File tree

3 files changed

+111
-4
lines changed

3 files changed

+111
-4
lines changed

lib/internal/test_runner/tap_stream.js

+5-3
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

+10-1
Original file line numberDiff line numberDiff line change
@@ -691,4 +691,13 @@ class Suite extends Test {
691691
}
692692
}
693693

694-
module.exports = { kCancelledByParent, kDefaultIndent, kSubtestsFailed, kTestCodeFailure, Test, Suite, ItTest };
694+
module.exports = {
695+
ItTest,
696+
kCancelledByParent,
697+
kDefaultIndent,
698+
kHookFailure,
699+
kSubtestsFailed,
700+
kTestCodeFailure,
701+
Suite,
702+
Test,
703+
};

test/message/test_runner_hooks.out

+96
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+
*
60+
*
61+
*
62+
*
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+
*
89+
*
90+
*
91+
*
92+
*
93+
*
94+
*
95+
*
96+
*
7797
...
7898
# Subtest: beforeEach throws
7999
# Subtest: 1
@@ -85,6 +105,15 @@ not ok 3 - after throws
85105
code: 'ERR_TEST_FAILURE'
86106
stack: |-
87107
*
108+
*
109+
*
110+
*
111+
*
112+
*
113+
*
114+
*
115+
*
116+
*
88117
...
89118
# Subtest: 2
90119
not ok 2 - 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+
*
127+
*
128+
*
129+
*
130+
*
131+
*
132+
*
133+
*
134+
*
135+
*
96136
...
97137
1..2
98138
not ok 4 - beforeEach throws
@@ -112,6 +152,15 @@ not ok 4 - beforeEach throws
112152
code: 'ERR_TEST_FAILURE'
113153
stack: |-
114154
*
155+
*
156+
*
157+
*
158+
*
159+
*
160+
*
161+
*
162+
*
163+
*
115164
...
116165
# Subtest: 2
117166
not ok 2 - 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+
*
174+
*
175+
*
176+
*
177+
*
178+
*
179+
*
180+
*
181+
*
182+
*
123183
...
124184
1..2
125185
not ok 5 - afterEach throws
@@ -171,6 +231,15 @@ ok 6 - test hooks
171231
code: 'ERR_TEST_FAILURE'
172232
stack: |-
173233
*
234+
*
235+
*
236+
*
237+
*
238+
*
239+
*
240+
*
241+
*
242+
*
174243
...
175244
# Subtest: 2
176245
not ok 2 - 2
@@ -181,6 +250,15 @@ ok 6 - test hooks
181250
code: 'ERR_TEST_FAILURE'
182251
stack: |-
183252
*
253+
*
254+
*
255+
*
256+
*
257+
*
258+
*
259+
*
260+
*
261+
*
184262
...
185263
1..2
186264
not ok 7 - t.beforeEach throws
@@ -200,6 +278,15 @@ not ok 7 - t.beforeEach throws
200278
code: 'ERR_TEST_FAILURE'
201279
stack: |-
202280
*
281+
*
282+
*
283+
*
284+
*
285+
*
286+
*
287+
*
288+
*
289+
*
203290
...
204291
# Subtest: 2
205292
not ok 2 - 2
@@ -210,6 +297,15 @@ not ok 7 - t.beforeEach throws
210297
code: 'ERR_TEST_FAILURE'
211298
stack: |-
212299
*
300+
*
301+
*
302+
*
303+
*
304+
*
305+
*
306+
*
307+
*
308+
*
213309
...
214310
1..2
215311
not ok 8 - t.afterEach throws

0 commit comments

Comments
 (0)