Skip to content

Commit

Permalink
refactor(runner): deprecate custom type in favour of "test" (#6866)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Nov 18, 2024
1 parent d9cc81d commit 338d955
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 24 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
24 changes: 17 additions & 7 deletions packages/runner/src/types/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,16 +220,25 @@ export interface Test<ExtraContext = object> extends TaskPopulated {
context: TaskContext<Test> & ExtraContext & TestContext
}

/**
* @deprecated Use `Test` instead. `type: 'custom'` is not used since 2.2
*/
export interface Custom<ExtraContext = object> extends TaskPopulated {
/**
* @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

/**
* @deprecated Vitest doesn't provide `done()` anymore
*/
export type DoneCallback = (error?: any) => void
export type TestFunction<ExtraContext = object> = (
context: ExtendedContext<Test> & ExtraContext
Expand Down Expand Up @@ -515,14 +524,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 @@ -552,7 +561,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 @@ -563,11 +572,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 @@ -593,7 +603,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 @@ -616,7 +626,7 @@ export interface TaskContext<Task extends Custom | Test = Custom | Test> {
skip: (note?: string) => 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
15 changes: 11 additions & 4 deletions packages/runner/src/utils/tasks.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import type { Custom, Suite, Task, Test } from '../types/tasks'
import { type Arrayable, toArray } from '@vitest/utils'

/**
* @deprecated use `isTestCase` instead
*/
export function isAtomTest(s: Task): s is Test | Custom {
return isTestCase(s)
}

export function isTestCase(s: Task): s is Test | Custom {
return s.type === 'test' || s.type === 'custom'
}

export function getTests(suite: Arrayable<Task>): (Test | Custom)[] {
const tests: (Test | Custom)[] = []
const arraySuites = toArray(suite)
for (const s of arraySuites) {
if (isAtomTest(s)) {
if (isTestCase(s)) {
tests.push(s)
}
else {
for (const task of s.tasks) {
if (isAtomTest(task)) {
if (isTestCase(task)) {
tests.push(task)
}
else {
Expand All @@ -31,7 +38,7 @@ export function getTests(suite: Arrayable<Task>): (Test | Custom)[] {

export function getTasks(tasks: Arrayable<Task> = []): Task[] {
return toArray(tasks).flatMap(s =>
isAtomTest(s) ? [s] : [s, ...getTasks(s.tasks)],
isTestCase(s) ? [s] : [s, ...getTasks(s.tasks)],
)
}

Expand All @@ -43,7 +50,7 @@ export function getSuites(suite: Arrayable<Task>): Suite[] {

export function hasTests(suite: Arrayable<Suite>): boolean {
return toArray(suite).some(s =>
s.tasks.some(c => isAtomTest(c) || hasTests(c)),
s.tasks.some(c => isTestCase(c) || hasTests(c)),
)
}

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 @@ -171,7 +170,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 338d955

Please sign in to comment.