@@ -19,14 +19,19 @@ function getDefaultHookTimeout() {
19
19
}
20
20
21
21
const CLEANUP_TIMEOUT_KEY = Symbol . for ( 'VITEST_CLEANUP_TIMEOUT' )
22
+ const CLEANUP_STACK_TRACE_KEY = Symbol . for ( 'VITEST_CLEANUP_STACK_TRACE' )
22
23
23
24
export function getBeforeHookCleanupCallback ( hook : Function , result : any ) : Function | undefined {
24
25
if ( typeof result === 'function' ) {
25
26
const timeout
26
27
= CLEANUP_TIMEOUT_KEY in hook && typeof hook [ CLEANUP_TIMEOUT_KEY ] === 'number'
27
28
? hook [ CLEANUP_TIMEOUT_KEY ]
28
29
: getDefaultHookTimeout ( )
29
- return withTimeout ( result , timeout , true )
30
+ const stackTraceError
31
+ = CLEANUP_STACK_TRACE_KEY in hook && hook [ CLEANUP_STACK_TRACE_KEY ] instanceof Error
32
+ ? hook [ CLEANUP_STACK_TRACE_KEY ]
33
+ : undefined
34
+ return withTimeout ( result , timeout , true , stackTraceError )
30
35
}
31
36
}
32
37
@@ -52,9 +57,21 @@ export function beforeAll(
52
57
timeout : number = getDefaultHookTimeout ( ) ,
53
58
) : void {
54
59
assertTypes ( fn , '"beforeAll" callback' , [ 'function' ] )
60
+ const stackTraceError = new Error ( 'STACK_TRACE_ERROR' )
55
61
return getCurrentSuite ( ) . on (
56
62
'beforeAll' ,
57
- Object . assign ( withTimeout ( fn , timeout , true ) , { [ CLEANUP_TIMEOUT_KEY ] : timeout } ) ,
63
+ Object . assign (
64
+ withTimeout (
65
+ fn ,
66
+ timeout ,
67
+ true ,
68
+ stackTraceError ,
69
+ ) ,
70
+ {
71
+ [ CLEANUP_TIMEOUT_KEY ] : timeout ,
72
+ [ CLEANUP_STACK_TRACE_KEY ] : stackTraceError ,
73
+ } ,
74
+ ) ,
58
75
)
59
76
}
60
77
@@ -79,7 +96,12 @@ export function afterAll(fn: AfterAllListener, timeout?: number): void {
79
96
assertTypes ( fn , '"afterAll" callback' , [ 'function' ] )
80
97
return getCurrentSuite ( ) . on (
81
98
'afterAll' ,
82
- withTimeout ( fn , timeout ?? getDefaultHookTimeout ( ) , true ) ,
99
+ withTimeout (
100
+ fn ,
101
+ timeout ?? getDefaultHookTimeout ( ) ,
102
+ true ,
103
+ new Error ( 'STACK_TRACE_ERROR' ) ,
104
+ ) ,
83
105
)
84
106
}
85
107
@@ -105,11 +127,20 @@ export function beforeEach<ExtraContext = object>(
105
127
timeout : number = getDefaultHookTimeout ( ) ,
106
128
) : void {
107
129
assertTypes ( fn , '"beforeEach" callback' , [ 'function' ] )
130
+ const stackTraceError = new Error ( 'STACK_TRACE_ERROR' )
108
131
return getCurrentSuite < ExtraContext > ( ) . on (
109
132
'beforeEach' ,
110
133
Object . assign (
111
- withTimeout ( withFixtures ( fn ) , timeout ?? getDefaultHookTimeout ( ) , true ) ,
112
- { [ CLEANUP_TIMEOUT_KEY ] : timeout } ,
134
+ withTimeout (
135
+ withFixtures ( fn ) ,
136
+ timeout ?? getDefaultHookTimeout ( ) ,
137
+ true ,
138
+ stackTraceError ,
139
+ ) ,
140
+ {
141
+ [ CLEANUP_TIMEOUT_KEY ] : timeout ,
142
+ [ CLEANUP_STACK_TRACE_KEY ] : stackTraceError ,
143
+ } ,
113
144
) ,
114
145
)
115
146
}
@@ -138,7 +169,12 @@ export function afterEach<ExtraContext = object>(
138
169
assertTypes ( fn , '"afterEach" callback' , [ 'function' ] )
139
170
return getCurrentSuite < ExtraContext > ( ) . on (
140
171
'afterEach' ,
141
- withTimeout ( withFixtures ( fn ) , timeout ?? getDefaultHookTimeout ( ) , true ) ,
172
+ withTimeout (
173
+ withFixtures ( fn ) ,
174
+ timeout ?? getDefaultHookTimeout ( ) ,
175
+ true ,
176
+ new Error ( 'STACK_TRACE_ERROR' ) ,
177
+ ) ,
142
178
)
143
179
}
144
180
@@ -165,7 +201,12 @@ export const onTestFailed: TaskHook<OnTestFailedHandler> = createTestHook(
165
201
( test , handler , timeout ) => {
166
202
test . onFailed ||= [ ]
167
203
test . onFailed . push (
168
- withTimeout ( handler , timeout ?? getDefaultHookTimeout ( ) , true ) ,
204
+ withTimeout (
205
+ handler ,
206
+ timeout ?? getDefaultHookTimeout ( ) ,
207
+ true ,
208
+ new Error ( 'STACK_TRACE_ERROR' ) ,
209
+ ) ,
169
210
)
170
211
} ,
171
212
)
@@ -198,7 +239,12 @@ export const onTestFinished: TaskHook<OnTestFinishedHandler> = createTestHook(
198
239
( test , handler , timeout ) => {
199
240
test . onFinished ||= [ ]
200
241
test . onFinished . push (
201
- withTimeout ( handler , timeout ?? getDefaultHookTimeout ( ) , true ) ,
242
+ withTimeout (
243
+ handler ,
244
+ timeout ?? getDefaultHookTimeout ( ) ,
245
+ true ,
246
+ new Error ( 'STACK_TRACE_ERROR' ) ,
247
+ ) ,
202
248
)
203
249
} ,
204
250
)
0 commit comments