Skip to content

Commit c1004e1

Browse files
authoredJun 6, 2023
fix(mocker)!: don't restore mock to the original if module is mocked (#3518)
1 parent 821126f commit c1004e1

File tree

3 files changed

+5
-4
lines changed

3 files changed

+5
-4
lines changed
 

‎examples/mocks/test/automocking.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ test('automock properly restores mock', async () => {
5656
log.warn()
5757
}).not.toThrow()
5858

59-
expect(moduleWithSymbol[methodSymbol]()).toBe('hello')
60-
expect(moduleWithSymbol.warn()).toBe('hello')
59+
expect(moduleWithSymbol[methodSymbol]()).toBe(undefined)
60+
expect(moduleWithSymbol.warn()).toBe(undefined)
6161

6262
expect(log.warn).toHaveProperty('mockImplementation')
6363
})

‎packages/vitest/src/runtime/mocker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ export class VitestMocker {
296296
const mock = spyOn(newContainer, property).mockImplementation(() => undefined)
297297
mock.mockRestore = () => {
298298
mock.mockReset()
299-
mock.mockImplementation(undefined!)
299+
mock.mockImplementation(() => undefined)
300300
return mock
301301
}
302302
// tinyspy retains length, but jest doesn't.

‎test/core/test/mocked-no-mocks.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ test('mocking several modules work', () => {
1515

1616
mockedB()
1717

18-
expect(mockedA).toHaveBeenCalledTimes(2)
18+
// mockedA is not called because mockedB is restored to be undefined
19+
expect(mockedA).toHaveBeenCalledTimes(1)
1920
expect(mockedB).toHaveBeenCalledTimes(1)
2021
})

0 commit comments

Comments
 (0)
Please sign in to comment.