-
-
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
mockClear vs mockReset vs mockRestore #5143
Comments
In your snippet const origProp = obj.prop;
obj.prop = jest.fn().mockImplementation(() => { /* ... */ });
// do something with obj.prop
obj.prop = origProp; You do not have to call Did that help? PR very much welcome for clarification to the docs 🙂 |
Thanks, that did help. 👍 I'm just wondering when is |
For instance: const mockAProp = jest.fn();
jest.mock('some-dep', () => ({ foobar: mockAProp }));
afterEach(() => {
mockAProp.mockReset();
});
test('test 1', () => {
mockAProp.mockImplementation(() => true);
});
test('test 2', () => {
mockAProp.mockImplementation(() => true);
}); |
Thanks! |
Hey all, think this issue is worth looking into again. The The site's pretty good on distinguishing the difference between I also like that Where I see the most confusing is the difference between
There's two main points of confusion here for me. The first is that both mention resetting implementation. The second is that Questions that could be clarified:
|
Any updates on this? |
The updated docs doesn't help? #6227 |
Currently I'm just adding the following any time I use mocks since I believe this should cover all cases whether I use spies or mocks.
|
FTR, here are the answers to the questions raised by @geoffreyyip.
No. It resets the implementation to a new
Yes. It resets the implementation to a new
Here's an example: // contrary to jest.spyOn(), jest.fn() has "replaces" the original implementation
// without keeping a copy for later restoration.
test('...', () => {
const origFn = module.api
module.api = jest.fn()
// act, assert, etc.
// manual restoration
module.api = origFn
}) Here's a passing test suite that clarifies how Further explanation: |
@sepehr, your example is really helpful but I think you forgot to change the text of the final test; it's currently a copy of the first test's text and doesn't describe the actual expected results. |
@tremby, thanks for the feedback and the catch. I've already fixed this in the linked repl, but forgot that there's a hardcoded snippet as well 😅 |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Do you want to request a feature or report a bug?
I would like to ask a question about a feature and then submit a PR to the docs based on the answer.
What is the current behavior?
The docs for
mockReset
say this:What is the expected behavior?
I'm not sure what
mockRestore
does exactly in the context of restoring. Do I need it for restoring a mock's initial implementation? For example:Is the above enough? Do I need to also call
mockReset
or maybe evenmockClear
beforehand? Why do the docs say thatmockReset
is useful for restoring mock to its initial state?My main question: what is
mockReset
useful for?The text was updated successfully, but these errors were encountered: