globalSetup TypeScript typing inconsistencies when migrating vitest config across major versions
0 reputation · 01 Jan 2024, 09:16 UTC
0 reputation · 01 Jan 2024, 09:16 UTC
Vitest configures globalSetup and globalTeardown under vitest.config.ts to run once before and after all tests, respectively, enabling shared resource initialization such as database connections or HTTP servers. When TypeScript is used, the return type of these functions must match the configuration expectations; otherwise vitest may emit type errors. However, the interaction between explicitly annotated return types and inferred types during config merging, especially across version transitions or when multiple setup hooks compose, remains ambiguous. Global state modifications within setup or teardown can introduce test ordering dependencies, potentially causing flaky results whose root cause is tied to configuration boundaries rather than test logic.
35775 reputation · 01 Jan 2024, 11:38 UTC
If you see TypeScript errors after merging configs, they are likely caused by conflicting inferred return types (e.g., one setup returns a value, another returns undefined) that tsc cannot reconcile. The error originates from the type checker, not from Vitest’s runtime.
Assuming you rely on teardown order for cleanup of shared resources, any change in config merge order (e.g., adding a plugin that prepends a setup) could flip the teardown sequence and expose ordering‑dependent flakiness.
Assuming you upgraded Vitest without changing your setup files, most suites will continue to pass; failures usually stem from importing internal Vitest types that were moved or renamed in 3.x.
npx tsc --noEmit (or vue-tsc) against your vitest.config.ts and setup files to confirm the exact type mismatch.npx vitest --version and inspect the bundled declarations (node_modules/vitest/dist/*.d.ts) for the globalSetup option’s expected type.console.log in each setup/teardown and run the test suite once to observe the actual execution order.To determine whether the type error is due to a conflicting inferred return type or a missing import, please share the exact TypeScript error message you see when running tsc.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.