From c1004e1493c00cb37ef39c547c51fb8bc49c4203 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Tue, 6 Jun 2023 19:00:24 +0200 Subject: [PATCH] fix(mocker)!: don't restore mock to the original if module is mocked (#3518) --- examples/mocks/test/automocking.spec.ts | 4 ++-- packages/vitest/src/runtime/mocker.ts | 2 +- test/core/test/mocked-no-mocks.test.ts | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/examples/mocks/test/automocking.spec.ts b/examples/mocks/test/automocking.spec.ts index 1eea3c48fa86..17a4b8929df0 100644 --- a/examples/mocks/test/automocking.spec.ts +++ b/examples/mocks/test/automocking.spec.ts @@ -56,8 +56,8 @@ test('automock properly restores mock', async () => { log.warn() }).not.toThrow() - expect(moduleWithSymbol[methodSymbol]()).toBe('hello') - expect(moduleWithSymbol.warn()).toBe('hello') + expect(moduleWithSymbol[methodSymbol]()).toBe(undefined) + expect(moduleWithSymbol.warn()).toBe(undefined) expect(log.warn).toHaveProperty('mockImplementation') }) diff --git a/packages/vitest/src/runtime/mocker.ts b/packages/vitest/src/runtime/mocker.ts index dfcbc0a189ac..4ac7f98a736a 100644 --- a/packages/vitest/src/runtime/mocker.ts +++ b/packages/vitest/src/runtime/mocker.ts @@ -296,7 +296,7 @@ export class VitestMocker { const mock = spyOn(newContainer, property).mockImplementation(() => undefined) mock.mockRestore = () => { mock.mockReset() - mock.mockImplementation(undefined!) + mock.mockImplementation(() => undefined) return mock } // tinyspy retains length, but jest doesn't. diff --git a/test/core/test/mocked-no-mocks.test.ts b/test/core/test/mocked-no-mocks.test.ts index f824a930db9e..6260bf30c41d 100644 --- a/test/core/test/mocked-no-mocks.test.ts +++ b/test/core/test/mocked-no-mocks.test.ts @@ -15,6 +15,7 @@ test('mocking several modules work', () => { mockedB() - expect(mockedA).toHaveBeenCalledTimes(2) + // mockedA is not called because mockedB is restored to be undefined + expect(mockedA).toHaveBeenCalledTimes(1) expect(mockedB).toHaveBeenCalledTimes(1) })