diff --git a/packages/vitest/src/runtime/mocker.ts b/packages/vitest/src/runtime/mocker.ts index c8414e7c6f9..c6f50186153 100644 --- a/packages/vitest/src/runtime/mocker.ts +++ b/packages/vitest/src/runtime/mocker.ts @@ -291,7 +291,7 @@ export class VitestMocker { const id = this.normalizePath(path) const mock = this.mockMap.get(suitefile) - if (mock?.[id]) + if (mock && id in mock) delete mock[id] const mockId = this.getMockPath(id) diff --git a/test/core/test/fixtures/mocked-dependency.ts b/test/core/test/fixtures/mocked-dependency.ts new file mode 100644 index 00000000000..ebbc7dcde87 --- /dev/null +++ b/test/core/test/fixtures/mocked-dependency.ts @@ -0,0 +1,3 @@ +export function helloWorld(): void { + throw new Error('not implemented') +} diff --git a/test/core/test/unmock-import.test.ts b/test/core/test/unmock-import.test.ts index c969ccec147..d7123feb237 100644 --- a/test/core/test/unmock-import.test.ts +++ b/test/core/test/unmock-import.test.ts @@ -8,6 +8,7 @@ beforeEach(() => { }, })) }) + afterEach(() => { vi.doUnmock('/data') }) @@ -24,3 +25,13 @@ test('second import should had been re-mock', async () => { const { data } = await import('/data') expect(data.state).toBe('STARTED') }) + +test('unmock should clear modules replaced with imitation', async () => { + vi.doMock('./fixtures/mocked-dependency') + const { helloWorld } = await import('./fixtures/mocked-dependency') + expect(vi.isMockFunction(helloWorld)).toBe(true) + + vi.doUnmock('./fixtures/mocked-dependency') + const { helloWorld: unmocked } = await import('./fixtures/mocked-dependency') + expect(vi.isMockFunction(unmocked)).toBe(false) +})