You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The [`onTestFailed`](/api/#ontestfailed) hookboundtothecurrenttest. ThisAPIisusefulifyouarerunningtestsconcurrentlyandneedtohaveaspecialhandlingonlyforthisspecifictest.
85
+
86
+
#### `onTestFinished`
87
+
88
+
The [`onTestFinished`](/api/#ontestfailed) hookboundtothecurrenttest. ThisAPIisusefulifyouarerunningtestsconcurrentlyandneedtohaveaspecialhandlingonlyforthisspecifictest.
@@ -73,15 +95,15 @@ Vitest provides two different ways to help you extend the test context.
73
95
74
96
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.
75
97
76
-
For example, we first create `myTest`with two fixtures,`todos` and `archive`.
Whenusing`test.extend()`withfixtures, youshouldalwaysusetheobjectdestructuringpattern`{ todos }`toaccesscontextbothinfixturefunction and test function.
158
183
159
184
```ts
160
-
myTest('context must be destructured', (context) => { // [!code --]
185
+
test('context must be destructured', (context) => { // [!code --]
161
186
expect(context.todos.length).toBe(2)
162
187
})
163
188
164
-
myTest('context must be destructured', ({ todos }) => { // [!code ++]
189
+
test('context must be destructured', ({ todos }) => { // [!code ++]
165
190
expect(todos.length).toBe(2)
166
191
})
167
192
```
@@ -316,19 +341,46 @@ interface MyFixtures {
316
341
archive:number[]
317
342
}
318
343
319
-
constmyTest=test.extend<MyFixtures>({
344
+
consttest=baseTest.extend<MyFixtures>({
320
345
todos: [],
321
346
archive: []
322
347
})
323
348
324
-
myTest('types are defined correctly', ({ todos, archive }) => {
349
+
test('types are defined correctly', ({ todos, archive }) => {
325
350
expectTypeOf(todos).toEqualTypeOf<number[]>()
326
351
expectTypeOf(archive).toEqualTypeOf<number[]>()
327
352
})
328
353
```
329
354
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 { testasbaseTest } 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
+
330
378
### `beforeEach` and `afterEach`
331
379
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
+
332
384
The contexts are different for each test. You can access and extend them within the `beforeEach` and `afterEach` hooks.
0 commit comments