-
-
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
Add test to repro problem with mocking out 'getDerivedStateFromProps' #6000
Add test to repro problem with mocking out 'getDerivedStateFromProps' #6000
Conversation
**what is the change?:** Soon folks will be adopting the new 'getDerivedStateFromProps' in their React components, and re-running existing tests. If those components are mocked in existing tests, then React will throw errors currently. Why? Because React validates that any component defining this method has state and returns the right thing from 'gDSFP', and when an auto-mock is created it doesn't fit these invariants. Talking through this with @mael and @mjesun we determined an easy solution is to add a small conditional to either blacklist mocking functions with this name, so that automocks just don't have this method and the invariant is never checked in React. An alternative solution would be to make the mocked 'gDSFP' method return 'null' and define 'state' on the mocked component. **why make this change?:** Let's work together to make Jest and React work together incredibly smoothly, like a sea of butter. ^_^ **test plan:** This test will throw errors right now - tagging @mjesun to tweak Jest so that the new test will pass correctly. (Flarnie will insert screenshot of the errors thrown)
Whoa, congrats on #6000 PR! We'll take it from here :) |
Hi! Tried pushing a commit to your branch but got permission denied 😔 Aside from the linting errors and a small typo |
Nevermind, i was able to reproduce it. Can you enable pushes to your branch, though? Thanks! :) |
Codecov Report
@@ Coverage Diff @@
## master #6000 +/- ##
=======================================
Coverage 64.32% 64.32%
=======================================
Files 217 217
Lines 8312 8312
Branches 3 4 +1
=======================================
Hits 5347 5347
Misses 2964 2964
Partials 1 1 Continue to review full report at Codecov.
|
Closing for #6011 |
@mjesun do we still need to do something here? |
Don't think so; we agreed on fixing this somewhere else. |
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
what is the change?:
Soon folks will be adopting the new 'getDerivedStateFromProps' in their
React components, and re-running existing tests.
If those components are mocked in existing tests, then React will throw
errors currently.
Why? Because React validates that any component defining this method has
state and returns the right thing from 'gDSFP', and when an auto-mock is
created it doesn't fit these invariants.
Talking through this with @mael and @mjesun we determined an easy
solution is to add a small conditional to either blacklist mocking
functions with this name, so that automocks just don't have this method
and the invariant is never checked in React.
An alternative solution would be to make the mocked 'gDSFP' method
return 'null' and define 'state' on the mocked component.
why make this change?:
Let's work together to make Jest and React work together incredibly
smoothly, like a sea of butter. ^_^
test plan:
This test will throw errors right now - tagging @mjesun to tweak Jest so
that the new test will pass correctly.