Skip to content

Commit

Permalink
refactor: remove type: 'custom' at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Nov 6, 2024
1 parent bbdbf91 commit 062bbee
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 22 deletions.
3 changes: 1 addition & 2 deletions packages/runner/src/context.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Awaitable } from '@vitest/utils'
import type { VitestRunner } from './types/runner'
import type {
Custom,
ExtendedContext,
RuntimeContext,
SuiteCollector,
Expand Down Expand Up @@ -57,7 +56,7 @@ export function withTimeout<T extends (...args: any[]) => any>(
}) as T
}

export function createTestContext<T extends Test | Custom>(
export function createTestContext<T extends Test>(
test: T,
runner: VitestRunner,
): ExtendedContext<T> {
Expand Down
4 changes: 2 additions & 2 deletions packages/runner/src/suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,14 +304,14 @@ function createSuiteCollector(
initSuite(true)

const task = function (name = '', options: TaskCustomOptions = {}) {
const task: Custom = {
const task: Test = {
id: '',
name,
suite: undefined!,
each: options.each,
fails: options.fails,
context: undefined!,
type: 'custom',
type: 'test',
file: undefined!,
retry: options.retry ?? runner.config.retry,
repeats: options.repeats,
Expand Down
2 changes: 1 addition & 1 deletion packages/runner/src/types/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export interface VitestRunner {
*
* @see https://vitest.dev/advanced/runner.html#your-task-function
*/
extendTaskContext?: <T extends Test | Custom>(
extendTaskContext?: <T extends Test>(
context: TaskContext<T>
) => ExtendedContext<T>
/**
Expand Down
19 changes: 10 additions & 9 deletions packages/runner/src/types/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,17 +219,17 @@ export interface Test<ExtraContext = object> extends TaskPopulated {
}

/**
* @deprecated Use `Test` instead. `type: 'custom'` will be removed in Vitest 3.0
* @deprecated Use `Test` instead. `type: 'custom'` is not used since 2.2
*/
export interface Custom<ExtraContext = object> extends TaskPopulated {
/**
* @deprecated use `test` instead. `custom` will be removed in Vitest 3.0
* @deprecated use `test` instead. `custom` is not used since 2.2
*/
type: 'custom'
/**
* Task context that will be passed to the test function.
*/
context: TaskContext<Custom> & ExtraContext & TestContext
context: TaskContext<Test> & ExtraContext & TestContext
}

export type Task = Test | Suite | Custom | File
Expand Down Expand Up @@ -522,14 +522,14 @@ export interface AfterAllListener {

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

export interface AfterEachListener<ExtraContext = object> {
(
context: ExtendedContext<Test | Custom> & ExtraContext,
context: ExtendedContext<Test> & ExtraContext,
suite: Readonly<Suite>
): Awaitable<unknown>
}
Expand Down Expand Up @@ -559,7 +559,7 @@ export interface TaskCustomOptions extends TestOptions {
* If nothing is provided, the runner will try to get the function using `getFn(task)`.
* If the runner cannot find the function, the task will be marked as failed.
*/
handler?: (context: TaskContext<Custom>) => Awaitable<void>
handler?: (context: TaskContext<Test>) => Awaitable<void>
}

export interface SuiteCollector<ExtraContext = object> {
Expand All @@ -570,11 +570,12 @@ export interface SuiteCollector<ExtraContext = object> {
test: TestAPI<ExtraContext>
tasks: (
| Suite
// TODO: remove in Vitest 3
| Custom<ExtraContext>
| Test<ExtraContext>
| SuiteCollector<ExtraContext>
)[]
task: (name: string, options?: TaskCustomOptions) => Custom<ExtraContext>
task: (name: string, options?: TaskCustomOptions) => Test<ExtraContext>
collect: (file: File) => Promise<Suite>
clear: () => void
on: <T extends keyof SuiteHooks<ExtraContext>>(
Expand All @@ -600,7 +601,7 @@ export interface TestContext {}
/**
* Context that's always available in the test function.
*/
export interface TaskContext<Task extends Custom | Test = Custom | Test> {
export interface TaskContext<Task extends Test = Test> {
/**
* Metadata of the current test
*/
Expand All @@ -623,7 +624,7 @@ export interface TaskContext<Task extends Custom | Test = Custom | Test> {
skip: () => void
}

export type ExtendedContext<T extends Custom | Test> = TaskContext<T> &
export type ExtendedContext<T extends Test> = TaskContext<T> &
TestContext

export type OnTestFailedHandler = (result: TaskResult) => Awaitable<void>
Expand Down
8 changes: 4 additions & 4 deletions packages/vitest/src/runtime/benchmark.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import type { Custom } from '@vitest/runner'
import type { Test } from '@vitest/runner'
import type { BenchFunction, BenchmarkAPI, BenchOptions } from './types/benchmark'
import { getCurrentSuite } from '@vitest/runner'
import { createChainable } from '@vitest/runner/utils'
import { noop } from '@vitest/utils'
import { getWorkerState } from './utils'

const benchFns = new WeakMap<Custom, BenchFunction>()
const benchFns = new WeakMap<Test, BenchFunction>()
const benchOptsMap = new WeakMap()

export function getBenchOptions(key: Custom): BenchOptions {
export function getBenchOptions(key: Test): BenchOptions {
return benchOptsMap.get(key)
}

export function getBenchFn(key: Custom): BenchFunction {
export function getBenchFn(key: Test): BenchFunction {
return benchFns.get(key)!
}

Expand Down
3 changes: 1 addition & 2 deletions packages/vitest/src/runtime/runners/test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { ExpectStatic } from '@vitest/expect'
import type {
CancelReason,
Custom,
ExtendedContext,
File,
Suite,
Expand Down Expand Up @@ -164,7 +163,7 @@ export class VitestTestRunner implements VitestRunner {
}
}

extendTaskContext<T extends Test | Custom>(
extendTaskContext<T extends Test>(
context: TaskContext<T>,
): ExtendedContext<T> {
// create error during the test initialization so we have a nice stack trace
Expand Down
4 changes: 2 additions & 2 deletions packages/vitest/src/runtime/types/benchmark.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Custom } from '@vitest/runner'
import type { Test } from '@vitest/runner'
import type { ChainableFunction } from '@vitest/runner/utils'
import type {
Bench as BenchFactory,
Expand All @@ -8,7 +8,7 @@ import type {
TaskResult as TinybenchResult,
} from 'tinybench'

export interface Benchmark extends Custom {
export interface Benchmark extends Test {
meta: {
benchmark: true
result?: BenchTaskResult
Expand Down

0 comments on commit 062bbee

Please sign in to comment.