( suite: Suite, currentTask: Task, name: T, runner: VitestRunner, args: SuiteHooks[T][0] extends (...args: infer A) => Awaitable<any> ? A : never, )
| 165 | } |
| 166 | |
| 167 | export async function callSuiteHook<T extends keyof SuiteHooks>( |
| 168 | suite: Suite, |
| 169 | currentTask: Task, |
| 170 | name: T, |
| 171 | runner: VitestRunner, |
| 172 | args: SuiteHooks[T][0] extends (...args: infer A) => Awaitable<any> ? A : never, |
| 173 | ): Promise<unknown[]> { |
| 174 | const sequence = runner.config.sequence.hooks |
| 175 | |
| 176 | const callbacks: unknown[] = [] |
| 177 | // stop at file level |
| 178 | const parentSuite: Suite | null = 'filepath' in suite ? null : suite.suite || suite.file |
| 179 | |
| 180 | if (name === 'beforeEach' && parentSuite) { |
| 181 | callbacks.push( |
| 182 | ...(await callSuiteHook(parentSuite, currentTask, name, runner, args)), |
| 183 | ) |
| 184 | } |
| 185 | |
| 186 | const hooks = getSuiteHooks(suite, name, sequence) |
| 187 | |
| 188 | if (hooks.length > 0) { |
| 189 | updateSuiteHookState(currentTask, name, 'run', runner) |
| 190 | } |
| 191 | |
| 192 | async function runHook(hook: Function) { |
| 193 | return limitMaxConcurrency(async () => { |
| 194 | return getBeforeHookCleanupCallback( |
| 195 | hook, |
| 196 | await hook(...args), |
| 197 | name === 'beforeEach' ? args[0] as TestContext : undefined, |
| 198 | ) |
| 199 | }) |
| 200 | } |
| 201 | |
| 202 | if (sequence === 'parallel') { |
| 203 | callbacks.push( |
| 204 | ...(await Promise.all(hooks.map(hook => runHook(hook)))), |
| 205 | ) |
| 206 | } |
| 207 | else { |
| 208 | for (const hook of hooks) { |
| 209 | callbacks.push(await runHook(hook)) |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | if (hooks.length > 0) { |
| 214 | updateSuiteHookState(currentTask, name, 'pass', runner) |
| 215 | } |
| 216 | |
| 217 | if (name === 'afterEach' && parentSuite) { |
| 218 | callbacks.push( |
| 219 | ...(await callSuiteHook(parentSuite, currentTask, name, runner, args)), |
| 220 | ) |
| 221 | } |
| 222 | |
| 223 | return callbacks |
| 224 | } |
no test coverage detected