Skip to content

Commit d1afd26

Browse files
authoredMay 24, 2023
fix: don't restore methods in automocked dependencies (#3438)
1 parent 32b5361 commit d1afd26

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed
 

Diff for: ‎examples/mocks/test/automocking.spec.ts

+4
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ test('automock properly restores mock', async () => {
4848
expect(moduleWithSymbol.warn()).toBeUndefined()
4949
expect(moduleWithSymbol[methodSymbol]()).toBeUndefined()
5050

51+
expect(log.warn).toHaveProperty('mockImplementation')
52+
5153
vi.restoreAllMocks()
5254

5355
expect(() => {
@@ -56,6 +58,8 @@ test('automock properly restores mock', async () => {
5658

5759
expect(moduleWithSymbol[methodSymbol]()).toBe('hello')
5860
expect(moduleWithSymbol.warn()).toBe('hello')
61+
62+
expect(log.warn).toHaveProperty('mockImplementation')
5963
})
6064

6165
test('automock has a getter', () => {

Diff for: ‎packages/vitest/src/runtime/mocker.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,12 @@ export class VitestMocker {
267267
continue
268268

269269
if (isFunction) {
270-
spyOn(newContainer, property).mockImplementation(() => undefined)
270+
const mock = spyOn(newContainer, property).mockImplementation(() => undefined)
271+
mock.mockRestore = () => {
272+
mock.mockReset()
273+
mock.mockImplementation(undefined!)
274+
return mock
275+
}
271276
// tinyspy retains length, but jest doesn't.
272277
Object.defineProperty(newContainer[property], 'length', { value: 0 })
273278
}

0 commit comments

Comments
 (0)
Please sign in to comment.