Closed codyzu closed 2 years ago
Thanks for reporting this issue.
This might have been added by #5 , could you please try with v2.0.4
to confirm?
Regarding using jest.resetAllMocks()
, I usually use the restoreMocks option:: . Did you try it?
@ValentinH Sorry for the slow turnaround.
I have some updates...
jest.resetAllMocks()
in a beforeEach()
block in version v2.0.4
without disabling jest-fail-on-console. So your theory about it being introduced by #5 appears correct.restoreMocks
or resetMocks
config options works for both versions (v.2.0.4
and v2.1.1
). Unfortunately, I prefer to be able to call resetAllMocks
explicitly in my test so it's obvious. In the repo where this problem occurred, I think there are some tests that depend on some common mock implementations so it would require a bit of refactoring to use the config options.I created a repo that tests both versions v.2.0.4
and v2.1.1
. You can see it, clone, and test from here: https://github.com/codyzu/test-jest-fail-on-console
Thanks for the details answer.
I think we could partially revert what has been changed in #5. If I understand correctly this PR did 2 things:
beforeEach
and removing it in an afterEach
jest.spyOn()
to set the custom logicif we only revert the second one, meaning setting the custom logic "manually", it should fix this issue while still fixing the original issue of #5.
What do you think ?
My jest setup looks llike this: `import failOnConsole from 'jest-fail-on-console'
beforeEach(() => { jest.resetAllMocks(); });
failOnConsole();` And it works fine! When the failOnConsole() was before the beforeEach it didn't though.
Thanks for the details answer.
I think we could partially revert what has been changed in #5. If I understand correctly this PR did 2 things:
- enabling the custom console logic in a
beforeEach
and removing it in anafterEach
- using
jest.spyOn()
to set the custom logicif we only revert the second one, meaning setting the custom logic "manually", it should fix this issue while still fixing the original issue of #5.
What do you think ?
@ValentinH Yes, I agree... if we just get rid of the spy usage it should be ok.
While appreciating the author's intentions to use the native spy functionality, it could lead to unexpected conflicts (as it did for me).
Do you need help with this?
No it's ok, I'll do it. I just need to get back my admin access. I'll probably have it this week.
The fix was published in v2.2.1
Awesome sauce! I’ll integrate and give it a try tomorrow!
Worked like a charm. Thanks!
@codyzu thanks for checking 👍 Turns out this version caused issue with older versions of Jest 🙈 Could you please try the version mentioned in this PR to check if it also works for your setup: https://github.com/ricardo-ch/jest-fail-on-console/pull/14 ? 🙏
A pattern common in my tests is to call
jest.resetAllMocks()
in abeforeEach
block to help ensure that tests are truly independent and don't depend on any state from other tests.I found that this disables the magic from this package. This seems obvious looking at the source since it depends on jest spyOn mocks.
To re-enable the magic, I found that I can call
failOnConsole()
after anybeforeEach
block that calledjest.resetAllMocks()
.