diff --git a/docs/config/index.md b/docs/config/index.md index 7d1f011d2..156db2bb6 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -974,29 +974,32 @@ Since Vitest 1.0.0-beta, global setup runs only if there is at least one running 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: -```ts -// globalSetup.js +:::code-group +```js [globalSetup.js] export default function setup({ provide }) { provide('wsPort', 3000) } ``` +```ts [globalSetup.ts] +import type { GlobalSetupContext } from 'vitest/node' -```ts -// example.test.js -import { inject } from 'vitest' - -inject('wsPort') === 3000 -``` - -If you are using TypeScript, you can extend `ProvidedContext` type to have type safe access to `provide/inject` methods: +export default function setup({ provide }: GlobalSetupContext) { + provide('wsPort', 3000) +} -```ts +// You can also extend `ProvidedContext` type +// to have type safe access to `provide/inject` methods: declare module 'vitest' { export interface ProvidedContext { wsPort: number } } ``` +```ts [example.test.js] +import { inject } from 'vitest' + +inject('wsPort') === 3000 +``` ::: ### watchExclude diff --git a/packages/vitest/src/node/workspace.ts b/packages/vitest/src/node/workspace.ts index 456a3b4e2..e9920207c 100644 --- a/packages/vitest/src/node/workspace.ts +++ b/packages/vitest/src/node/workspace.ts @@ -114,7 +114,7 @@ export class WorkspaceProject { return this.ctx.getCoreWorkspaceProject() === this } - provide = (key: string, value: unknown) => { + provide = (key: T, value: ProvidedContext[T]) => { try { structuredClone(value) }