Skip to content

Commit 1497134

Browse files
authored
fix(runner): guard test hook callback (#6604)
1 parent 7e26235 commit 1497134

File tree

6 files changed

+43
-0
lines changed

6 files changed

+43
-0
lines changed

packages/runner/src/hooks.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { assertTypes } from '@vitest/utils'
12
import type {
23
AfterAllListener,
34
AfterEachListener,
@@ -35,6 +36,7 @@ function getDefaultHookTimeout() {
3536
* ```
3637
*/
3738
export function beforeAll(fn: BeforeAllListener, timeout?: number): void {
39+
assertTypes(fn, '"beforeAll" callback', ['function'])
3840
return getCurrentSuite().on(
3941
'beforeAll',
4042
withTimeout(fn, timeout ?? getDefaultHookTimeout(), true),
@@ -59,6 +61,7 @@ export function beforeAll(fn: BeforeAllListener, timeout?: number): void {
5961
* ```
6062
*/
6163
export function afterAll(fn: AfterAllListener, timeout?: number): void {
64+
assertTypes(fn, '"afterAll" callback', ['function'])
6265
return getCurrentSuite().on(
6366
'afterAll',
6467
withTimeout(fn, timeout ?? getDefaultHookTimeout(), true),
@@ -86,6 +89,7 @@ export function beforeEach<ExtraContext = object>(
8689
fn: BeforeEachListener<ExtraContext>,
8790
timeout?: number,
8891
): void {
92+
assertTypes(fn, '"beforeEach" callback', ['function'])
8993
return getCurrentSuite<ExtraContext>().on(
9094
'beforeEach',
9195
withTimeout(withFixtures(fn), timeout ?? getDefaultHookTimeout(), true),
@@ -113,6 +117,7 @@ export function afterEach<ExtraContext = object>(
113117
fn: AfterEachListener<ExtraContext>,
114118
timeout?: number,
115119
): void {
120+
assertTypes(fn, '"afterEach" callback', ['function'])
116121
return getCurrentSuite<ExtraContext>().on(
117122
'afterEach',
118123
withTimeout(withFixtures(fn), timeout ?? getDefaultHookTimeout(), true),
@@ -183,6 +188,8 @@ function createTestHook<T>(
183188
handler: (test: TaskPopulated, handler: T, timeout?: number) => void,
184189
): TaskHook<T> {
185190
return (fn: T, timeout?: number) => {
191+
assertTypes(fn, `"${name}" callback`, ['function'])
192+
186193
const current = getCurrentTest()
187194

188195
if (!current) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { describe, test, afterAll } from 'vitest';
2+
3+
describe('afterAll hooks fail', () => {
4+
// @ts-ignore expects a function
5+
afterAll('fail')
6+
test.todo('todo')
7+
})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { afterEach, describe, test } from 'vitest';
2+
3+
describe('afterEach hooks fail', () => {
4+
// @ts-ignore expects a function
5+
afterEach('fail')
6+
test.todo('todo')
7+
})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { beforeAll, describe, test } from 'vitest';
2+
3+
describe('beforeAll hooks fail', () => {
4+
// @ts-ignore expects a function
5+
beforeAll('fail')
6+
test.todo('todo')
7+
})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { beforeEach, describe, test } from 'vitest';
2+
3+
describe('beforeEach hooks fail', () => {
4+
// @ts-ignore expects a function
5+
beforeEach('fail')
6+
test.todo('todo')
7+
})

test/cli/test/__snapshots__/fails.test.ts.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ exports[`should fail hooks-called.test.ts > hooks-called.test.ts 1`] = `
2828
Error: before all"
2929
`;
3030
31+
exports[`should fail hooks-fail-afterAll.test.ts > hooks-fail-afterAll.test.ts 1`] = `"TypeError: "afterAll" callback value must be function, received "string""`;
32+
33+
exports[`should fail hooks-fail-afterEach.test.ts > hooks-fail-afterEach.test.ts 1`] = `"TypeError: "afterEach" callback value must be function, received "string""`;
34+
35+
exports[`should fail hooks-fail-beforeAll.test.ts > hooks-fail-beforeAll.test.ts 1`] = `"TypeError: "beforeAll" callback value must be function, received "string""`;
36+
37+
exports[`should fail hooks-fail-beforeEach.test.ts > hooks-fail-beforeEach.test.ts 1`] = `"TypeError: "beforeEach" callback value must be function, received "string""`;
38+
3139
exports[`should fail inline-snapshop-inside-each.test.ts > inline-snapshop-inside-each.test.ts 1`] = `
3240
"Error: InlineSnapshot cannot be used inside of test.each or describe.each
3341
Error: InlineSnapshot cannot be used inside of test.each or describe.each

0 commit comments

Comments
 (0)