Skip to content

Commit 338d955

Browse files
authored
refactor(runner): deprecate custom type in favour of "test" (#6866)
1 parent d9cc81d commit 338d955

File tree

8 files changed

+39
-24
lines changed

8 files changed

+39
-24
lines changed

packages/runner/src/context.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { Awaitable } from '@vitest/utils'
22
import type { VitestRunner } from './types/runner'
33
import type {
4-
Custom,
54
ExtendedContext,
65
RuntimeContext,
76
SuiteCollector,
@@ -57,7 +56,7 @@ export function withTimeout<T extends (...args: any[]) => any>(
5756
}) as T
5857
}
5958

60-
export function createTestContext<T extends Test | Custom>(
59+
export function createTestContext<T extends Test>(
6160
test: T,
6261
runner: VitestRunner,
6362
): ExtendedContext<T> {

packages/runner/src/suite.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,14 +304,14 @@ function createSuiteCollector(
304304
initSuite(true)
305305

306306
const task = function (name = '', options: TaskCustomOptions = {}) {
307-
const task: Custom = {
307+
const task: Test = {
308308
id: '',
309309
name,
310310
suite: undefined!,
311311
each: options.each,
312312
fails: options.fails,
313313
context: undefined!,
314-
type: 'custom',
314+
type: 'test',
315315
file: undefined!,
316316
retry: options.retry ?? runner.config.retry,
317317
repeats: options.repeats,

packages/runner/src/types/runner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export interface VitestRunner {
140140
*
141141
* @see https://vitest.dev/advanced/runner.html#your-task-function
142142
*/
143-
extendTaskContext?: <T extends Test | Custom>(
143+
extendTaskContext?: <T extends Test>(
144144
context: TaskContext<T>
145145
) => ExtendedContext<T>
146146
/**

packages/runner/src/types/tasks.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,16 +220,25 @@ export interface Test<ExtraContext = object> extends TaskPopulated {
220220
context: TaskContext<Test> & ExtraContext & TestContext
221221
}
222222

223+
/**
224+
* @deprecated Use `Test` instead. `type: 'custom'` is not used since 2.2
225+
*/
223226
export interface Custom<ExtraContext = object> extends TaskPopulated {
227+
/**
228+
* @deprecated use `test` instead. `custom` is not used since 2.2
229+
*/
224230
type: 'custom'
225231
/**
226232
* Task context that will be passed to the test function.
227233
*/
228-
context: TaskContext<Custom> & ExtraContext & TestContext
234+
context: TaskContext<Test> & ExtraContext & TestContext
229235
}
230236

231237
export type Task = Test | Suite | Custom | File
232238

239+
/**
240+
* @deprecated Vitest doesn't provide `done()` anymore
241+
*/
233242
export type DoneCallback = (error?: any) => void
234243
export type TestFunction<ExtraContext = object> = (
235244
context: ExtendedContext<Test> & ExtraContext
@@ -515,14 +524,14 @@ export interface AfterAllListener {
515524

516525
export interface BeforeEachListener<ExtraContext = object> {
517526
(
518-
context: ExtendedContext<Test | Custom> & ExtraContext,
527+
context: ExtendedContext<Test> & ExtraContext,
519528
suite: Readonly<Suite>
520529
): Awaitable<unknown>
521530
}
522531

523532
export interface AfterEachListener<ExtraContext = object> {
524533
(
525-
context: ExtendedContext<Test | Custom> & ExtraContext,
534+
context: ExtendedContext<Test> & ExtraContext,
526535
suite: Readonly<Suite>
527536
): Awaitable<unknown>
528537
}
@@ -552,7 +561,7 @@ export interface TaskCustomOptions extends TestOptions {
552561
* If nothing is provided, the runner will try to get the function using `getFn(task)`.
553562
* If the runner cannot find the function, the task will be marked as failed.
554563
*/
555-
handler?: (context: TaskContext<Custom>) => Awaitable<void>
564+
handler?: (context: TaskContext<Test>) => Awaitable<void>
556565
}
557566

558567
export interface SuiteCollector<ExtraContext = object> {
@@ -563,11 +572,12 @@ export interface SuiteCollector<ExtraContext = object> {
563572
test: TestAPI<ExtraContext>
564573
tasks: (
565574
| Suite
575+
// TODO: remove in Vitest 3
566576
| Custom<ExtraContext>
567577
| Test<ExtraContext>
568578
| SuiteCollector<ExtraContext>
569579
)[]
570-
task: (name: string, options?: TaskCustomOptions) => Custom<ExtraContext>
580+
task: (name: string, options?: TaskCustomOptions) => Test<ExtraContext>
571581
collect: (file: File) => Promise<Suite>
572582
clear: () => void
573583
on: <T extends keyof SuiteHooks<ExtraContext>>(
@@ -593,7 +603,7 @@ export interface TestContext {}
593603
/**
594604
* Context that's always available in the test function.
595605
*/
596-
export interface TaskContext<Task extends Custom | Test = Custom | Test> {
606+
export interface TaskContext<Task extends Test = Test> {
597607
/**
598608
* Metadata of the current test
599609
*/
@@ -616,7 +626,7 @@ export interface TaskContext<Task extends Custom | Test = Custom | Test> {
616626
skip: (note?: string) => void
617627
}
618628

619-
export type ExtendedContext<T extends Custom | Test> = TaskContext<T> &
629+
export type ExtendedContext<T extends Test> = TaskContext<T> &
620630
TestContext
621631

622632
export type OnTestFailedHandler = (result: TaskResult) => Awaitable<void>

packages/runner/src/utils/tasks.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
import type { Custom, Suite, Task, Test } from '../types/tasks'
22
import { type Arrayable, toArray } from '@vitest/utils'
33

4+
/**
5+
* @deprecated use `isTestCase` instead
6+
*/
47
export function isAtomTest(s: Task): s is Test | Custom {
8+
return isTestCase(s)
9+
}
10+
11+
export function isTestCase(s: Task): s is Test | Custom {
512
return s.type === 'test' || s.type === 'custom'
613
}
714

815
export function getTests(suite: Arrayable<Task>): (Test | Custom)[] {
916
const tests: (Test | Custom)[] = []
1017
const arraySuites = toArray(suite)
1118
for (const s of arraySuites) {
12-
if (isAtomTest(s)) {
19+
if (isTestCase(s)) {
1320
tests.push(s)
1421
}
1522
else {
1623
for (const task of s.tasks) {
17-
if (isAtomTest(task)) {
24+
if (isTestCase(task)) {
1825
tests.push(task)
1926
}
2027
else {
@@ -31,7 +38,7 @@ export function getTests(suite: Arrayable<Task>): (Test | Custom)[] {
3138

3239
export function getTasks(tasks: Arrayable<Task> = []): Task[] {
3340
return toArray(tasks).flatMap(s =>
34-
isAtomTest(s) ? [s] : [s, ...getTasks(s.tasks)],
41+
isTestCase(s) ? [s] : [s, ...getTasks(s.tasks)],
3542
)
3643
}
3744

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

4451
export function hasTests(suite: Arrayable<Suite>): boolean {
4552
return toArray(suite).some(s =>
46-
s.tasks.some(c => isAtomTest(c) || hasTests(c)),
53+
s.tasks.some(c => isTestCase(c) || hasTests(c)),
4754
)
4855
}
4956

packages/vitest/src/runtime/benchmark.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import type { Custom } from '@vitest/runner'
1+
import type { Test } from '@vitest/runner'
22
import type { BenchFunction, BenchmarkAPI, BenchOptions } from './types/benchmark'
33
import { getCurrentSuite } from '@vitest/runner'
44
import { createChainable } from '@vitest/runner/utils'
55
import { noop } from '@vitest/utils'
66
import { getWorkerState } from './utils'
77

8-
const benchFns = new WeakMap<Custom, BenchFunction>()
8+
const benchFns = new WeakMap<Test, BenchFunction>()
99
const benchOptsMap = new WeakMap()
1010

11-
export function getBenchOptions(key: Custom): BenchOptions {
11+
export function getBenchOptions(key: Test): BenchOptions {
1212
return benchOptsMap.get(key)
1313
}
1414

15-
export function getBenchFn(key: Custom): BenchFunction {
15+
export function getBenchFn(key: Test): BenchFunction {
1616
return benchFns.get(key)!
1717
}
1818

packages/vitest/src/runtime/runners/test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { ExpectStatic } from '@vitest/expect'
22
import type {
33
CancelReason,
4-
Custom,
54
ExtendedContext,
65
File,
76
Suite,
@@ -171,7 +170,7 @@ export class VitestTestRunner implements VitestRunner {
171170
}
172171
}
173172

174-
extendTaskContext<T extends Test | Custom>(
173+
extendTaskContext<T extends Test>(
175174
context: TaskContext<T>,
176175
): ExtendedContext<T> {
177176
// create error during the test initialization so we have a nice stack trace

packages/vitest/src/runtime/types/benchmark.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Custom } from '@vitest/runner'
1+
import type { Test } from '@vitest/runner'
22
import type { ChainableFunction } from '@vitest/runner/utils'
33
import type {
44
Bench as BenchFactory,
@@ -8,7 +8,7 @@ import type {
88
TaskResult as TinybenchResult,
99
} from 'tinybench'
1010

11-
export interface Benchmark extends Custom {
11+
export interface Benchmark extends Test {
1212
meta: {
1313
benchmark: true
1414
result?: BenchTaskResult

0 commit comments

Comments
 (0)