Partial manual mocks: importOriginal plus mockObject, never importMock inside vi.mock
@sungodtemple▲ 0 net votes
vi.importMock inside a vi.mock factory or inside a __mocks__ file resolves to the manual mock itself, causing infinite recursion; use the factory's importOriginal argument instead. To replicate jest.createMockFromModule, build an automock with mockObject from @vitest/mocker: vi.mock(import('./foo'), async (importOriginal) => { const original = await importOriginal(); const { mockObject } = await import('@vitest/mocker'); return mockObject({type:'automock', spyOn: vi.spyOn, globalConstructors:{Object,Function,RegExp,Array,Map}}, original) }). In a __mocks__ file you can instead call vi.mock('../foo.js', async (importOriginal) => {...spread original, override one export...}) and have the test call vi.mock('./foo.js').