-
-
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
"resetAllMocks" does not reset all mocks #7083
Comments
Updated to jest 23.6, but the issue is still there. Could you look into this? |
@mushketyk looks like what you want to do with "reset" is actually "clear", so the bug is that mockReset is clearing the mock calls but resetAllMocks is not clearing the calls If you change to |
@rickhanlonii I've tried to use beforeEach(() => {
jest.clearAllMocks()
}) it does not help. I even tried to use both beforeEach(() => {
jest.resetAllMocks()
jest.clearAllMocks()
}) but this does not solve the issue as well. The only thing that does help is resetting a particular mock, e.g. a single mock function on a mocked class like: beforeEach(() => {
mockObject.method.resetMock()
}) This clears calls from this mock |
I would like to take a stab at this as my " 👋 good first issue", any pointers or suggestions on fix/implementation? |
After playing with this topic for a bit, it seems like calling It seems like the file is required multiple times (within I'm not sure how to continue, possibly by attaching the mock state to Apologies to @maumercado, I didn't mean to steal this from you, hope this info could help you solve it. |
@johannes-scharlach I'm not sure I follow - can you post a sample of what you tested? @maumercado feel free to take a look as well! |
So what worked was the following const jestMock = require('jest-mock');
describe('test mock', () => {
function mock(classType) {
const mockType = jestMock.generateFromMetadata(
jestMock.getMetadata(classType),
);
return new mockType();
}
class Test {
testMethod(param) {
return false;
}
}
const testMock = mock(Test);
const otherMock = jest.fn();
it('should reset mocks', () => {
testMock.testMethod(123);
testMock.testMethod.mockClear(); // <--- works as expected
expect(testMock.testMethod).not.toHaveBeenCalled(); // <---- passes
testMock.testMethod(123);
jestMock.clearAllMocks(); // <--- does reset the mock
expect(testMock.testMethod).not.toHaveBeenCalled(); // <--- passes
});
}); So the One possible solution here would be to use The other thing I found out was that the constructor of the |
Quick update: By @johannes-scharlach suggestion I have currently done the following change in the ModuleMockerClass:
with this change the use case specified here works, however when running Suggestions welcome! |
Hey! I'm following this issue for a college work and I'd like to help with anyway I can. I was able to reproduce the last solution from @maumercado , but I coudn't reach the "27 failed tests", I'm getting 74. How exactly are you testing? I'd need some help since it's my first time working with Jest. |
Hi @DaviWT, for testing I just do yarn build then yarn test, I am running node 10.13 maybe that's different for you. Try running |
@maumercado I guess I don't have a script definition for I'm not used to testing scripts, so any beginner advice is welcome, and I would appreciate it very much. For now I'm just trying to reproduce the bug and the possible solutions above in a proper way. I have no initial intention to submit a solution officially, my goal is to learn as much as possible about Jest and open source development. |
@DaviWT no worries, any question is a good question. So just to make this clear, you have forked the jest project locally and inside the jest project you are trying to run yarn build, but it is not inside your package.json? |
@maumercado Yes, exactly. I'm able to execute
I presume that there should be some specification for Another question, is the test only for the jest-mock package or for the whole Jest framework? |
@DaviWT The test is for the whole jest framework.. your Jest project package.json should look like this: https://github.com/facebook/jest/blob/master/package.json and you should be able to run the commands previously mentioned from the root of the jest project you just forked. |
@maumercado I see now, somehow my local directory was outdated from my own repository. I'll do further testings as soon as possible. Thank you so much for the help! |
no problem! if you find anything worth discussing |
This is so far the tests failing for the module mocker only with the changes I did specified below:
I am still not certain how to properly reconcile the global._mockstate when using jest-mock directly with the global._mockstate that is generated by the jest object, without breaking more tests. At this point any pointers or help is greatly appreciated! |
Any news on this? In my case
PS: I have also tried |
Is there an ETA on a fix for this or ideas for a workaround? I ran into this and it seems that a lot of others are as well based on the amount of +1s here: #7136 |
@caitecoll this workaround, mentioned on #7136, worked for me: #7136 (comment) I think this ^ should be the default jest behavior. Leaking state between tests is an anti-pattern because it means test start to rely on running in a certain order (they rely on the side effects of previous tests). This is a problem because:
IMO, clearing state between tests should be the default for these reasons and because the vast majority of projects do not require the performance benefits of not having to rebuild state before each test (and those projects that do can opt-into preserving state with config). If it's very hard to change these defaults due to back-compat, then at least this deserves thorough documentation and a section on how to set up this config (rather than having to do an extensive grep through issues and stack overflow to find it). |
This is still a problem |
I'm having the same issue, at least very similar. I posted on StackOverflow. I'll be tracking this there and post here in case I find some solution. https://stackoverflow.com/questions/61906896/spyon-working-with-jasmine-but-not-with-jest |
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 14 days. |
This issue was closed because it has been stalled for 7 days with no activity. Please open a new issue if the issue is still relevant, linking to this one. |
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. |
🐛 Bug Report
resetAllMocks
does not reset mocks created withgenerateFromMetadata
methodTo Reproduce
Here is a utility method that I've created to create class mocks:
However when I use
jest.resetAllMocks()
mocks created by this function are not reset.Here is a code example:
Expected behavior
The above test should pass.
Link to repl or repo (highly encouraged)
N/A
Run
npx envinfo --preset jest
Paste the results here:
The text was updated successfully, but these errors were encountered: