Skip to content

Commit

Permalink
fix: don't restore methods in automocked dependencies (#3438)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed May 24, 2023
1 parent 32b5361 commit d1afd26
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
4 changes: 4 additions & 0 deletions examples/mocks/test/automocking.spec.ts
Expand Up @@ -48,6 +48,8 @@ test('automock properly restores mock', async () => {
expect(moduleWithSymbol.warn()).toBeUndefined()
expect(moduleWithSymbol[methodSymbol]()).toBeUndefined()

expect(log.warn).toHaveProperty('mockImplementation')

vi.restoreAllMocks()

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

expect(moduleWithSymbol[methodSymbol]()).toBe('hello')
expect(moduleWithSymbol.warn()).toBe('hello')

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

test('automock has a getter', () => {
Expand Down
7 changes: 6 additions & 1 deletion packages/vitest/src/runtime/mocker.ts
Expand Up @@ -267,7 +267,12 @@ export class VitestMocker {
continue

if (isFunction) {
spyOn(newContainer, property).mockImplementation(() => undefined)
const mock = spyOn(newContainer, property).mockImplementation(() => undefined)
mock.mockRestore = () => {
mock.mockReset()
mock.mockImplementation(undefined!)
return mock
}
// tinyspy retains length, but jest doesn't.
Object.defineProperty(newContainer[property], 'length', { value: 0 })
}
Expand Down

0 comments on commit d1afd26

Please sign in to comment.