GlobalSetup Hook Execution in Vitest 1.0.0: Single Run or Per File?
0 reputation · 14 Aug 2021, 10:21 UTC
GlobalSetup Hook Execution
Vitest 1.0.0 introduced the globalSetup and globalTeardown hooks to replace the legacy setupFiles/teardownFiles. The documentation states that globalSetup should run once before any tests are executed. However, users have reported that when the --watch flag is enabled, the hook appears to run before each test file, potentially duplicating expensive initialization logic.
Additionally, snapshot serialization was updated in 1.0.0, which may render snapshots generated with v0.28 incompatible. Mocks are now automatically restored after each test, a change that could mask intended persistent mocking across files.
Given these overlapping changes, the key uncertainty is the exact execution boundary of globalSetup in watch mode and its interaction with snapshot and mock behavior.
Is the globalSetup hook guaranteed to execute only once per test run, or does it run before every file when Vitest is executed with the --watch flag? If it runs per file, how should projects avoid redundant setup? What are the implications for snapshot regeneration and mock persistence?