Skip to content

Commit

Permalink
docs: clarify how to use local test context (#1829)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
Tanimodori and antfu committed Aug 10, 2022
1 parent bee20f6 commit 39b19cf
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion docs/guide/test-context.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ it('should work', ({ foo }) => {

### TypeScript

To provide types for your custom context properties, you can aggregate the `TestContext` type by adding
To provide property types for all your custom contexts, you can aggregate the `TestContext` type by adding

```ts
declare module 'vitest' {
Expand All @@ -58,3 +58,20 @@ declare module 'vitest' {
}
```

If you want to provide property types only for specific `beforeEach`, `afterEach`, `it` and `test` hooks, you can pass the type as a generic.

```ts
interface LocalTestContext {
foo: string
}

beforeEach<LocalTestContext>(async (context) => {
// typeof context is 'TestContext & LocalTestContext'
context.foo = 'bar'
})

it<LocalTestContext>('should work', ({ foo }) => {
// typeof foo is 'string'
console.log(foo) // 'bar'
})
```

0 comments on commit 39b19cf

Please sign in to comment.