Skip to content
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

fix: only suppress console.error for non-pure imports #549

Merged
merged 12 commits into from Jan 22, 2021

Conversation

mpeyper
Copy link
Member

@mpeyper mpeyper commented Jan 18, 2021

What:

Yet another alternative to #541 and #542

Why:

#546

How:

The complete opposite approach to #542 but the documentation and tests are still valid. By extending the suppression time from the initial import to after all tests have run, the patched console.error can be used by other utilities when setting up local mocks, while the code remains relatively straight forward and we don't need to wrap act.

Care here is taken to ensure that patching only occurs if the defaul imports are used and not the /pure imports.

Checklist:

  • Documentation updated
  • Tests
  • Ready to be merged

Fixes #546

@codecov
Copy link

codecov bot commented Jan 18, 2021

Codecov Report

Merging #549 (934fa6a) into master (9af1343) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #549   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           14        15    +1     
  Lines          210       218    +8     
  Branches        23        30    +7     
=========================================
+ Hits           210       218    +8     
Impacted Files Coverage Δ
src/helpers/createTestHarness.tsx 100.00% <ø> (ø)
src/core/console.ts 100.00% <100.00%> (ø)
src/dom/index.ts 100.00% <100.00%> (ø)
src/index.ts 100.00% <100.00%> (ø)
src/native/index.ts 100.00% <100.00%> (ø)
src/server/index.ts 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9af1343...934fa6a. Read the comment docs.

@mpeyper
Copy link
Member Author

mpeyper commented Jan 18, 2021

I wasn't quite right about the docs and tests not needing updating. The docs were very close, but I have added more detail about the side effects, and the tests were still accurate, but there were some new cases that needed a test so I have refactored them a bit too.

@joshuaellis
Copy link
Member

So would the way to disable the console patching to be to use the /pure export of each module, else we continue to do what we're currently doing?

@mpeyper
Copy link
Member Author

mpeyper commented Jan 19, 2021

Using the pure import will disable it for the test file or setting the env variable (e.g. with the setup file utility) can disable it more globally. The docs cover these options.

@@ -12,6 +12,7 @@ route: '/reference/api'
- [`cleanup`](/reference/api#cleanup)
- [`addCleanup`](/reference/api#addcleanup)
- [`removeCleanup`](/reference/api#removecleanup)
- [`console.error`](/reference/api#consoleerror)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just reading what this list of links is for, I don't this this one should be added here after all (it will appear in the side menu still though).

@mpeyper
Copy link
Member Author

mpeyper commented Jan 19, 2021

Honestly, I'm feeling best about this option at the moment, especially now the suppression is being handled in before/after test hooks.

@mpeyper
Copy link
Member Author

mpeyper commented Jan 19, 2021

especially now the suppression is being handled in before/after test hooks.

Now that I've just said that I wonder if the "on import" of the previous change makes it slightly better for people wanting to mock console.error so that their mock is mock likely to occur after our patching.

Or moving to to beforeAll and afterAll instead of ...Each could also make sense as they are more likely to be using ...Each (not may people is ...All in my experience, but that might just be me).

... Back to confusion.

@joshuaellis
Copy link
Member

I've had a quick look at this, it does look promising. Resolving the pure import was a good step to take & i think taking advantage of *Each functions in Jest was a good idea too. I'm especially happy we don't seem to be wrapping act anymore?

@mpeyper
Copy link
Member Author

mpeyper commented Jan 22, 2021

I'm especially happy we don't seem to be wrapping act anymore

Correct. No act wrapping.

I think we should move forward with this one. It's not worse than what we have now, but also has the escape hatches if people need them.

@mpeyper mpeyper marked this pull request as ready for review January 22, 2021 01:18
Copy link
Member

@joshuaellis joshuaellis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, well done for exploring so many options, I don't think I would have been able to tackle this issue so thoroughly 😅

@mpeyper
Copy link
Member Author

mpeyper commented Jan 22, 2021

Ok... deep breath... here we go...

@mpeyper mpeyper merged commit 804d9ac into master Jan 22, 2021
@mpeyper mpeyper deleted the fix/pure-console-error-side-effect branch January 22, 2021 10:08
@github-actions
Copy link

🎉 This PR is included in version 5.0.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

renderHook from /dom/pure has global side-effects
2 participants