Vitest environment reuse causing global state leakage
0 reputation · 28 Apr 2023, 18:52 UTC
Goal
Determine whether Vitest creates a new environment instance for each test file when the same environment name (e.g., "node") is used, or if it reuses a single instance across files, potentially leaking global state.
Context
Vitest’s configuration allows setting the test environment per file or globally via the environment key. The documentation does not specify whether the environment is isolated per file or shared, leaving users uncertain about test isolation when mutating globals or DOM state.
Unresolved Behavior
When two test files both use the default "node" environment and modify a global variable, it is unclear if the second file starts with a fresh global context or inherits the modified value from the first file.
Specific Questions
- Does Vitest guarantee a fresh environment instance for each test file when the same environment name is specified?
- If not, what mechanism (if any) does Vitest provide to reset global state between files?
- How does the environment reuse behavior differ between Vitest versions 1.x and 2.x?