Skip to content

Commit cba3ff0

Browse files
committed
fix: check hook teardown return type, closes #2092
1 parent 757199a commit cba3ff0

File tree

1 file changed

+12
-3
lines changed
  • packages/vitest/src/runtime

1 file changed

+12
-3
lines changed

packages/vitest/src/runtime/run.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { performance } from 'perf_hooks'
22
import limit from 'p-limit'
33
import type { BenchTask, Benchmark, BenchmarkResult, File, HookCleanupCallback, HookListener, ResolvedConfig, Suite, SuiteHooks, Task, TaskResult, TaskState, Test } from '../types'
44
import { vi } from '../integrations/vi'
5-
import { clearTimeout, createDefer, getFullName, getWorkerState, hasFailed, hasTests, isBrowser, isNode, isRunningInBenchmark, partitionSuiteChildren, setTimeout, shuffle } from '../utils'
5+
import { assertTypes, clearTimeout, createDefer, getFullName, getWorkerState, hasFailed, hasTests, isBrowser, isNode, isRunningInBenchmark, partitionSuiteChildren, setTimeout, shuffle } from '../utils'
66
import { getState, setState } from '../integrations/chai/jest-expect'
77
import { GLOBAL_EXPECT } from '../integrations/chai/constants'
88
import { takeCoverageInsideWorker } from '../integrations/coverage'
@@ -84,6 +84,15 @@ async function sendTasksUpdate() {
8484
}
8585
}
8686

87+
const callCleanupHooks = async (cleanups: HookCleanupCallback[]) => {
88+
await Promise.all(cleanups.map(async (fn) => {
89+
if (!fn)
90+
return
91+
assertTypes(fn, 'hook teardown', ['function'])
92+
await fn()
93+
}))
94+
}
95+
8796
export async function runTest(test: Test) {
8897
if (test.mode !== 'run') {
8998
const { getSnapshotClient } = await import('../integrations/snapshot/chai')
@@ -157,7 +166,7 @@ export async function runTest(test: Test) {
157166

158167
try {
159168
await callSuiteHook(test.suite, test, 'afterEach', [test.context, test.suite])
160-
await Promise.all(beforeEachCleanups.map(i => i?.()))
169+
await callCleanupHooks(beforeEachCleanups)
161170
}
162171
catch (e) {
163172
test.result.state = 'fail'
@@ -264,7 +273,7 @@ export async function runSuite(suite: Suite) {
264273
}
265274

266275
await callSuiteHook(suite, suite, 'afterAll', [suite])
267-
await Promise.all(beforeAllCleanups.map(i => i?.()))
276+
await callCleanupHooks(beforeAllCleanups)
268277
}
269278
catch (e) {
270279
suite.result.state = 'fail'

0 commit comments

Comments
 (0)