-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
jest.resetAllMocks also resets manual mocks #10419
Comments
Your sentence "there is no way to return to the original, manually mocked implementation" sums it all. I've spent hours playing with clearAllMocks, resetAllMocks, restoreAllMocks, resetModules trying to find a clean solution. There are none. In my opinion this is what you would expect from restoreAllMocks instead of "only works when the mock was created with jest.spyOn". What's the point of having // __mocks__/npm-package.js
export const foo = jest.fn(() => 'original'); // mytest.test.js
import { foo } from 'npm-package';
//beforeEach(jest.restoreAllMocks);
test('original', () => {
expect(foo()).toEqual('original');
});
test('override', () => {
foo.mockReturnValue('override');
expect(foo()).toEqual('override');
});
test('original', () => {
jest.restoreAllMocks();
// FAIL
// restoreAllMocks/resetAllMocks assigns jest.fn() to foo instead of using __mocks__/npm-package.js
expect(foo()).toEqual('original');
}); Related: |
I agree with this sentiment. I could definitely use a way to reset back to the original mock. In my case I'm trying to mock the knex module. Currently I'm doing the following:
When i override one of the properties to provide a specific value for my test I then cant reset it back to the original mock as iv lost the context of |
see also #7573 that's similar. |
I just wasted hours trying to figure out why my Why would you ever want to reset manual mocks to essentially be an empty Also, this only seems to apply to manual mocks of user modules. Manual mocks of node modules seem to work fine with |
Just ran into this today. I can't find any function that would reset I can work around by ordering the tests differently (yuck). This is just nasty. |
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days. |
Please don't close 🙏 (I hate those bots) |
This issue makes |
🐛 Bug Report
Calling jest.resetAllMocks() resets manual mocks created via a _ _ mocks _ _ folder. I'm calling this a bug because it means there is no way to return to the original, manually mocked implementation. The manual mock is wiped out by resetAllMocks(); It also means that you can't easily mix and match manual mocks and the mockResolvedValue, mockValue, mockImplementation, etc. methods.
To Reproduce
Use a manual mock, and call
jest.resetAllMocks()
Expected behavior
I expect the manual mock to be restored to its manually mocked state, and not to a blank mock.
Link to repl or repo (highly encouraged)
I couldn't create a _ _ mocks _ _ folder in repl.it, can upload a repo later if required.
envinfo
The text was updated successfully, but these errors were encountered: