Skip to content

Commit b2c0ea8

Browse files
committed
feat: support type inference with new test.extend syntax
1 parent 20e00ef commit b2c0ea8

File tree

8 files changed

+2486
-257
lines changed

8 files changed

+2486
-257
lines changed

AGENTS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ Vitest is a next-generation testing framework powered by Vite. This is a monorep
3535
- **Core directory test**: `CI=true pnpm test <test-file>` (for `test/core`)
3636
- **Browser tests**: `CI=true pnpm test:browser:playwright` or `CI=true pnpm test:browser:webdriverio`
3737

38+
When writing tests, AVOID using `toContain` for validation. Prefer using `toMatchSnapshot` to include the test error and its stack.
39+
40+
If you need to typecheck tests, run `pnpm typecheck` from the root of the workspace.
41+
3842
### Testing Utilities
3943
- **`runInlineTests`** from `test/test-utils/index.ts` - You must use this for complex file system setups (>1 file)
4044
- **`runVitest`** from `test/test-utils/index.ts` - You can use this to run Vitest programmatically

docs/api/test.md

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -261,47 +261,42 @@ Whether the test is expected to fail. If it does, the test will pass, otherwise
261261

262262
- **Alias:** `it.extend`
263263

264-
Use `test.extend` to extend the test context with custom fixtures. This will return a new `test` and it's also extendable, so you can compose more fixtures or override existing ones by extending it as you need. See [Extend Test Context](/guide/test-context.html#test-extend) for more information.
264+
Use `test.extend` to extend the test context with custom fixtures. This will return a new `test` and it's also extendable, so you can compose more fixtures or override existing ones by extending it as you need. See [Extend Test Context](/guide/test-context#extend-test-context) for more information.
265265

266266
```ts
267267
import { test as baseTest, expect } from 'vitest'
268268
269-
const todos = []
270-
const archive = []
271-
272-
const test = baseTest.extend({
273-
todos: async ({ task }, use) => {
274-
todos.push(1, 2, 3)
275-
await use(todos)
276-
todos.length = 0
277-
},
278-
archive,
279-
})
280-
281-
test('add item', ({ todos }) => {
282-
expect(todos.length).toBe(3)
269+
export const test = baseTest
270+
// Simple value - type is inferred as { port: number; host: string }
271+
.extend('config', { port: 3000, host: 'localhost' })
272+
// Function fixture - type is inferred from return value
273+
.extend('server', async ({ config }) => {
274+
// TypeScript knows config is { port: number; host: string }
275+
return `http://${config.host}:${config.port}`
276+
})
283277

284-
todos.push(4)
285-
expect(todos.length).toBe(4)
278+
test('server uses correct port', ({ config, server }) => {
279+
// TypeScript knows the types:
280+
// - config is { port: number; host: string }
281+
// - server is string
282+
expect(server).toBe('http://localhost:3000')
283+
expect(config.port).toBe(3000)
286284
})
287285
```
288286

289-
## test.scoped <Version>3.1.0</Version> {#test-scoped}
290-
291-
- **Alias:** `it.scoped`
287+
## test.override <Version>4.1.0</Version> {#test-override}
292288

293-
Use `test.scoped` to override fixture values for all tests within the current suite and its nested suites. This must be called at the top level of a `describe` block. See [Scoping Values to Suite](/guide/test-context.html#scoping-values-to-suite) for more information.
289+
Use `test.override` to override fixture values for all tests within the current suite and its nested suites. This must be called at the top level of a `describe` block. See [Overriding Fixture Values](/guide/test-context.html#overriding-fixture-values) for more information.
294290

295291
```ts
296292
import { test as baseTest, describe, expect } from 'vitest'
297293

298-
const test = baseTest.extend({
299-
dependency: 'default',
300-
dependant: ({ dependency }, use) => use({ dependency }),
301-
})
294+
const test = baseTest
295+
.extend('dependency', 'default')
296+
.extend('dependant', ({ dependency }) => dependency)
302297

303298
describe('use scoped values', () => {
304-
test.scoped({ dependency: 'new' })
299+
test.override({ dependency: 'new' })
305300

306301
test('uses scoped value', ({ dependant }) => {
307302
// `dependant` uses the new overridden value that is scoped
@@ -311,6 +306,16 @@ describe('use scoped values', () => {
311306
})
312307
```
313308

309+
## test.scoped <Version>3.1.0</Version> <Deprecated /> {#test-scoped}
310+
311+
- **Alias:** `it.scoped`
312+
313+
::: danger DEPRECATED
314+
`test.scoped` is deprecated in favor of [`test.override`](#test-override) and will be removed in a future major version.
315+
:::
316+
317+
Alias of [`test.override`](#test-override)
318+
314319
## test.skip
315320

316321
- **Alias:** `it.skip`
@@ -661,6 +666,10 @@ test.concurrent.for([
661666

662667
Scoped `describe`. See [describe](/api/describe) for more information.
663668

669+
## test.suite <Version>4.1.0</Version> {#test-suite}
670+
671+
Alias for `suite`. See [describe](/api/describe) for more information.
672+
664673
## test.beforeEach
665674

666675
Scoped `beforeEach` hook that inherits types from [`test.extend`](#test-extend). See [beforeEach](/api/hooks#beforeeach) for more information.
@@ -671,19 +680,19 @@ Scoped `afterEach` hook that inherits types from [`test.extend`](#test-extend).
671680

672681
## test.beforeAll
673682

674-
Scoped `beforeAll` hook. See [beforeAll](/api/hooks#beforeall) for more information.
683+
Scoped `beforeAll` hook that inherits types from [`test.extend`](#test-extend). See [beforeAll](/api/hooks#beforeall) for more information.
675684

676685
## test.afterAll
677686

678-
Scoped `afterAll` hook. See [afterAll](/api/hooks#afterall) for more information.
687+
Scoped `afterAll` hook that inherits types from [`test.extend`](#test-extend). See [afterAll](/api/hooks#afterall) for more information.
679688

680689
## test.aroundEach <Version>4.1.0</Version> {#test-aroundeach}
681690

682691
Scoped `aroundEach` hook that inherits types from [`test.extend`](#test-extend). See [aroundEach](/api/hooks#aroundeach) for more information.
683692

684693
## test.aroundAll <Version>4.1.0</Version> {#test-aroundall}
685694

686-
Scoped `aroundAll` hook. See [aroundAll](/api/hooks#aroundall) for more information.
695+
Scoped `aroundAll` hook that inherits types from [`test.extend`](#test-extend). See [aroundAll](/api/hooks#aroundall) for more information.
687696

688697
## bench <Experimental /> {#bench}
689698

0 commit comments

Comments
 (0)