11import { nanoid } from 'nanoid/non-secure'
2- import { defaultTestTimeout , defaultHookTimeout } from '../constants'
32import type { SuiteHooks , Test , SuiteCollector , TestCollector , RunMode , ComputeMode , TestFactory , TestFunction , File , Suite , Awaitable } from '../types'
43import { context } from './context'
54import { getHooks , setFn , setHooks } from './map'
@@ -12,6 +11,14 @@ function getCurrentSuite() {
1211 return context . currentSuite || defaultSuite
1312}
1413
14+ const getDefaultTestTimeout = ( ) => {
15+ return process . __vitest_worker__ ?. config ?. testTimeout ?? 5000
16+ }
17+
18+ const getDefaultHookTimeout = ( ) => {
19+ return process . __vitest_worker__ ?. config ?. hookTimeout ?? 5000
20+ }
21+
1522export function createSuiteHooks ( ) {
1623 return {
1724 beforeAll : [ ] ,
@@ -209,10 +216,10 @@ export const describe = suite
209216export const it = test
210217
211218// hooks
212- export const beforeAll = ( fn : SuiteHooks [ 'beforeAll' ] [ 0 ] , timeout = defaultHookTimeout ) => getCurrentSuite ( ) . on ( 'beforeAll' , withTimeout ( fn , timeout ) )
213- export const afterAll = ( fn : SuiteHooks [ 'afterAll' ] [ 0 ] , timeout = defaultHookTimeout ) => getCurrentSuite ( ) . on ( 'afterAll' , withTimeout ( fn , timeout ) )
214- export const beforeEach = ( fn : SuiteHooks [ 'beforeEach' ] [ 0 ] , timeout = defaultHookTimeout ) => getCurrentSuite ( ) . on ( 'beforeEach' , withTimeout ( fn , timeout ) )
215- export const afterEach = ( fn : SuiteHooks [ 'afterEach' ] [ 0 ] , timeout = defaultHookTimeout ) => getCurrentSuite ( ) . on ( 'afterEach' , withTimeout ( fn , timeout ) )
219+ export const beforeAll = ( fn : SuiteHooks [ 'beforeAll' ] [ 0 ] , timeout ?: number ) => getCurrentSuite ( ) . on ( 'beforeAll' , withTimeout ( fn , timeout ?? getDefaultHookTimeout ( ) ) )
220+ export const afterAll = ( fn : SuiteHooks [ 'afterAll' ] [ 0 ] , timeout ?: number ) => getCurrentSuite ( ) . on ( 'afterAll' , withTimeout ( fn , timeout ?? getDefaultHookTimeout ( ) ) )
221+ export const beforeEach = ( fn : SuiteHooks [ 'beforeEach' ] [ 0 ] , timeout ?: number ) => getCurrentSuite ( ) . on ( 'beforeEach' , withTimeout ( fn , timeout ?? getDefaultHookTimeout ( ) ) )
222+ export const afterEach = ( fn : SuiteHooks [ 'afterEach' ] [ 0 ] , timeout ?: number ) => getCurrentSuite ( ) . on ( 'afterEach' , withTimeout ( fn , timeout ?? getDefaultHookTimeout ( ) ) )
216223
217224// utils
218225export function clearContext ( ) {
@@ -221,7 +228,8 @@ export function clearContext() {
221228 context . currentSuite = defaultSuite
222229}
223230
224- function withTimeout < T extends ( ( ...args : any [ ] ) => any ) > ( fn : T , timeout = defaultTestTimeout ) : T {
231+ function withTimeout < T extends ( ( ...args : any [ ] ) => any ) > ( fn : T , _timeout ?: number ) : T {
232+ const timeout = _timeout ?? getDefaultTestTimeout ( )
225233 if ( timeout <= 0 || timeout === Infinity )
226234 return fn
227235
0 commit comments