Migrating from Jest to Vitest: Resolving Mock Hoisting Differences
0 reputation · 18 Nov 2024, 05:40 UTC
Goal
Gradually shift a small application’s test suite from Jest to Vitest while keeping local development and CI pipelines operational.
Constraints
Vitest runs in a separate Node environment and hoists vi.mock calls statically above all imports, unlike Jest’s transform‑based timing. Path aliases and browser globals must be re‑declared, and in‑memory fixtures used by the legacy runner are not shared with Vitest’s isolated workers.
Unresolved Decision
When migrating a test that relies on import order, will Vitest’s hoisting break the original execution flow? Is there a configuration knob to align mock timing with Jest, or must the test be refactored? Additionally, can we maintain shared fixtures across both runners without duplicating state?
Questions
- Does Vitest provide a way to defer
vi.mockhoisting to match Jest’s behavior? - What is the impact on import order when a migrated test imports a module that itself imports a mocked dependency?
- Can shared in‑memory fixtures be made available to both Jest and Vitest during parallel execution?