diff --git a/docs/config/index.md b/docs/config/index.md index 499204c6fbb8..16675e53fcc8 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -94,7 +94,7 @@ Handling for dependencies resolution. #### deps.experimentalOptimizer - **Type:** `DepOptimizationConfig & { enabled: boolean }` -- **Version:** Vitets 0.29.0 +- **Version:** Since Vitest 0.29.0 - **See also:** [Dep Optimization Options](https://vitejs.dev/config/dep-optimization-options.html) Enable dependency optimization. If you have a lot of tests, this might improve their performance. @@ -1162,7 +1162,7 @@ Path to cache directory. ### sequence -- **Type**: `{ sequencer?, shuffle?, seed?, hooks? }` +- **Type**: `{ sequencer?, shuffle?, seed?, hooks?, setupFiles? }` Options for how tests should be sorted. @@ -1211,6 +1211,18 @@ Changes the order in which hooks are executed. - `list` will order all hooks in the order they are defined - `parallel` will run hooks in a single group in parallel (hooks in parent suites will still run before the current suite's hooks) +#### sequence.setupFiles + +- **Type**: `'list' | 'parallel'` +- **Default**: `'parallel'` +- **CLI**: `--sequence.setupFiles=` +- **Version**: Since Vitest 0.29.3 + +Changes the order in which setup files are executed. + +- `list` will run setup files in the order they are defined +- `parallel` will run setup files in parallel + ### typecheck Options for configuring [typechecking](/guide/testing-types) test environment. diff --git a/packages/runner/src/setup.ts b/packages/runner/src/setup.ts index 750a57a18c4d..32c365b3037a 100644 --- a/packages/runner/src/setup.ts +++ b/packages/runner/src/setup.ts @@ -3,9 +3,15 @@ import type { VitestRunner, VitestRunnerConfig } from './types' export async function runSetupFiles(config: VitestRunnerConfig, runner: VitestRunner) { const files = toArray(config.setupFiles) - await Promise.all( - files.map(async (fsPath) => { + if (config.sequence.setupFiles === 'parallel') { + await Promise.all( + files.map(async (fsPath) => { + await runner.importFile(fsPath, 'setup') + }), + ) + } + else { + for (const fsPath of files) await runner.importFile(fsPath, 'setup') - }), - ) + } } diff --git a/packages/runner/src/types/runner.ts b/packages/runner/src/types/runner.ts index 8ba40c29abb8..6511441cef1b 100644 --- a/packages/runner/src/types/runner.ts +++ b/packages/runner/src/types/runner.ts @@ -1,4 +1,4 @@ -import type { File, SequenceHooks, Suite, TaskResult, Test, TestContext } from './tasks' +import type { File, SequenceHooks, SequenceSetupFiles, Suite, TaskResult, Test, TestContext } from './tasks' export interface VitestRunnerConfig { root: string @@ -11,6 +11,7 @@ export interface VitestRunnerConfig { shuffle?: boolean seed: number hooks: SequenceHooks + setupFiles: SequenceSetupFiles } maxConcurrency: number testTimeout: number diff --git a/packages/runner/src/types/tasks.ts b/packages/runner/src/types/tasks.ts index c917deb5739e..2a5719c29a66 100644 --- a/packages/runner/src/types/tasks.ts +++ b/packages/runner/src/types/tasks.ts @@ -230,3 +230,4 @@ export interface TestContext { export type OnTestFailedHandler = (result: TaskResult) => Awaitable export type SequenceHooks = 'stack' | 'list' | 'parallel' +export type SequenceSetupFiles = 'list' | 'parallel' diff --git a/packages/vitest/src/types/config.ts b/packages/vitest/src/types/config.ts index 5ddb7a3a704a..7bd231f47f71 100644 --- a/packages/vitest/src/types/config.ts +++ b/packages/vitest/src/types/config.ts @@ -1,6 +1,7 @@ import type { AliasOptions, CommonServerOptions, DepOptimizationConfig } from 'vite' import type { PrettyFormatOptions } from 'pretty-format' import type { FakeTimerInstallOpts } from '@sinonjs/fake-timers' +import type { SequenceHooks, SequenceSetupFiles } from '@vitest/runner' import type { BuiltinReporters } from '../node/reporters' import type { TestSequencerConstructor } from '../node/sequencers/types' import type { CoverageOptions, ResolvedCoverageOptions } from './coverage' @@ -10,11 +11,12 @@ import type { SnapshotStateOptions } from './snapshot' import type { Arrayable } from './general' import type { BenchmarkUserOptions } from './benchmark' +export type { SequenceHooks, SequenceSetupFiles } from '@vitest/runner' + export type BuiltinEnvironment = 'node' | 'jsdom' | 'happy-dom' | 'edge-runtime' // Record is used, so user can get intellisense for builtin environments, but still allow custom environments export type VitestEnvironment = BuiltinEnvironment | (string & Record) export type CSSModuleScopeStrategy = 'stable' | 'scoped' | 'non-scoped' -export type SequenceHooks = 'stack' | 'list' | 'parallel' export type ApiConfig = Pick @@ -498,6 +500,13 @@ export interface InlineConfig { * @default false */ shuffle?: boolean + /** + * Defines how setup files should be ordered + * - 'parallel' will run all setup files in parallel + * - 'list' will run all setup files in the order they are defined in the config file + * @default 'parallel' + */ + setupFiles?: SequenceSetupFiles /** * Seed for the random number generator. * @default Date.now() @@ -648,6 +657,7 @@ export interface ResolvedConfig extends Omit, 'config' | 'f sequence: { sequencer: TestSequencerConstructor hooks: SequenceHooks + setupFiles: SequenceSetupFiles shuffle?: boolean seed: number }