Skip to content

Commit

Permalink
fix(types): Allow Callbacks Passed to before*/after* to Return Anythi…
Browse files Browse the repository at this point in the history
…ng (#6393)
  • Loading branch information
LuciNyan authored and sheremet-va committed Sep 12, 2024
1 parent 02fa22b commit 84c16ef
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
13 changes: 8 additions & 5 deletions packages/runner/src/types/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,28 +498,31 @@ export type HookListener<T extends any[], Return = void> = (
...args: T
) => Awaitable<Return>

export type HookCleanupCallback = (() => Awaitable<unknown>) | void
/**
* @deprecated
*/
export type HookCleanupCallback = unknown

export interface BeforeAllListener {
(suite: Readonly<Suite | File>): Awaitable<HookCleanupCallback>
(suite: Readonly<Suite | File>): Awaitable<unknown>
}

export interface AfterAllListener {
(suite: Readonly<Suite | File>): Awaitable<void>
(suite: Readonly<Suite | File>): Awaitable<unknown>
}

export interface BeforeEachListener<ExtraContext = object> {
(
context: ExtendedContext<Test | Custom> & ExtraContext,
suite: Readonly<Suite>
): Awaitable<HookCleanupCallback>
): Awaitable<unknown>
}

export interface AfterEachListener<ExtraContext = object> {
(
context: ExtendedContext<Test | Custom> & ExtraContext,
suite: Readonly<Suite>
): Awaitable<void>
): Awaitable<unknown>
}

export interface SuiteHooks<ExtraContext = object> {
Expand Down
24 changes: 23 additions & 1 deletion test/core/test/hooks.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { beforeAll, beforeEach, expect, it, suite } from 'vitest'
import { afterAll, afterEach, beforeAll, beforeEach, expect, it, suite } from 'vitest'

let count = -1

Expand Down Expand Up @@ -80,4 +80,26 @@ suite('hooks cleanup', () => {
it('end', () => {
expect(cleanUpCount).toBe(0)
})

suite('do nothing when given a non-function value as cleanupCallback', () => {
beforeAll(() => {
return 1
})
beforeEach(() => {
return null
})
afterAll(() => {
return '1'
})
afterEach(() => {
return {}
})

it('one', () => {
expect(cleanUpCount).toBe(0)
})
})
it('end', () => {
expect(cleanUpCount).toBe(0)
})
})

0 comments on commit 84c16ef

Please sign in to comment.