Skip to content

Commit

Permalink
fix(runner): guard test hook callback (#6604)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Oct 1, 2024
1 parent 7e26235 commit 1497134
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/runner/src/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { assertTypes } from '@vitest/utils'
import type {
AfterAllListener,
AfterEachListener,
Expand Down Expand Up @@ -35,6 +36,7 @@ function getDefaultHookTimeout() {
* ```
*/
export function beforeAll(fn: BeforeAllListener, timeout?: number): void {
assertTypes(fn, '"beforeAll" callback', ['function'])
return getCurrentSuite().on(
'beforeAll',
withTimeout(fn, timeout ?? getDefaultHookTimeout(), true),
Expand All @@ -59,6 +61,7 @@ export function beforeAll(fn: BeforeAllListener, timeout?: number): void {
* ```
*/
export function afterAll(fn: AfterAllListener, timeout?: number): void {
assertTypes(fn, '"afterAll" callback', ['function'])
return getCurrentSuite().on(
'afterAll',
withTimeout(fn, timeout ?? getDefaultHookTimeout(), true),
Expand Down Expand Up @@ -86,6 +89,7 @@ export function beforeEach<ExtraContext = object>(
fn: BeforeEachListener<ExtraContext>,
timeout?: number,
): void {
assertTypes(fn, '"beforeEach" callback', ['function'])
return getCurrentSuite<ExtraContext>().on(
'beforeEach',
withTimeout(withFixtures(fn), timeout ?? getDefaultHookTimeout(), true),
Expand Down Expand Up @@ -113,6 +117,7 @@ export function afterEach<ExtraContext = object>(
fn: AfterEachListener<ExtraContext>,
timeout?: number,
): void {
assertTypes(fn, '"afterEach" callback', ['function'])
return getCurrentSuite<ExtraContext>().on(
'afterEach',
withTimeout(withFixtures(fn), timeout ?? getDefaultHookTimeout(), true),
Expand Down Expand Up @@ -183,6 +188,8 @@ function createTestHook<T>(
handler: (test: TaskPopulated, handler: T, timeout?: number) => void,
): TaskHook<T> {
return (fn: T, timeout?: number) => {
assertTypes(fn, `"${name}" callback`, ['function'])

const current = getCurrentTest()

if (!current) {
Expand Down
7 changes: 7 additions & 0 deletions test/cli/fixtures/fails/hooks-fail-afterAll.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { describe, test, afterAll } from 'vitest';

describe('afterAll hooks fail', () => {
// @ts-ignore expects a function
afterAll('fail')
test.todo('todo')
})
7 changes: 7 additions & 0 deletions test/cli/fixtures/fails/hooks-fail-afterEach.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { afterEach, describe, test } from 'vitest';

describe('afterEach hooks fail', () => {
// @ts-ignore expects a function
afterEach('fail')
test.todo('todo')
})
7 changes: 7 additions & 0 deletions test/cli/fixtures/fails/hooks-fail-beforeAll.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { beforeAll, describe, test } from 'vitest';

describe('beforeAll hooks fail', () => {
// @ts-ignore expects a function
beforeAll('fail')
test.todo('todo')
})
7 changes: 7 additions & 0 deletions test/cli/fixtures/fails/hooks-fail-beforeEach.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { beforeEach, describe, test } from 'vitest';

describe('beforeEach hooks fail', () => {
// @ts-ignore expects a function
beforeEach('fail')
test.todo('todo')
})
8 changes: 8 additions & 0 deletions test/cli/test/__snapshots__/fails.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ exports[`should fail hooks-called.test.ts > hooks-called.test.ts 1`] = `
Error: before all"
`;
exports[`should fail hooks-fail-afterAll.test.ts > hooks-fail-afterAll.test.ts 1`] = `"TypeError: "afterAll" callback value must be function, received "string""`;
exports[`should fail hooks-fail-afterEach.test.ts > hooks-fail-afterEach.test.ts 1`] = `"TypeError: "afterEach" callback value must be function, received "string""`;
exports[`should fail hooks-fail-beforeAll.test.ts > hooks-fail-beforeAll.test.ts 1`] = `"TypeError: "beforeAll" callback value must be function, received "string""`;
exports[`should fail hooks-fail-beforeEach.test.ts > hooks-fail-beforeEach.test.ts 1`] = `"TypeError: "beforeEach" callback value must be function, received "string""`;
exports[`should fail inline-snapshop-inside-each.test.ts > inline-snapshop-inside-each.test.ts 1`] = `
"Error: InlineSnapshot cannot be used inside of test.each or describe.each
Error: InlineSnapshot cannot be used inside of test.each or describe.each
Expand Down

0 comments on commit 1497134

Please sign in to comment.