docs: test.extend example to destructure context (#7614)

This commit is contained in:
Ari Perkkiö 2025-03-06 10:55:58 +02:00 committed by GitHub
parent 0c2924b7ae
commit 2029c9e36d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -154,6 +154,17 @@ myTest('', ({ 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 --]
expect(context.todos.length).toBe(2)
})
myTest('context must be destructured', ({ todos }) => { // [!code ++]
expect(todos.length).toBe(2)
})
```
:::
#### Automatic fixture
@ -245,9 +256,9 @@ const myTest = test.extend<MyFixtures>({
archive: []
})
myTest('types are defined correctly', (context) => {
expectTypeOf(context.todos).toEqualTypeOf<number[]>()
expectTypeOf(context.archive).toEqualTypeOf<number[]>()
myTest('types are defined correctly', ({ todos, archive }) => {
expectTypeOf(todos).toEqualTypeOf<number[]>()
expectTypeOf(archive).toEqualTypeOf<number[]>()
})
```