Skip to content

Commit

Permalink
feat(config): add an option to run setupFiles in sequence (#3008)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Mar 15, 2023
1 parent 22ca0b6 commit c2e25bb
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 8 deletions.
16 changes: 14 additions & 2 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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=<value>`
- **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.
Expand Down
14 changes: 10 additions & 4 deletions packages/runner/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}),
)
}
}
3 changes: 2 additions & 1 deletion packages/runner/src/types/runner.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -11,6 +11,7 @@ export interface VitestRunnerConfig {
shuffle?: boolean
seed: number
hooks: SequenceHooks
setupFiles: SequenceSetupFiles
}
maxConcurrency: number
testTimeout: number
Expand Down
1 change: 1 addition & 0 deletions packages/runner/src/types/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,4 @@ export interface TestContext {
export type OnTestFailedHandler = (result: TaskResult) => Awaitable<void>

export type SequenceHooks = 'stack' | 'list' | 'parallel'
export type SequenceSetupFiles = 'list' | 'parallel'
12 changes: 11 additions & 1 deletion packages/vitest/src/types/config.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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<never, never>)
export type CSSModuleScopeStrategy = 'stable' | 'scoped' | 'non-scoped'
export type SequenceHooks = 'stack' | 'list' | 'parallel'

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

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -648,6 +657,7 @@ export interface ResolvedConfig extends Omit<Required<UserConfig>, 'config' | 'f
sequence: {
sequencer: TestSequencerConstructor
hooks: SequenceHooks
setupFiles: SequenceSetupFiles
shuffle?: boolean
seed: number
}
Expand Down

0 comments on commit c2e25bb

Please sign in to comment.