This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Since Vitest 3.1, it accepts a boolean parameter to skip the test conditionally:
```ts
it('math is hard', ({ skip, mind }) => {
skip(mind === 'foggy')
expect(2 + 2).toBe(5)
})
```
#### `onTestFailed`
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.
#### `onTestFinished`
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.
## Extend Test Context
Vitest provides two different ways to help you extend the test context.
Expand All
@@ -73,15 +95,15 @@ Vitest provides two different ways to help you extend the test context.
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.
For example, we first create `myTest` with two fixtures, `todos` and `archive`.
For example, we first create the `test` collector with two fixtures: `todos` and `archive`.
```ts [my-test.ts]
import { test } from 'vitest'
import { test as baseTest } from 'vitest'
const todos = []
const archive = []
export const myTest = test.extend({
export const test = baseTest.extend({
todos: async ({}, use) => {
// setup the fixture before each test function
todos.push(1, 2, 3)
Expand All
@@ -100,16 +122,16 @@ Then we can import and use it.
```ts [my-test.test.ts]
import { expect } from 'vitest'
import { myTest } from './my-test.js'
import { test } from './my-test.js'
myTest('add items to todos', ({ todos }) => {
test('add items to todos', ({ todos }) => {
expect(todos.length).toBe(3)
todos.push(4)
expect(todos.length).toBe(4)
})
myTest('move items from todos to archive', ({ todos, archive }) => {
test('move items from todos to archive', ({ todos, archive }) => {
expect(todos.length).toBe(3)
expect(archive.length).toBe(0)
Expand All
@@ -119,10 +141,12 @@ myTest('move items from todos to archive', ({ todos, archive }) => {
})
```
We can also add more fixtures or override existing fixtures by extending `myTest`.
We can also add more fixtures or override existing fixtures by extending our `test`.
Vitest runner will smartly initialize your fixtures and inject them into the test context based on usage.
```ts
import { test } from 'vitest'
import { test as baseTest } from 'vitest'
async function todosFn({ task }, use) {
await use([1, 2, 3])
}
const myTest = test.extend({
todos: todosFn,
const test = baseTest.extend<{
todos: number[]
archive: number[]
}>({
todos: async ({ task }, use) => {
await use([1, 2, 3])
},
archive: []
})
// todosFn will not run
myTest('', () => {})
myTest('', ({ archive }) => {})
// todos will not run
test('skip', () => {})
test('skip', ({ archive }) => {})
// todosFn will run
myTest('', ({ todos }) => {})
// todos will run
test('run', ({ todos }) => {})
```
::: warning
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.
```ts
myTest('context must be destructured', (context) => { // [!code --]
test('context must be destructured', (context) => { // [!code --]
expect(context.todos.length).toBe(2)
})
myTest('context must be destructured', ({ todos }) => { // [!code ++]
test('context must be destructured', ({ todos }) => { // [!code ++]
expect(todos.length).toBe(2)
})
```
Expand Down
Expand Up
@@ -316,19 +341,46 @@ interface MyFixtures {
archive: number[]
}
const myTest = test.extend<MyFixtures>({
const test = baseTest.extend<MyFixtures>({
todos: [],
archive: []
})
myTest('types are defined correctly', ({ todos, archive }) => {
test('types are defined correctly', ({ todos, archive }) => {
expectTypeOf(todos).toEqualTypeOf<number[]>()
expectTypeOf(archive).toEqualTypeOf<number[]>()
})
```
::: info Type Infering
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:
```ts
import { test as baseTest } from 'vitest'
const test = baseTest.extend<{
todos: number[]
schema: string
}>({
todos: ({ schema }, use) => use([]),
schema: 'test'
})
test('types are correct', ({
todos, // number[]
schema, // string
}) => {
// ...
})
```
:::
### `beforeEach` and `afterEach`
::: danger Deprecated
This is an outdated way of extending context and it will not work when the `test` is extended with `test.extend`.
:::
The contexts are different for each test. You can access and extend them within the `beforeEach` and `afterEach` hooks.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
docs: deprecate old context augmentation and recommend test.extend #7703
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
docs: deprecate old context augmentation and recommend test.extend #7703
Filter by extension
Viewed files
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There are no files selected for viewing