Skip to content

Commit a5bbc0a

Browse files
authored
feat: provide the current project to the global setup (#6942)
1 parent ac1a7fd commit a5bbc0a

File tree

4 files changed

+41
-41
lines changed

4 files changed

+41
-41
lines changed

docs/config/index.md

+20-7
Original file line numberDiff line numberDiff line change
@@ -1065,17 +1065,30 @@ Beware that the global setup is running in a different global scope, so your tes
10651065

10661066
:::code-group
10671067
```js [globalSetup.js]
1068-
export default function setup({ provide }) {
1069-
provide('wsPort', 3000)
1068+
export default function setup(project) {
1069+
project.provide('wsPort', 3000)
10701070
}
10711071
```
1072-
```ts [globalSetup.ts]
1072+
```ts [globalSetup.ts <Version>2.0.0</Version>]
10731073
import type { GlobalSetupContext } from 'vitest/node'
10741074

10751075
export default function setup({ provide }: GlobalSetupContext) {
10761076
provide('wsPort', 3000)
10771077
}
10781078

1079+
declare module 'vitest' {
1080+
export interface ProvidedContext {
1081+
wsPort: number
1082+
}
1083+
}
1084+
```
1085+
```ts [globalSetup.ts <Version>2.2.0</Version>]
1086+
import type { TestProject } from 'vitest/node'
1087+
1088+
export default function setup(project: TestProject) {
1089+
project.provide('wsPort', 3000)
1090+
}
1091+
10791092
declare module 'vitest' {
10801093
export interface ProvidedContext {
10811094
wsPort: number
@@ -1089,13 +1102,13 @@ inject('wsPort') === 3000
10891102
```
10901103
:::
10911104

1092-
Since Vitest 2.2.0, you can define a custom callback function to be called when Vitest reruns tests. If the function is asynchronous, the runner will wait for it to complete before executing the tests.
1105+
Since Vitest 2.2.0, you can define a custom callback function to be called when Vitest reruns tests. If the function is asynchronous, the runner will wait for it to complete before executing tests. Note that you cannot destruct the `project` like `{ onTestsRerun }` because it relies on the context.
10931106

10941107
```ts
1095-
import type { GlobalSetupContext } from 'vitest/node'
1108+
import type { TestProject } from 'vitest/node'
10961109

1097-
export default function setup({ onTestsRerun }: GlobalSetupContext) {
1098-
onTestsRerun(async () => {
1110+
export default function setup(project: TestProject) {
1111+
project.onTestsRerun(async () => {
10991112
await restartDb()
11001113
})
11011114
}

packages/vitest/src/node/globalSetup.ts

+4-20
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,13 @@
11
import type { ViteNodeRunner } from 'vite-node/client'
2-
import type { OnTestsRerunHandler, ProvidedContext } from '../types/general'
3-
import type { ResolvedConfig } from './types/config'
2+
import type { TestProject } from './project'
43
import { toArray } from '@vitest/utils'
54

6-
export interface GlobalSetupContext {
7-
/**
8-
* Config of the current project.
9-
*/
10-
config: ResolvedConfig
11-
/**
12-
* Provide a value to the test context. This value will be available to all tests via `inject`.
13-
*/
14-
provide: <T extends keyof ProvidedContext & string>(
15-
key: T,
16-
value: ProvidedContext[T]
17-
) => void
18-
/**
19-
* Register a function that will be called before tests run again in watch mode.
20-
*/
21-
onTestsRerun: (cb: OnTestsRerunHandler) => void
22-
}
5+
/** @deprecated use `TestProject` instead */
6+
export type GlobalSetupContext = TestProject
237

248
export interface GlobalSetupFile {
259
file: string
26-
setup?: (context: GlobalSetupContext) => Promise<Function | void> | void
10+
setup?: (context: TestProject) => Promise<Function | void> | void
2711
teardown?: Function
2812
}
2913

packages/vitest/src/node/project.ts

+14-11
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import type {
55
InlineConfig as ViteInlineConfig,
66
} from 'vite'
77
import type { Typechecker } from '../typecheck/typechecker'
8-
import type { ProvidedContext } from '../types/general'
8+
import type { OnTestsRerunHandler, ProvidedContext } from '../types/general'
99
import type { Vitest } from './core'
1010
import type { GlobalSetupFile } from './globalSetup'
11+
import type { Logger } from './logger'
1112
import type { BrowserServer } from './types/browser'
1213
import type {
1314
ResolvedConfig,
@@ -87,13 +88,15 @@ export class TestProject {
8788
this.globalConfig = vitest.config
8889
}
8990

91+
// "provide" is a property, not a method to keep the context when destructed in the global setup,
92+
// making it a method would be a breaking change, and can be done in Vitest 3 at minimum
9093
/**
9194
* Provide a value to the test context. This value will be available to all tests with `inject`.
9295
*/
93-
provide<T extends keyof ProvidedContext & string>(
96+
provide = <T extends keyof ProvidedContext & string>(
9497
key: T,
9598
value: ProvidedContext[T],
96-
): void {
99+
): void => {
97100
try {
98101
structuredClone(value)
99102
}
@@ -217,11 +220,7 @@ export class TestProject {
217220
)
218221

219222
for (const globalSetupFile of this._globalSetups) {
220-
const teardown = await globalSetupFile.setup?.({
221-
provide: (key, value) => this.provide(key, value),
222-
config: this.config,
223-
onTestsRerun: cb => this.vitest.onTestsRerun(cb),
224-
})
223+
const teardown = await globalSetupFile.setup?.(this)
225224
if (teardown == null || !!globalSetupFile.teardown) {
226225
continue
227226
}
@@ -234,13 +233,17 @@ export class TestProject {
234233
}
235234
}
236235

236+
onTestsRerun(cb: OnTestsRerunHandler): void {
237+
this.vitest.onTestsRerun(cb)
238+
}
239+
237240
/** @deprecated */
238-
teardownGlobalSetup() {
241+
teardownGlobalSetup(): Promise<void> {
239242
return this._teardownGlobalSetup()
240243
}
241244

242245
/** @internal */
243-
async _teardownGlobalSetup() {
246+
async _teardownGlobalSetup(): Promise<void> {
244247
if (!this._globalSetups) {
245248
return
246249
}
@@ -250,7 +253,7 @@ export class TestProject {
250253
}
251254

252255
/** @deprecated use `vitest.logger` instead */
253-
get logger() {
256+
get logger(): Logger {
254257
return this.vitest.logger
255258
}
256259

test/watch/fixtures/global-setup.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { GlobalSetupContext } from 'vitest/node';
1+
import { TestProject } from 'vitest/node';
22

33
const calls: string[] = [];
44

55
(globalThis as any).__CALLS = calls
66

7-
export default ({ onTestsRerun }: GlobalSetupContext) => {
7+
export default (project: TestProject) => {
88
calls.push('start')
9-
onTestsRerun(() => {
9+
project.onTestsRerun(() => {
1010
calls.push('rerun')
1111
})
1212
return () => {

0 commit comments

Comments
 (0)