Skip to content

Commit 5ec44b7

Browse files
authored
docs: explain globalSetup/setupFiles and add Lifecyle page (#9385)
1 parent fb50d33 commit 5ec44b7

File tree

5 files changed

+374
-34
lines changed

5 files changed

+374
-34
lines changed

docs/.vitepress/config.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,10 @@ export default ({ mode }: { mode: string }) => {
730730
text: 'Test Environment',
731731
link: '/guide/environment',
732732
},
733+
{
734+
text: 'Test Run Lifecycle',
735+
link: '/guide/lifecycle',
736+
},
733737
{
734738
text: 'Snapshot',
735739
link: '/guide/snapshot',
@@ -858,7 +862,7 @@ export default ({ mode }: { mode: string }) => {
858862
},
859863
{
860864
text: 'Advanced',
861-
collapsed: true,
865+
collapsed: false,
862866
items: [
863867
{
864868
text: 'Getting Started',
@@ -947,7 +951,7 @@ export default ({ mode }: { mode: string }) => {
947951
},
948952
{
949953
text: 'Advanced',
950-
collapsed: true,
954+
collapsed: false,
951955
items: [
952956
{
953957
text: 'Vitest',

docs/config/globalsetup.md

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,45 @@ outline: deep
77

88
- **Type:** `string | string[]`
99

10-
Path to global setup files, relative to project root.
10+
Path to global setup files relative to project [root](/config/root).
1111

12-
A global setup file can either export named functions `setup` and `teardown` or a `default` function that returns a teardown function ([example](https://github.com/vitest-dev/vitest/blob/main/test/global-setup/vitest.config.ts)).
12+
A global setup file can either export named functions `setup` and `teardown` or a `default` function that returns a teardown function:
1313

14-
::: info
15-
Multiple globalSetup files are possible. setup and teardown are executed sequentially with teardown in reverse order.
14+
::: code-group
15+
```js [exports]
16+
export function setup(project) {
17+
console.log('setup')
18+
}
19+
20+
export function teardown() {
21+
console.log('teardown')
22+
}
23+
```
24+
```js [default]
25+
export default function setup(project) {
26+
console.log('setup')
27+
28+
return function teardown() {
29+
console.log('teardown')
30+
}
31+
}
32+
```
1633
:::
1734

18-
::: warning
19-
Global setup runs only if there is at least one running test. This means that global setup might start running during watch mode after test file is changed (the test file will wait for global setup to finish before running).
35+
Note that the `setup` method and a `default` function receive a [test project](/api/advanced/test-project) as the first argument. The global setup is called before the test workers are created and only if there is at least one test queued, and teardown is called after all test files have finished running. In [watch mode](/config/watch), the teardown is called before the process is exited instead. If you need to reconfigure your setup before the test rerun, you can use [`onTestsRerun`](#handling-test-reruns) hook instead.
2036

21-
Beware that the global setup is running in a different global scope, so your tests don't have access to variables defined here. However, you can pass down serializable data to tests via [`provide`](#provide) method:
37+
Multiple global setup files are possible. `setup` and `teardown` are executed sequentially with teardown in reverse order.
38+
39+
::: danger
40+
Beware that the global setup is running in a different global scope before test workers are even created, so your tests don't have access to global variables defined here. However, you can pass down serializable data to tests via [`provide`](/config/provide) method and read them in your tests via `inject` imported from `vitest`:
2241

2342
:::code-group
24-
```ts [example.test.js]
43+
```ts [example.test.ts]
2544
import { inject } from 'vitest'
2645

2746
inject('wsPort') === 3000
2847
```
29-
```ts [globalSetup.ts <Version>3.0.0</Version>]
48+
```ts [globalSetup.ts]
3049
import type { TestProject } from 'vitest/node'
3150

3251
export default function setup(project: TestProject) {
@@ -39,22 +58,13 @@ declare module 'vitest' {
3958
}
4059
}
4160
```
42-
```ts [globalSetup.ts <Version>2.0.0</Version>]
43-
import type { GlobalSetupContext } from 'vitest/node'
4461

45-
export default function setup({ provide }: GlobalSetupContext) {
46-
provide('wsPort', 3000)
47-
}
48-
49-
declare module 'vitest' {
50-
export interface ProvidedContext {
51-
wsPort: number
52-
}
53-
}
54-
```
62+
If you need to execute code in the same process as tests, use [`setupFiles`](/config/setupfiles) instead, but note that it runs before every test file.
5563
:::
5664

57-
Since Vitest 3, 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.
65+
### Handling Test Reruns
66+
67+
You can define a custom callback function to be called when Vitest reruns tests. The test 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.
5868

5969
```ts [globalSetup.ts]
6070
import type { TestProject } from 'vitest/node'

docs/config/sequence.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Sharding is happening before sorting, and only if `--shard` option is provided.
2626

2727
If [`sequencer.groupOrder`](#grouporder) is specified, the sequencer will be called once for each group and pool.
2828

29-
## groupOrder
29+
## sequence.groupOrder
3030

3131
- **Type:** `number`
3232
- **Default:** `0`
@@ -97,7 +97,7 @@ Tests in these projects will run in this order:
9797

9898
If you want files and tests to run randomly, you can enable it with this option, or CLI argument [`--sequence.shuffle`](/guide/cli).
9999

100-
Vitest usually uses cache to sort tests, so long running tests start earlier - this makes tests run faster. If your files and tests will run in random order you will lose this performance improvement, but it may be useful to track tests that accidentally depend on another run previously.
100+
Vitest usually uses cache to sort tests, so long-running tests start earlier, which makes tests run faster. If your files and tests run in random order, you will lose this performance improvement, but it may be useful to track tests that accidentally depend on another test run previously.
101101

102102
### sequence.shuffle.files {#sequence-shuffle-files}
103103

docs/config/setupfiles.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,39 @@ outline: deep
77

88
- **Type:** `string | string[]`
99

10-
Path to setup files. They will be run before each test file.
10+
Paths to setup files resolved relative to the [`root`](/config/root). They will run before each _test file_ in the same process. By default, all test files run in parallel, but you can configure it with [`sequence.setupFiles`](/config/sequence#sequence-setupfiles) option.
11+
12+
Vitest will ignore any exports from these files.
13+
14+
:::warning
15+
Note that setup files are executed in the same process as tests, unlike [`globalSetup`](/config/globalsetup) that runs once in the main thread before any test worker is created.
16+
:::
1117

1218
:::info
1319
Editing a setup file will automatically trigger a rerun of all tests.
1420
:::
1521

16-
You can use `process.env.VITEST_POOL_ID` (integer-like string) inside to distinguish between workers.
22+
If you have a heavy process running in the background, you can use `process.env.VITEST_POOL_ID` (integer-like string) inside to distinguish between workers and spread the workload.
1723

18-
:::tip
19-
Note, that if you are running [`--isolate=false`](#isolate), this setup file will be run in the same global scope multiple times. Meaning, that you are accessing the same global object before each test, so make sure you are not doing the same thing more than you need.
20-
:::
24+
:::warning
25+
If [isolation](/config/isolate) is disabled, imported modules are cached, but the setup file itself is executed again before each test file, meaning that you are accessing the same global object before each test file. Make sure you are not doing the same thing more than necessary.
2126

2227
For example, you may rely on a global variable:
2328

2429
```ts
2530
import { config } from '@some-testing-lib'
2631

27-
if (!globalThis.defined) {
32+
if (!globalThis.setupInitialized) {
2833
config.plugins = [myCoolPlugin]
2934
computeHeavyThing()
30-
globalThis.defined = true
35+
globalThis.setupInitialized = true
3136
}
3237

33-
// hooks are reset before each suite
38+
// hooks reset before each test file
3439
afterEach(() => {
3540
cleanup()
3641
})
3742

3843
globalThis.resetBeforeEachTest = true
3944
```
45+
:::

0 commit comments

Comments
 (0)