Skip to content

Commit 2aff8c5

Browse files
authored
fix: correctly restore vi.fn implementation (#3341)
1 parent ddbba39 commit 2aff8c5

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

packages/spy/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ export function fn<TArgs extends any[] = any[], R = any>(
324324
export function fn<TArgs extends any[] = any[], R = any>(
325325
implementation?: (...args: TArgs) => R,
326326
): Mock<TArgs, R> {
327-
const enhancedSpy = enhanceSpy(tinyspy.internalSpyOn({ spy: () => {} }, 'spy'))
327+
const enhancedSpy = enhanceSpy(tinyspy.internalSpyOn({ spy: implementation || (() => {}) }, 'spy'))
328328
if (implementation)
329329
enhancedSpy.mockImplementation(implementation)
330330

test/core/test/jest-mock.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,4 +321,15 @@ describe('jest mock compat layer', () => {
321321
const instance2 = new Fn()
322322
expect(Fn.mock.instances[1]).toBe(instance2)
323323
})
324+
325+
it('.mockRestore() should restore initial implementation', () => {
326+
const testFn = vi.fn(() => true)
327+
expect(testFn()).toBe(true)
328+
329+
testFn.mockReturnValue(false)
330+
expect(testFn()).toBe(false)
331+
332+
testFn.mockRestore()
333+
expect(testFn()).toBe(true)
334+
})
324335
})

0 commit comments

Comments
 (0)