[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

fix(runner): guard test hook callback (#6604)

authored by

Vladimir and committed by
GitHub
(Oct 1, 2024, 3:58 PM +0200) 1497134e 7e262357

+43
+7
packages/runner/src/hooks.ts
··· 1 + import { assertTypes } from '@vitest/utils' 1 2 import type { 2 3 AfterAllListener, 3 4 AfterEachListener, ··· 35 36 * ``` 36 37 */ 37 38 export function beforeAll(fn: BeforeAllListener, timeout?: number): void { 39 + assertTypes(fn, '"beforeAll" callback', ['function']) 38 40 return getCurrentSuite().on( 39 41 'beforeAll', 40 42 withTimeout(fn, timeout ?? getDefaultHookTimeout(), true), ··· 59 61 * ``` 60 62 */ 61 63 export function afterAll(fn: AfterAllListener, timeout?: number): void { 64 + assertTypes(fn, '"afterAll" callback', ['function']) 62 65 return getCurrentSuite().on( 63 66 'afterAll', 64 67 withTimeout(fn, timeout ?? getDefaultHookTimeout(), true), ··· 86 89 fn: BeforeEachListener<ExtraContext>, 87 90 timeout?: number, 88 91 ): void { 92 + assertTypes(fn, '"beforeEach" callback', ['function']) 89 93 return getCurrentSuite<ExtraContext>().on( 90 94 'beforeEach', 91 95 withTimeout(withFixtures(fn), timeout ?? getDefaultHookTimeout(), true), ··· 113 117 fn: AfterEachListener<ExtraContext>, 114 118 timeout?: number, 115 119 ): void { 120 + assertTypes(fn, '"afterEach" callback', ['function']) 116 121 return getCurrentSuite<ExtraContext>().on( 117 122 'afterEach', 118 123 withTimeout(withFixtures(fn), timeout ?? getDefaultHookTimeout(), true), ··· 183 188 handler: (test: TaskPopulated, handler: T, timeout?: number) => void, 184 189 ): TaskHook<T> { 185 190 return (fn: T, timeout?: number) => { 191 + assertTypes(fn, `"${name}" callback`, ['function']) 192 + 186 193 const current = getCurrentTest() 187 194 188 195 if (!current) {
+7
test/cli/fixtures/fails/hooks-fail-afterAll.test.ts
··· 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 + })
+7
test/cli/fixtures/fails/hooks-fail-afterEach.test.ts
··· 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 + })
+7
test/cli/fixtures/fails/hooks-fail-beforeAll.test.ts
··· 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 + })
+7
test/cli/fixtures/fails/hooks-fail-beforeEach.test.ts
··· 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 + })
+8
test/cli/test/__snapshots__/fails.test.ts.snap
··· 28 28 Error: before all" 29 29 `; 30 30 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 + 31 39 exports[`should fail inline-snapshop-inside-each.test.ts > inline-snapshop-inside-each.test.ts 1`] = ` 32 40 "Error: InlineSnapshot cannot be used inside of test.each or describe.each 33 41 Error: InlineSnapshot cannot be used inside of test.each or describe.each