Skip to content

Commit 68ed9df

Browse files
authored
feat(options): defaultTimeout (#85)
1 parent 4742496 commit 68ed9df

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed

src/constants.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ import { fileURLToPath } from 'url'
33

44
export const distDir = resolve(fileURLToPath(import.meta.url), '../../dist')
55

6-
// TODO: make then globally configureable in vitest.config.js
7-
export const defaultTestTimeout = 5000
8-
export const defaultHookTimeout = 5000
9-
106
export const defaultIncludes = ['**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}']
117
export const defaultExcludes = ['**/node_modules/**', '**/dist/**']
128

src/runtime/suite.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { nanoid } from 'nanoid/non-secure'
2-
import { defaultTestTimeout, defaultHookTimeout } from '../constants'
32
import type { SuiteHooks, Test, SuiteCollector, TestCollector, RunMode, ComputeMode, TestFactory, TestFunction, File, Suite, Awaitable } from '../types'
43
import { context } from './context'
54
import { 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+
1522
export function createSuiteHooks() {
1623
return {
1724
beforeAll: [],
@@ -209,10 +216,10 @@ export const describe = suite
209216
export 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
218225
export 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

src/types/options.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,20 @@ export interface UserOptions {
103103
* @default true
104104
*/
105105
interpretDefault?: boolean
106+
107+
/*
108+
* Default timeout of a test in milliseconds
109+
*
110+
* @default 5000
111+
*/
112+
testTimeout?: number
113+
114+
/*
115+
* Default timeout of a hook in milliseconds
116+
*
117+
* @default 5000
118+
*/
119+
hookTimeout?: number
106120
}
107121

108122
export interface CliOptions extends UserOptions {

test/core/vitest.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { defineConfig } from 'vite'
22

33
export default defineConfig({
44
test: {
5+
testTimeout: 1000,
56
// threads: false,
67
},
78
})

0 commit comments

Comments
 (0)