Skip to content

Commit ca743ed

Browse files
authored
docs: deprecate old context augmentation and recommend test.extend (#7703)
1 parent 2fa763a commit ca743ed

File tree

8 files changed

+105
-56
lines changed

8 files changed

+105
-56
lines changed

docs/advanced/runner.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ interface Test<ExtraContext = object> extends TaskBase {
209209
*/
210210
file: File
211211
/**
212-
* Whether the task was skipped by calling `t.skip()`.
212+
* Whether the task was skipped by calling `context.skip()`.
213213
*/
214214
pending?: boolean
215215
/**

docs/api/index.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,16 +1279,6 @@ test('performs an organization query', async () => {
12791279

12801280
::: tip
12811281
This hook is always called in reverse order and is not affected by [`sequence.hooks`](/config/#sequence-hooks) option.
1282-
1283-
<!-- TODO: should it be called? https://github.com/vitest-dev/vitest/pull/7069 -->
1284-
Note that this hook is not called if test was skipped with a dynamic `ctx.skip()` call:
1285-
1286-
```ts{2}
1287-
test('skipped dynamically', (t) => {
1288-
onTestFinished(() => {}) // not called
1289-
t.skip()
1290-
})
1291-
```
12921282
:::
12931283

12941284
### onTestFailed

docs/guide/cli-generated.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,13 @@ Omit annotation lines from the output (default: `false`)
761761

762762
Print basic prototype Object and Array (default: `true`)
763763

764+
### diff.maxDepth
765+
766+
- **CLI:** `--diff.maxDepth <maxDepth>`
767+
- **Config:** [diff.maxDepth](/config/#diff-maxdepth)
768+
769+
Limit the depth to recurse when printing nested objects (default: `20`)
770+
764771
### diff.truncateThreshold
765772

766773
- **CLI:** `--diff.truncateThreshold <threshold>`

docs/guide/test-context.md

Lines changed: 82 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ The first argument for each test callback is a test context.
1414
```ts
1515
import { it } from 'vitest'
1616

17-
it('should work', (ctx) => {
17+
it('should work', ({ task }) => {
1818
// prints name of the test
19-
console.log(ctx.task.name)
19+
console.log(task.name)
2020
})
2121
```
2222

2323
## Built-in Test Context
2424

25-
#### `context.task`
25+
#### `task`
2626

2727
A readonly object containing metadata about the test.
2828

29-
#### `context.expect`
29+
#### `expect`
3030

3131
The `expect` API bound to the current test:
3232

@@ -52,7 +52,12 @@ it.concurrent('math is hard', ({ expect }) => {
5252
})
5353
```
5454

55-
#### `context.skip`
55+
#### `skip`
56+
57+
```ts
58+
function skip(note?: string): never
59+
function skip(condition: boolean, note?: string): void
60+
```
5661

5762
Skips subsequent test execution and marks test as skipped:
5863

@@ -65,6 +70,23 @@ it('math is hard', ({ skip }) => {
6570
})
6671
```
6772

73+
Since Vitest 3.1, it accepts a boolean parameter to skip the test conditionally:
74+
75+
```ts
76+
it('math is hard', ({ skip, mind }) => {
77+
skip(mind === 'foggy')
78+
expect(2 + 2).toBe(5)
79+
})
80+
```
81+
82+
#### `onTestFailed`
83+
84+
The [`onTestFailed`](/api/#ontestfailed) hook bound to the current test. This API is useful if you are running tests concurrently and need to have a special handling only for this specific test.
85+
86+
#### `onTestFinished`
87+
88+
The [`onTestFinished`](/api/#ontestfailed) hook bound to the current test. This API is useful if you are running tests concurrently and need to have a special handling only for this specific test.
89+
6890
## Extend Test Context
6991

7092
Vitest provides two different ways to help you extend the test context.
@@ -73,15 +95,15 @@ Vitest provides two different ways to help you extend the test context.
7395

7496
Like [Playwright](https://playwright.dev/docs/api/class-test#test-extend), you can use this method to define your own `test` API with custom fixtures and reuse it anywhere.
7597

76-
For example, we first create `myTest` with two fixtures, `todos` and `archive`.
98+
For example, we first create the `test` collector with two fixtures: `todos` and `archive`.
7799

78100
```ts [my-test.ts]
79-
import { test } from 'vitest'
101+
import { test as baseTest } from 'vitest'
80102
81103
const todos = []
82104
const archive = []
83105
84-
export const myTest = test.extend({
106+
export const test = baseTest.extend({
85107
todos: async ({}, use) => {
86108
// setup the fixture before each test function
87109
todos.push(1, 2, 3)
@@ -100,16 +122,16 @@ Then we can import and use it.
100122

101123
```ts [my-test.test.ts]
102124
import { expect } from 'vitest'
103-
import { myTest } from './my-test.js'
125+
import { test } from './my-test.js'
104126
105-
myTest('add items to todos', ({ todos }) => {
127+
test('add items to todos', ({ todos }) => {
106128
expect(todos.length).toBe(3)
107129
108130
todos.push(4)
109131
expect(todos.length).toBe(4)
110132
})
111133
112-
myTest('move items from todos to archive', ({ todos, archive }) => {
134+
test('move items from todos to archive', ({ todos, archive }) => {
113135
expect(todos.length).toBe(3)
114136
expect(archive.length).toBe(0)
115137
@@ -119,10 +141,12 @@ myTest('move items from todos to archive', ({ todos, archive }) => {
119141
})
120142
```
121143

122-
We can also add more fixtures or override existing fixtures by extending `myTest`.
144+
We can also add more fixtures or override existing fixtures by extending our `test`.
123145

124146
```ts
125-
export const myTest2 = myTest.extend({
147+
import { test as todosTest } from './my-test.js'
148+
149+
export const test = todosTest.extend({
126150
settings: {
127151
// ...
128152
}
@@ -134,34 +158,35 @@ export const myTest2 = myTest.extend({
134158
Vitest runner will smartly initialize your fixtures and inject them into the test context based on usage.
135159

136160
```ts
137-
import { test } from 'vitest'
161+
import { test as baseTest } from 'vitest'
138162
139-
async function todosFn({ task }, use) {
140-
await use([1, 2, 3])
141-
}
142-
143-
const myTest = test.extend({
144-
todos: todosFn,
163+
const test = baseTest.extend<{
164+
todos: number[]
165+
archive: number[]
166+
}>({
167+
todos: async ({ task }, use) => {
168+
await use([1, 2, 3])
169+
},
145170
archive: []
146171
})
147172
148-
// todosFn will not run
149-
myTest('', () => {})
150-
myTest('', ({ archive }) => {})
173+
// todos will not run
174+
test('skip', () => {})
175+
test('skip', ({ archive }) => {})
151176
152-
// todosFn will run
153-
myTest('', ({ todos }) => {})
177+
// todos will run
178+
test('run', ({ todos }) => {})
154179
```
155180

156181
::: warning
157182
When using `test.extend()` with fixtures, you should always use the object destructuring pattern `{ todos }` to access context both in fixture function and test function.
158183

159184
```ts
160-
myTest('context must be destructured', (context) => { // [!code --]
185+
test('context must be destructured', (context) => { // [!code --]
161186
expect(context.todos.length).toBe(2)
162187
})
163188

164-
myTest('context must be destructured', ({ todos }) => { // [!code ++]
189+
test('context must be destructured', ({ todos }) => { // [!code ++]
165190
expect(todos.length).toBe(2)
166191
})
167192
```
@@ -316,19 +341,46 @@ interface MyFixtures {
316341
archive: number[]
317342
}
318343

319-
const myTest = test.extend<MyFixtures>({
344+
const test = baseTest.extend<MyFixtures>({
320345
todos: [],
321346
archive: []
322347
})
323348

324-
myTest('types are defined correctly', ({ todos, archive }) => {
349+
test('types are defined correctly', ({ todos, archive }) => {
325350
expectTypeOf(todos).toEqualTypeOf<number[]>()
326351
expectTypeOf(archive).toEqualTypeOf<number[]>()
327352
})
328353
```
329354

355+
::: info Type Infering
356+
Note that Vitest doesn't support infering the types when the `use` function is called. It is always preferable to pass down the whole context type as the generic type when `test.extend` is called:
357+
358+
```ts
359+
import { test as baseTest } from 'vitest'
360+
361+
const test = baseTest.extend<{
362+
todos: number[]
363+
schema: string
364+
}>({
365+
todos: ({ schema }, use) => use([]),
366+
schema: 'test'
367+
})
368+
369+
test('types are correct', ({
370+
todos, // number[]
371+
schema, // string
372+
}) => {
373+
// ...
374+
})
375+
```
376+
:::
377+
330378
### `beforeEach` and `afterEach`
331379

380+
::: danger Deprecated
381+
This is an outdated way of extending context and it will not work when the `test` is extended with `test.extend`.
382+
:::
383+
332384
The contexts are different for each test. You can access and extend them within the `beforeEach` and `afterEach` hooks.
333385

334386
```ts
@@ -346,7 +398,7 @@ it('should work', ({ foo }) => {
346398

347399
#### TypeScript
348400

349-
To provide property types for all your custom contexts, you can aggregate the `TestContext` type by adding
401+
To provide property types for all your custom contexts, you can augment the `TestContext` type by adding
350402

351403
```ts
352404
declare module 'vitest' {

packages/runner/src/types/tasks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export interface TaskResult {
149149
/** @private */
150150
note?: string
151151
/**
152-
* Whether the task was skipped by calling `t.skip()`.
152+
* Whether the task was skipped by calling `context.skip()`.
153153
* @internal
154154
*/
155155
pending?: boolean
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { expect, it } from 'vitest';
22

3-
it('skips correctly', (t) => {
4-
t.skip(true)
3+
it('skips correctly', ({ skip }) => {
4+
skip(true)
55
expect.unreachable()
66
})
77

8-
it('doesnt skip correctly', (t) => {
9-
t.skip(false)
8+
it('doesnt skip correctly', ({ skip }) => {
9+
skip(false)
1010
throw new Error('doesnt skip')
1111
})

test/core/test/on-finished.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ it('on-finished regular', () => {
1111
collected.push(2)
1212
})
1313

14-
it('on-finished context', (t) => {
14+
it('on-finished context', ({ onTestFinished }) => {
1515
collected.push(4)
16-
t.onTestFinished(() => {
16+
onTestFinished(() => {
1717
collected.push(6)
1818
})
1919
collected.push(5)
@@ -29,9 +29,9 @@ it.fails('failed finish', () => {
2929
collected.push(null)
3030
})
3131

32-
it.fails('failed finish context', (t) => {
32+
it.fails('failed finish context', ({ onTestFinished }) => {
3333
collected.push(10)
34-
t.onTestFinished(() => {
34+
onTestFinished(() => {
3535
collected.push(12)
3636
})
3737
collected.push(11)

test/reporters/fixtures/default/a.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ describe('a failed', () => {
2626
})
2727

2828
describe('a skipped', () => {
29-
test('skipped with note', (t) => {
30-
t.skip('reason')
29+
test('skipped with note', ({ skip }) => {
30+
skip('reason')
3131
})
3232

33-
test('condition', (t) => {
34-
t.skip(true)
33+
test('condition', ({ skip }) => {
34+
skip(true)
3535
})
3636

37-
test('condition with note', (t) => {
38-
t.skip(true, 'note')
37+
test('condition with note', ({ skip }) => {
38+
skip(true, 'note')
3939
})
4040
})

0 commit comments

Comments
 (0)