fix: .mockRestore() should restore original implementation when calling getMockImplementation()#5352
Conversation
…tMockImplementation()
✅ Deploy Preview for fastidious-cascaron-4ded94 canceled.
|
|
Current behavior is correct. Restoring the mock should remove |
|
@sheremet-va I don't follow your reasoning completely. When I create a mock function with const mock = vi.fn(() => true)Then initially the values are like this: expect(mock()).toBe(true)
expect(mock.getMockImplementation()).toBeDefined()
expect(mock.getMockImplementation()?.()).toBe(true)When I restore the mock, I would expect, the mock implementation to be the same as it was initially, right? But instead it is: mock.restore();
expect(mock()).toBe(true)
expect(testFn.getMockImplementation()).toBe(undefined)This seems at least asymmetric? I think I would get your reasoning, if initially, the implementation and the mock implementation would also be different: const mock = vi.fn(() => true)
expect(mock()).toBe(true)
expect(testFn.getMockImplementation()).toBe(undefined)But maybe that is just me. If I follow your reasoning, would you agree that it would make sense then if we can get a |
|
Anyway, will close this. And use |
You have a point. But what we should do is the opposite - mock.mockRestore();
expect(mock()).toBe(undefined)I created an issue - #5436 |
Description
We do some shady stuff in storybook, trying to automatically showing vitest spy in the storybook action panel:
https://github.com/storybookjs/storybook/blob/1d765656e8d3657baaf1b025c2571be48078171f/code/addons/actions/src/loaders.ts#L20-L29
However, this does not work after a spy is restored. As value.getMockImplementation() won't the original implementation, which I believe it should after restoring (not resetting) a mock. I think this is a bug in @vitest/spy.
There is a workaround though, that I implemented in this PR:
storybookjs/storybook#26364
But it seems weird that the
getMockImplementation()value is out of sync with what is actually being "implemented" after restoring the spy. So feels like a bug to me, especially given the documentation:Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
pnpm-lock.yamlunless you introduce a new test example.Tests
pnpm test:ci.Documentation
pnpm run docscommand.Changesets
feat:,fix:,perf:,docs:, orchore:.