Skip to content

Commit f2066c4

Browse files
authored
docs: improve "provide" documentation (#5554)
1 parent c84113f commit f2066c4

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

docs/config/index.md

+14-11
Original file line numberDiff line numberDiff line change
@@ -974,29 +974,32 @@ Since Vitest 1.0.0-beta, global setup runs only if there is at least one running
974974

975975
Beware that the global setup is running in a different global scope, so your tests don't have access to variables defined here. However, since 1.0.0 you can pass down serializable data to tests via `provide` method:
976976

977-
```ts
978-
// globalSetup.js
977+
:::code-group
978+
```js [globalSetup.js]
979979
export default function setup({ provide }) {
980980
provide('wsPort', 3000)
981981
}
982982
```
983+
```ts [globalSetup.ts]
984+
import type { GlobalSetupContext } from 'vitest/node'
983985

984-
```ts
985-
// example.test.js
986-
import { inject } from 'vitest'
987-
988-
inject('wsPort') === 3000
989-
```
990-
991-
If you are using TypeScript, you can extend `ProvidedContext` type to have type safe access to `provide/inject` methods:
986+
export default function setup({ provide }: GlobalSetupContext) {
987+
provide('wsPort', 3000)
988+
}
992989

993-
```ts
990+
// You can also extend `ProvidedContext` type
991+
// to have type safe access to `provide/inject` methods:
994992
declare module 'vitest' {
995993
export interface ProvidedContext {
996994
wsPort: number
997995
}
998996
}
999997
```
998+
```ts [example.test.js]
999+
import { inject } from 'vitest'
1000+
1001+
inject('wsPort') === 3000
1002+
```
10001003
:::
10011004

10021005
### watchExclude<NonProjectOption />

packages/vitest/src/node/workspace.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export class WorkspaceProject {
114114
return this.ctx.getCoreWorkspaceProject() === this
115115
}
116116

117-
provide = (key: string, value: unknown) => {
117+
provide = <T extends keyof ProvidedContext>(key: T, value: ProvidedContext[T]) => {
118118
try {
119119
structuredClone(value)
120120
}

0 commit comments

Comments
 (0)