| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,6 +18,18 @@ function getDefaultHookTimeout() { | |||
| 18 | 18 | return getRunner().config.hookTimeout | |
| 19 | 19 | } | |
| 20 | 20 | ||
| 21 | + const CLEANUP_TIMEOUT_KEY = Symbol.for('VITEST_CLEANUP_TIMEOUT') | ||
| 22 | + | ||
| 23 | + export function getBeforeHookCleanupCallback(hook: Function, result: any): Function | undefined { | ||
| 24 | + if (typeof result === 'function') { | ||
| 25 | + const timeout | ||
| 26 | + = CLEANUP_TIMEOUT_KEY in hook && typeof hook[CLEANUP_TIMEOUT_KEY] === 'number' | ||
| 27 | + ? hook[CLEANUP_TIMEOUT_KEY] | ||
| 28 | + : getDefaultHookTimeout() | ||
| 29 | + return withTimeout(result, timeout, true) | ||
| 30 | + } | ||
| 31 | + } | ||
| 32 | + | ||
| 21 | 33 | /** | |
| 22 | 34 | * Registers a callback function to be executed once before all tests within the current suite. | |
| 23 | 35 | * This hook is useful for scenarios where you need to perform setup operations that are common to all tests in a suite, such as initializing a database connection or setting up a test environment. | |
@@ -35,11 +47,14 @@ function getDefaultHookTimeout() { | |||
| 35 | 47 | * }); | |
| 36 | 48 | * ``` | |
| 37 | 49 | */ | |
| 38 | - export function beforeAll(fn: BeforeAllListener, timeout?: number): void { | ||
| 50 | + export function beforeAll( | ||
| 51 | + fn: BeforeAllListener, | ||
| 52 | + timeout: number = getDefaultHookTimeout(), | ||
| 53 | + ): void { | ||
| 39 | 54 | assertTypes(fn, '"beforeAll" callback', ['function']) | |
| 40 | 55 | return getCurrentSuite().on( | |
| 41 | 56 | 'beforeAll', | |
| 42 | - withTimeout(fn, timeout ?? getDefaultHookTimeout(), true), | ||
| 57 | + Object.assign(withTimeout(fn, timeout, true), { [CLEANUP_TIMEOUT_KEY]: timeout }), | ||
| 43 | 58 | ) | |
| 44 | 59 | } | |
| 45 | 60 | ||
@@ -87,12 +102,15 @@ export function afterAll(fn: AfterAllListener, timeout?: number): void { | |||
| 87 | 102 | */ | |
| 88 | 103 | export function beforeEach<ExtraContext = object>( | |
| 89 | 104 | fn: BeforeEachListener<ExtraContext>, | |
| 90 | - timeout?: number, | ||
| 105 | + timeout: number = getDefaultHookTimeout(), | ||
| 91 | 106 | ): void { | |
| 92 | 107 | assertTypes(fn, '"beforeEach" callback', ['function']) | |
| 93 | 108 | return getCurrentSuite<ExtraContext>().on( | |
| 94 | 109 | 'beforeEach', | |
| 95 | - withTimeout(withFixtures(fn), timeout ?? getDefaultHookTimeout(), true), | ||
| 110 | + Object.assign( | ||
| 111 | + withTimeout(withFixtures(fn), timeout ?? getDefaultHookTimeout(), true), | ||
| 112 | + { [CLEANUP_TIMEOUT_KEY]: timeout }, | ||
| 113 | + ), | ||
| 96 | 114 | ) | |
| 97 | 115 | } | |
| 98 | 116 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -21,6 +21,7 @@ import { processError } from '@vitest/utils/error' | |||
| 21 | 21 | import { collectTests } from './collect' | |
| 22 | 22 | import { PendingError } from './errors' | |
| 23 | 23 | import { callFixtureCleanup } from './fixture' | |
| 24 | + import { getBeforeHookCleanupCallback } from './hooks' | ||
| 24 | 25 | import { getFn, getHooks } from './map' | |
| 25 | 26 | import { setCurrentTest } from './test-state' | |
| 26 | 27 | import { limitConcurrency } from './utils/limit-concurrency' | |
@@ -143,14 +144,18 @@ export async function callSuiteHook<T extends keyof SuiteHooks>( | |||
| 143 | 144 | updateSuiteHookState(currentTask, name, 'run', runner) | |
| 144 | 145 | } | |
| 145 | 146 | ||
| 147 | + async function runHook(hook: Function) { | ||
| 148 | + return getBeforeHookCleanupCallback(hook, await hook(...args)) | ||
| 149 | + } | ||
| 150 | + | ||
| 146 | 151 | if (sequence === 'parallel') { | |
| 147 | 152 | callbacks.push( | |
| 148 | - ...(await Promise.all(hooks.map(hook => (hook as any)(...args)))), | ||
| 153 | + ...(await Promise.all(hooks.map(hook => runHook(hook)))), | ||
| 149 | 154 | ) | |
| 150 | 155 | } | |
| 151 | 156 | else { | |
| 152 | 157 | for (const hook of hooks) { | |
| 153 | - callbacks.push(await (hook as any)(...args)) | ||
| 158 | + callbacks.push(await runHook(hook)) | ||
| 154 | 159 | } | |
| 155 | 160 | } | |
| 156 | 161 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,11 @@ | |||
| 1 | + import { beforeEach, beforeAll, describe, test } from 'vitest'; | ||
| 2 | + | ||
| 3 | + describe('beforeEach cleanup timeout', () => { | ||
| 4 | + beforeEach(() => new Promise(() => {}), 101) | ||
| 5 | + test("ok", () => {}) | ||
| 6 | + }) | ||
| 7 | + | ||
| 8 | + describe('beforeAll cleanup timeout', () => { | ||
| 9 | + beforeAll(() => new Promise(() => {}), 102) | ||
| 10 | + test("ok", () => {}) | ||
| 11 | + }) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -42,6 +42,11 @@ exports[`should fail hooks-fail-beforeAll.test.ts 1`] = `"TypeError: "beforeAll" | |||
| 42 | 42 | ||
| 43 | 43 | exports[`should fail hooks-fail-beforeEach.test.ts 1`] = `"TypeError: "beforeEach" callback value must be function, received "string""`; | |
| 44 | 44 | ||
| 45 | + exports[`should fail hooks-timeout-before-hook-cleanup-callback.test.ts 1`] = ` | ||
| 46 | + "Error: Hook timed out in 101ms. | ||
| 47 | + Error: Hook timed out in 102ms." | ||
| 48 | + `; | ||
| 49 | + | ||
| 45 | 50 | exports[`should fail inline-snapshop-inside-each.test.ts 1`] = ` | |
| 46 | 51 | "Error: InlineSnapshot cannot be used inside of test.each or describe.each | |
| 47 | 52 | Error: InlineSnapshot cannot be used inside of test.each or describe.each | |
| Back | FazBrowse Home | New Git URL |
0 commit comments