ValentinH / jest-fail-on-console

Utility to make jest tests fail when console.error() or any other methods are used
MIT License
141 stars 20 forks source link

Calling jest.resetAllMocks() disables the check #10

Closed codyzu closed 2 years ago

codyzu commented 2 years ago

A pattern common in my tests is to call jest.resetAllMocks() in a beforeEach 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 any beforeEach block that called jest.resetAllMocks().

  1. Does this seem like the sane thing to do? I can't think of any better fix since this package just uses jest to spy on the console, but I'm interested if anyone else has any better ideas.
  2. Should a note be added to the readme to save anyone else some time debugging?
  3. :point_up_2: If yes, I can create PR... let me know
ValentinH commented 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?

codyzu commented 2 years ago

@ValentinH Sorry for the slow turnaround.

I have some updates...

  1. I can call 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.
  2. using the 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

ValentinH commented 2 years ago

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:

if 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 ?

swavans commented 2 years ago

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.

codyzu commented 2 years ago

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 an afterEach
  • using jest.spyOn() to set the custom logic

if 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?

ValentinH commented 2 years ago

No it's ok, I'll do it. I just need to get back my admin access. I'll probably have it this week.

ValentinH commented 2 years ago

The fix was published in v2.2.1

codyzu commented 2 years ago

Awesome sauce! I’ll integrate and give it a try tomorrow!

codyzu commented 2 years ago

Worked like a charm. Thanks!

ValentinH commented 2 years ago

@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 ? 🙏