Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mocker)!: don't restore mock to the original if module is mocked #3518

Merged
merged 3 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/mocks/test/automocking.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/runtime/mocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,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.
Expand Down
3 changes: 2 additions & 1 deletion test/core/test/mocked-no-mocks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})