-
-
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
[Feature]: stop changing the behaviour of spies on mockFn.mockReset
#13229
Comments
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
Again, as far as I know, this behaviour is different from all test libraries derived from Jasmin. Wouldn't it worth to make this change? |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
Keeping this thread alive for a while, or until receiving a response that this behaviour change is not intended. |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
Hey! I'll try to investigate and take a look at this issue :) |
@gerardolima Hey, just to double check if I understand what your problem is. const myObject = {bar: () => 'bar'};
let barStub: any;
beforeAll(() => {
barStub = jest.spyOn(myObject, 'bar');
});
test('run #1', () => {
expect(myObject.bar()).toBe('bar');
barStub.mockReturnValue('POTATO!');
expect(myObject.bar()).toBe('POTATO!');
jest.resetAllMocks();
expect(myObject.bar()).toBe('bar');
}); In this example that I created, Is that it? Only trying to double check to see if I'm following the correct aproach |
yes, @feliperli, your example shows exactly the behaviour I suggested. |
In that case @gerardolima, I believe the What do you think? Does it helps you? |
hey, @feliperli, if you tested, I believe you. Either way, though, don't you think it's strange changing the behaviour of a function which is never mocked on reset? |
@gerardolima Indeed. I'll see what I can do in a PR and see if the maintainers agree with it. |
@cpojer thoughts on this one? |
Yeah, I agree this is basically a bug in Jest and will appreciate a PR fixing it. |
According to this change there's no case for using mockRestore. If it's a spied function it would be the same as mockReset (the issued change), and if it's a mock function (jest.fn()) it makes no sense to call restore at all. That's why I (and probably many others) assumed that the only use case where there would be a difference is in a spy with implementation:
That's why I use mockReset everywhere in my code on spies with implementation - to make sure they go back to be default stubs. I think the purpose of this change wasn't important to begin with, since it was fine so far and didn't cause any unfixable problems (just replace mockReset with mockRestore). A real issue I think is important to notice after this change is that it's now impossible to reset mocks automatically without distinguishing between a mocked function that's mocked by jest.spyOn(...).mock... and a mock that's created by jest.fn(). They would behave differently when resetting them. If I now want to fix it I would create a wrapper that performs both of these on a mock:
Since I can definitely see why one would want to keep the old behavior, and I can't see a special need to keep the new behavior - I think this change should be reverted in the next major. |
Not sure I fully understand what you mean. Are you simply hitting #13808 or is that something else? Could you drop an example, please? |
I think it's a different topic. They are complaining about resetAllMocks specifically. I am pointing out that the actual intended implementation here - mockReset, that is now restoring instead of setting a default stub - seems to be problematic. Problems:
|
Shouldn’t we compare test("resetAllMocks", () => {
const obj = {
two() {
return 2;
},
};
const mock = jest.fn();
const spy = jest.spyOn(obj, "two");
expect(mock()).toBe(undefined);
expect(obj.two()).toBe(2);
mock.mockImplementation(() => 4);
spy.mockImplementation(() => 4);
expect(mock()).toBe(4);
expect(obj.two()).toBe(4);
jest.resetAllMocks();
expect(mock()).toBe(undefined);
expect(obj.two()).toBe(2);
});
Hm.. I think it is as useful as
Do I get it right that you are using |
I came across this issue independently when working on vitest-codemod. I agree with @shlomo-artlist, and have re-opened #13916 where we can continue discussing. |
hey, @shlomo-artlist and @trivikr, I opened this issue because I was having side-effects on methods that had spies, but whose behaviour were never changed. Could you, please, explain how to address the issue described at the code sample at the top) prior to the changes done on the scope of this thread? |
Hey @gerardolima I think it's what @feliperli suggested here: #13229 (comment) |
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. |
🚀 Feature Proposal
I suggest
mockFn.mockReset
andjest.resetAllMocks
to stop changing the behaviour of spies .Motivation
The behaviour of
mockFn.mockReset()
is NOT intuitive: functions whose behaviour should only be observed by Jest actually are changed after the first call ofjest.resetAllMocks()
.In my current setup, I use to create spies and stubs on
beforeAll
and change their behaviour onbeforeEach
. This allows test cases to adapt the stubs according to their specific needs (i.emockFn.mockReturnValue
), without leaking changes into the following test cases.When I created a spy-only
mockFn
from a method of an object, I wouldn't expect for the original behaviour to be erased when I calledjest.resetAllMocks()
.Example
Pitch
This is the behaviour of all other test suits I worked with. If it wasn't by a quick comment on the documentation*, I'd file this as a bug.
The text was updated successfully, but these errors were encountered: