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: correctly restore vi.fn implementation #3341

Merged
merged 2 commits into from May 10, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/spy/src/index.ts
Expand Up @@ -324,7 +324,7 @@ export function fn<TArgs extends any[] = any[], R = any>(
export function fn<TArgs extends any[] = any[], R = any>(
implementation?: (...args: TArgs) => R,
): Mock<TArgs, R> {
const enhancedSpy = enhanceSpy(tinyspy.internalSpyOn({ spy: () => {} }, 'spy'))
const enhancedSpy = enhanceSpy(tinyspy.internalSpyOn({ spy: implementation || (() => {}) }, 'spy'))
if (implementation)
enhancedSpy.mockImplementation(implementation)

Expand Down
11 changes: 11 additions & 0 deletions test/core/test/jest-mock.test.ts
Expand Up @@ -321,4 +321,15 @@ describe('jest mock compat layer', () => {
const instance2 = new Fn()
expect(Fn.mock.instances[1]).toBe(instance2)
})

it('.mockRestore() should restore initial implementation', () => {
const testFn = vi.fn(() => true)
expect(testFn()).toBe(true)

testFn.mockReturnValue(false)
expect(testFn()).toBe(false)

testFn.mockRestore()
expect(testFn()).toBe(true)
})
})