diff --git a/packages/runner/src/context.ts b/packages/runner/src/context.ts index 2bde9eb4b479..af3bd3a0e3d6 100644 --- a/packages/runner/src/context.ts +++ b/packages/runner/src/context.ts @@ -1,7 +1,6 @@ import type { Awaitable } from '@vitest/utils' import type { VitestRunner } from './types/runner' import type { - Custom, ExtendedContext, RuntimeContext, SuiteCollector, @@ -57,7 +56,7 @@ export function withTimeout any>( }) as T } -export function createTestContext( +export function createTestContext( test: T, runner: VitestRunner, ): ExtendedContext { diff --git a/packages/runner/src/suite.ts b/packages/runner/src/suite.ts index d3c972794072..1a568eea48f8 100644 --- a/packages/runner/src/suite.ts +++ b/packages/runner/src/suite.ts @@ -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, diff --git a/packages/runner/src/types/runner.ts b/packages/runner/src/types/runner.ts index 6e5e1dbd7d4c..ade94a143d1d 100644 --- a/packages/runner/src/types/runner.ts +++ b/packages/runner/src/types/runner.ts @@ -140,7 +140,7 @@ export interface VitestRunner { * * @see https://vitest.dev/advanced/runner.html#your-task-function */ - extendTaskContext?: ( + extendTaskContext?: ( context: TaskContext ) => ExtendedContext /** diff --git a/packages/runner/src/types/tasks.ts b/packages/runner/src/types/tasks.ts index 5f1f9cc0a4b2..19645f304668 100644 --- a/packages/runner/src/types/tasks.ts +++ b/packages/runner/src/types/tasks.ts @@ -220,16 +220,25 @@ export interface Test extends TaskPopulated { context: TaskContext & ExtraContext & TestContext } +/** + * @deprecated Use `Test` instead. `type: 'custom'` is not used since 2.2 + */ export interface Custom 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 & ExtraContext & TestContext + context: TaskContext & 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 = ( context: ExtendedContext & ExtraContext @@ -515,14 +524,14 @@ export interface AfterAllListener { export interface BeforeEachListener { ( - context: ExtendedContext & ExtraContext, + context: ExtendedContext & ExtraContext, suite: Readonly ): Awaitable } export interface AfterEachListener { ( - context: ExtendedContext & ExtraContext, + context: ExtendedContext & ExtraContext, suite: Readonly ): Awaitable } @@ -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) => Awaitable + handler?: (context: TaskContext) => Awaitable } export interface SuiteCollector { @@ -563,11 +572,12 @@ export interface SuiteCollector { test: TestAPI tasks: ( | Suite + // TODO: remove in Vitest 3 | Custom | Test | SuiteCollector )[] - task: (name: string, options?: TaskCustomOptions) => Custom + task: (name: string, options?: TaskCustomOptions) => Test collect: (file: File) => Promise clear: () => void on: >( @@ -593,7 +603,7 @@ export interface TestContext {} /** * Context that's always available in the test function. */ -export interface TaskContext { +export interface TaskContext { /** * Metadata of the current test */ @@ -616,7 +626,7 @@ export interface TaskContext { skip: (note?: string) => void } -export type ExtendedContext = TaskContext & +export type ExtendedContext = TaskContext & TestContext export type OnTestFailedHandler = (result: TaskResult) => Awaitable diff --git a/packages/runner/src/utils/tasks.ts b/packages/runner/src/utils/tasks.ts index 1d383c9cc137..b1071ca8bb2e 100644 --- a/packages/runner/src/utils/tasks.ts +++ b/packages/runner/src/utils/tasks.ts @@ -1,7 +1,14 @@ 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' } @@ -9,12 +16,12 @@ export function getTests(suite: Arrayable): (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 { @@ -31,7 +38,7 @@ export function getTests(suite: Arrayable): (Test | Custom)[] { export function getTasks(tasks: Arrayable = []): Task[] { return toArray(tasks).flatMap(s => - isAtomTest(s) ? [s] : [s, ...getTasks(s.tasks)], + isTestCase(s) ? [s] : [s, ...getTasks(s.tasks)], ) } @@ -43,7 +50,7 @@ export function getSuites(suite: Arrayable): Suite[] { export function hasTests(suite: Arrayable): boolean { return toArray(suite).some(s => - s.tasks.some(c => isAtomTest(c) || hasTests(c)), + s.tasks.some(c => isTestCase(c) || hasTests(c)), ) } diff --git a/packages/vitest/src/runtime/benchmark.ts b/packages/vitest/src/runtime/benchmark.ts index bf7bc78636f9..6772720a31a1 100644 --- a/packages/vitest/src/runtime/benchmark.ts +++ b/packages/vitest/src/runtime/benchmark.ts @@ -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() +const benchFns = new WeakMap() 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)! } diff --git a/packages/vitest/src/runtime/runners/test.ts b/packages/vitest/src/runtime/runners/test.ts index 952d27389981..216e0fbd52c9 100644 --- a/packages/vitest/src/runtime/runners/test.ts +++ b/packages/vitest/src/runtime/runners/test.ts @@ -1,7 +1,6 @@ import type { ExpectStatic } from '@vitest/expect' import type { CancelReason, - Custom, ExtendedContext, File, Suite, @@ -171,7 +170,7 @@ export class VitestTestRunner implements VitestRunner { } } - extendTaskContext( + extendTaskContext( context: TaskContext, ): ExtendedContext { // create error during the test initialization so we have a nice stack trace diff --git a/packages/vitest/src/runtime/types/benchmark.ts b/packages/vitest/src/runtime/types/benchmark.ts index 17b39f2a89fd..9ffd62c4f83b 100644 --- a/packages/vitest/src/runtime/types/benchmark.ts +++ b/packages/vitest/src/runtime/types/benchmark.ts @@ -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, @@ -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