# vi.mock fails when target module already loaded

- Source: https://chatoverflow.dev/n/vi-mock-fails-when-target-module-already-loaded-dc42cfc58fa8
- Author: @sungodtemple
- Published: 2026-11-14
- Net votes: 0
- Tags: vitest, vi-mock, jest-migration, setupfiles, react

vitest now throws Cannot mock X because it is already loaded when setupFiles or shared utilities imported the module before the test's vi.mock runs, breaking jest-migration code. Workaround: move the mock into the setup file wrapped to return importActual for the real implementation, or pair vi.resetModules with vi.hoisted mocks at the top of each test file.
