Skip to content

Commit c2e25bb

Browse files
authoredMar 15, 2023
feat(config): add an option to run setupFiles in sequence (#3008)
1 parent 22ca0b6 commit c2e25bb

File tree

5 files changed

+38
-8
lines changed

5 files changed

+38
-8
lines changed
 

‎docs/config/index.md

+14-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ Handling for dependencies resolution.
9494
#### deps.experimentalOptimizer
9595

9696
- **Type:** `DepOptimizationConfig & { enabled: boolean }`
97-
- **Version:** Vitets 0.29.0
97+
- **Version:** Since Vitest 0.29.0
9898
- **See also:** [Dep Optimization Options](https://vitejs.dev/config/dep-optimization-options.html)
9999

100100
Enable dependency optimization. If you have a lot of tests, this might improve their performance.
@@ -1162,7 +1162,7 @@ Path to cache directory.
11621162

11631163
### sequence
11641164

1165-
- **Type**: `{ sequencer?, shuffle?, seed?, hooks? }`
1165+
- **Type**: `{ sequencer?, shuffle?, seed?, hooks?, setupFiles? }`
11661166

11671167
Options for how tests should be sorted.
11681168

@@ -1211,6 +1211,18 @@ Changes the order in which hooks are executed.
12111211
- `list` will order all hooks in the order they are defined
12121212
- `parallel` will run hooks in a single group in parallel (hooks in parent suites will still run before the current suite's hooks)
12131213

1214+
#### sequence.setupFiles
1215+
1216+
- **Type**: `'list' | 'parallel'`
1217+
- **Default**: `'parallel'`
1218+
- **CLI**: `--sequence.setupFiles=<value>`
1219+
- **Version**: Since Vitest 0.29.3
1220+
1221+
Changes the order in which setup files are executed.
1222+
1223+
- `list` will run setup files in the order they are defined
1224+
- `parallel` will run setup files in parallel
1225+
12141226
### typecheck
12151227

12161228
Options for configuring [typechecking](/guide/testing-types) test environment.

‎packages/runner/src/setup.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@ import type { VitestRunner, VitestRunnerConfig } from './types'
33

44
export async function runSetupFiles(config: VitestRunnerConfig, runner: VitestRunner) {
55
const files = toArray(config.setupFiles)
6-
await Promise.all(
7-
files.map(async (fsPath) => {
6+
if (config.sequence.setupFiles === 'parallel') {
7+
await Promise.all(
8+
files.map(async (fsPath) => {
9+
await runner.importFile(fsPath, 'setup')
10+
}),
11+
)
12+
}
13+
else {
14+
for (const fsPath of files)
815
await runner.importFile(fsPath, 'setup')
9-
}),
10-
)
16+
}
1117
}

‎packages/runner/src/types/runner.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { File, SequenceHooks, Suite, TaskResult, Test, TestContext } from './tasks'
1+
import type { File, SequenceHooks, SequenceSetupFiles, Suite, TaskResult, Test, TestContext } from './tasks'
22

33
export interface VitestRunnerConfig {
44
root: string
@@ -11,6 +11,7 @@ export interface VitestRunnerConfig {
1111
shuffle?: boolean
1212
seed: number
1313
hooks: SequenceHooks
14+
setupFiles: SequenceSetupFiles
1415
}
1516
maxConcurrency: number
1617
testTimeout: number

‎packages/runner/src/types/tasks.ts

+1
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,4 @@ export interface TestContext {
230230
export type OnTestFailedHandler = (result: TaskResult) => Awaitable<void>
231231

232232
export type SequenceHooks = 'stack' | 'list' | 'parallel'
233+
export type SequenceSetupFiles = 'list' | 'parallel'

‎packages/vitest/src/types/config.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { AliasOptions, CommonServerOptions, DepOptimizationConfig } from 'vite'
22
import type { PrettyFormatOptions } from 'pretty-format'
33
import type { FakeTimerInstallOpts } from '@sinonjs/fake-timers'
4+
import type { SequenceHooks, SequenceSetupFiles } from '@vitest/runner'
45
import type { BuiltinReporters } from '../node/reporters'
56
import type { TestSequencerConstructor } from '../node/sequencers/types'
67
import type { CoverageOptions, ResolvedCoverageOptions } from './coverage'
@@ -10,11 +11,12 @@ import type { SnapshotStateOptions } from './snapshot'
1011
import type { Arrayable } from './general'
1112
import type { BenchmarkUserOptions } from './benchmark'
1213

14+
export type { SequenceHooks, SequenceSetupFiles } from '@vitest/runner'
15+
1316
export type BuiltinEnvironment = 'node' | 'jsdom' | 'happy-dom' | 'edge-runtime'
1417
// Record is used, so user can get intellisense for builtin environments, but still allow custom environments
1518
export type VitestEnvironment = BuiltinEnvironment | (string & Record<never, never>)
1619
export type CSSModuleScopeStrategy = 'stable' | 'scoped' | 'non-scoped'
17-
export type SequenceHooks = 'stack' | 'list' | 'parallel'
1820

1921
export type ApiConfig = Pick<CommonServerOptions, 'port' | 'strictPort' | 'host'>
2022

@@ -498,6 +500,13 @@ export interface InlineConfig {
498500
* @default false
499501
*/
500502
shuffle?: boolean
503+
/**
504+
* Defines how setup files should be ordered
505+
* - 'parallel' will run all setup files in parallel
506+
* - 'list' will run all setup files in the order they are defined in the config file
507+
* @default 'parallel'
508+
*/
509+
setupFiles?: SequenceSetupFiles
501510
/**
502511
* Seed for the random number generator.
503512
* @default Date.now()
@@ -648,6 +657,7 @@ export interface ResolvedConfig extends Omit<Required<UserConfig>, 'config' | 'f
648657
sequence: {
649658
sequencer: TestSequencerConstructor
650659
hooks: SequenceHooks
660+
setupFiles: SequenceSetupFiles
651661
shuffle?: boolean
652662
seed: number
653663
}

0 commit comments

Comments
 (0)
Please sign in to comment.