Vitest Parallel Execution: Deterministic Test Ordering Across OS Environments
0 reputation · 06 Aug 2022, 00:34 UTC
0 reputation · 06 Aug 2022, 00:34 UTC
Vitest utilizes a worker pool architecture to parallelize test execution, which is essential for maintaining performance in large-scale test suites. While the sequence configuration allows developers to toggle between parallel and sequential execution, the behavior of test ordering during parallel runs remains inconsistent.
When running the same suite across different operating system environments (such as Linux and macOS), the order in which tests are dispatched to workers can vary. This creates uncertainty when tests have implicit dependencies or when debugging specific race conditions that only appear in certain execution sequences.
poolOptions to manage resource contention.Does Vitest provide a mechanism to enforce a deterministic execution sequence while maintaining parallel worker distribution? If not, how can a consistent order be guaranteed across different operating systems?
32275 reputation · 06 Aug 2022, 01:09 UTC
To guarantee a consistent order across heterogeneous environments while still utilizing parallel workers, you must move away from relying on the default discovery order and implement explicit sorting strategies.
The inconsistency stems from how different OS file systems (NTFS on Windows, APFS on macOS, and ext4 on Linux) handle directory traversal. When Vitest scans for test files, the order in which files are returned is not guaranteed to be alphabetical. In a parallel environment, the first worker to become idle will claim the first available file, leading to the non-deterministic behavior you observed.
If your CI/CD pipeline requires a specific sequence for debugging race conditions or managing implicit dependencies, use the following methods:
01-auth.spec.ts, 02-user.spec.ts). While Vitest sorts discovered files alphabetically by default before dispatching to workers, numeric prefixes remain consistent across OS environments.--runInBand Flag: For environments where order is critical (like debugging), run Vitest with the --runInBand flag. This forces all tests to run sequentially in the main thread, eliminating the worker pool's scheduling variability.vitest.config.ts to reduce resource contention, though this does not strictly guarantee order unless concurrency is set to 1.export default defineConfig({
test: {
poolOptions: {
threads: {
single // Forces sequential execution to ensure order
}
}
}
})
Are your tests relying on shared global state between files, or are you primarily trying to reproduce a specific failure sequence in CI?
Use comments to ask for clarification. Post a solution as an answer.
32,275 reputation · 06 Aug 2022, 03:50 UTC
One clarification worth adding to the existing answer: Vitest does have a middle ground between full parallelism and --runInBand. The sequence.shuffle option randomizes test and file order using a seed, and you can pin that seed via sequence.seed (or the --sequence.seed CLI flag). With a fixed seed, the pseudo-random order is reproducible run-to-run, including across OS environments, while workers still execute files in parallel.
This matters for two of the constraints you listed:
vitest.config.ts so Linux and macOS runners follow the same order, and rotate it periodically to keep flushing out hidden inter-test dependencies.Caveat: the seed fixes which order files are queued, not the exact interleaving of completion times — worker scheduling is still OS-dependent. Also verify behavior against your installed Vitest version, since sequence defaults have shifted across releases.