|
1 | 1 | import { format, isObject, noop, objDisplay, objectAttr } from '@vitest/utils' |
2 | | -import type { File, RunMode, Suite, SuiteAPI, SuiteCollector, SuiteFactory, SuiteHooks, Task, TaskCustom, Test, TestAPI, TestFunction, TestOptions } from './types' |
| 2 | +import type { File, Fixtures, RunMode, Suite, SuiteAPI, SuiteCollector, SuiteFactory, SuiteHooks, Task, TaskCustom, Test, TestAPI, TestFunction, TestOptions } from './types' |
3 | 3 | import type { VitestRunner } from './types/runner' |
4 | 4 | import { createChainable } from './utils/chain' |
5 | 5 | import { collectTask, collectorContext, createTestContext, runWithSuite, withTimeout } from './context' |
6 | 6 | import { getHooks, setFn, setHooks } from './map' |
| 7 | +import { withFixtures } from './fixture' |
7 | 8 |
|
8 | 9 | // apis |
9 | 10 | export const suite = createSuite() |
@@ -95,7 +96,9 @@ function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, m |
95 | 96 | }) |
96 | 97 |
|
97 | 98 | setFn(test, withTimeout( |
98 | | - () => fn(context), |
| 99 | + this.fixtures |
| 100 | + ? withFixtures(fn, this.fixtures, context) |
| 101 | + : () => fn(context), |
99 | 102 | options?.timeout ?? runner.config.testTimeout, |
100 | 103 | )) |
101 | 104 |
|
@@ -229,12 +232,12 @@ function createSuite() { |
229 | 232 |
|
230 | 233 | function createTest(fn: ( |
231 | 234 | ( |
232 | | - this: Record<'concurrent' | 'skip' | 'only' | 'todo' | 'fails' | 'each', boolean | undefined>, |
| 235 | + this: Record<'concurrent' | 'skip' | 'only' | 'todo' | 'fails' | 'each', boolean | undefined> & { fixtures?: Fixtures<Record<string, any>> }, |
233 | 236 | title: string, |
234 | 237 | fn?: TestFunction, |
235 | 238 | options?: number | TestOptions |
236 | 239 | ) => void |
237 | | -)) { |
| 240 | +), context?: Record<string, any>) { |
238 | 241 | const testFn = fn as any |
239 | 242 |
|
240 | 243 | testFn.each = function<T>(this: { withContext: () => SuiteAPI; setContext: (key: string, value: boolean | undefined) => SuiteAPI }, cases: ReadonlyArray<T>, ...args: any[]) { |
@@ -262,9 +265,20 @@ function createTest(fn: ( |
262 | 265 | testFn.skipIf = (condition: any) => (condition ? test.skip : test) as TestAPI |
263 | 266 | testFn.runIf = (condition: any) => (condition ? test : test.skip) as TestAPI |
264 | 267 |
|
| 268 | + testFn.extend = function (fixtures: Fixtures<Record<string, any>>) { |
| 269 | + const _context = context |
| 270 | + ? { ...context, fixtures: { ...context.fixtures, ...fixtures } } |
| 271 | + : { fixtures } |
| 272 | + |
| 273 | + return createTest(function fn(name: string | Function, fn?: TestFunction, options?: number | TestOptions) { |
| 274 | + getCurrentSuite().test.fn.call(this, formatName(name), fn, options) |
| 275 | + }, _context) |
| 276 | + } |
| 277 | + |
265 | 278 | return createChainable( |
266 | 279 | ['concurrent', 'skip', 'only', 'todo', 'fails'], |
267 | 280 | testFn, |
| 281 | + context, |
268 | 282 | ) as TestAPI |
269 | 283 | } |
270 | 284 |
|
|
0 commit comments