-
Notifications
You must be signed in to change notification settings - Fork 726
Add Migrate from Enzyme page #568
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
Conversation
Thank you for this! I haven't the time to read it yet, but I want you to know that we've taken notice of the PR and we'll look at it as soon as we're able. |
Thank you, Kent. If there is anything that needs to be changed, please let me know |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this page can trim down on the installation and usage sections (we have other doc pages for that), and focus on migrating specific testing patterns.
- Events - what you have here about
user-event
is good; may want to coverfireEvent
and discuss differences. - Finding elements
- Here's a migration strategy for
wrapper.find(ComponentName)
:- Add a testid to the component => Change .find() to use the testid attribute (might now have issues with hostNodes/ReactElementWrappers both showing up) => migrate to RTL.
- Discuss how tests for component structure can be overconstraining.
- Show how you can use getByRole, label, etc. instead of testids if your components are ARIA-compliant.
- Here's a migration strategy for
- Checking prop values in components
- Address why this is not a pattern commonly used in RTL (validate behavior instead). Show how you can use mocks or spies to do it anyway.
- Checking component state
- Discuss how you can validate the render result instead
- Shallow render
- Show how you can mock a troublesome component (like a 3rd party lib) that you want to treat as a passthrough/noop componentt in the test env
- Triggering class methods in tests
- Discuss how this is not recommended because it overspecifies the component (can you rewrite it with Hooks and still have the tests pass)?
- Use of act()
- Not usually necessary in RTL due to wrapping behind the scenes
- Use of wrapper.update()
- Not necessary since there is no wrapper
- Show
await findBy*
andawait waitFor(callback)
for async interactions
- Naming conventions
- Don't call the value returned by
render
wrapper; mention use ofscreen
to avoid using that value at all, except for{rerender}
- Updating props
- Show use of
rerender
method
- Show use of
- Updating state
- Don't do it
- Working with Redux/Context
- Migrating wrapped/container components
- Testing "connected" and "pure" components in an integrated and non-integrated fashion
- Differences in queries
- Enzyme .find() is like jQuery - it can return an array/NodeList. Show how this creates extra handling in tests (like nodes[0]) and allows errors to get farther in the test while throwing non-descriptive error messages. Show
getBy
andfindBy
+*AllBy
as an alternative - they throw immediately with a detailed DOM printout and you can specify if you want 1 or multiple values.
- Enzyme .find() is like jQuery - it can return an array/NodeList. Show how this creates extra handling in tests (like nodes[0]) and allows errors to get farther in the test while throwing non-descriptive error messages. Show
Thanks for your comments I was waiting for others to write about their ideas, so I can start working on this page again. @kentcdodds do you agree with all of what @alexkrolick said? Thoughts? |
Yes, I agree with Alex |
Co-authored-by: Nick McCurdy <[email protected]>
@alexkrolick Thanks for all your good suggestions. Question 1
I don't understand about the example you want me to provide, can you please clarify? This is what I understand:
Question 2
As far as I know, React Testing Library does not support shallow rendering and even it's not something that we should recommend people to use. But I think I'm misunderstood. Can you please clarify how would you do that? Question 3
Does this need any discussion? Like, do people use to write this kind of test in Enzyme? (I guess you mean how they use Question 4
Should we care about Redux tests? I mean Redux is an implementation detail, and what we care about is that we do an action, and based on run the test against the component output. Should we care about the Redux store? In the following day, I might ask some more questions to hopefully complete this page ASAP |
Question 1
If you have a pretty large test, you may want to rewrite part of the Enzyme test first. Otherwise you are changing both tests and implementation at the same time and that is risky. The challenge with using test-ids in Enzyme is this: let wrapper = mount(<Foo><Bar data-testid="abc" /></Foo>)
wrapper.find('[data-testid="abc"]').debug()
// ->
// Bar data-testid="abc"
// div data-testid="abc" Both the div (created by Bar) and the React component (Bar) have the testid attribute. It's not clear which one to use in the test and you might get more than one element when you only want one. You can skip the Enzyme refactor if you trust that you can manage rewriting the test in RTL from scratch without missing anything. We could start documenting from this assumption I suppose. Question 2
One reason to shallow render is it doesn't execute the render logic, which is good if a subcomponent has weird dependencies, side-effects, etc. You can do something like this to create a fake component for a library: jest.mock('google-maps-react', () => (props) => <google-map data-testid="google-map" {...props} />) Question 3
People access instance methods on class components and call them all the time. I don't agree that most people are using hooks in legacy codebases, which is where the most Enzyme migration is coming from. Question 4
I'd cover the |
Dear @kentcdodds and @alexkrolick, I tried my best to add the requested changes. In the current context, if there is anything that you think can be changed or improved, please let me know. This PR is getting a little larger than expected! Changes are welcome if you feel something is wrong both grammatically or technically. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made a few modifications (and formatted with prettier). I'd love for someone to review/merge. Thanks so much @m98!
Thanks for your help @m98 |
@all-contributors please add @m98 for docs |
I've put up a pull request to add @m98! 🎉 |
Thanks for accepting the PR @kentcdodds I just browsed the Testing library website, but I could not see the "Migrate from Enzyme" page. It might be something that I've done wrongly! Can you please check it out? |
The page is on the site: https://testing-library.com/docs/react-testing-library/migrate I'm not sure why it's not showing up in the sidebar. I don't know how that works actually 😅 Anyone wanna take a look at this? |
I think that the name in the sidebar is wrong, it should be as the id of the page which is |
Hello everyone!
As discussed earlier on #563, we realized a new page for those developers who have experience with Enzyme is needed, for at least the following purposes:
I did not go into detail on the queries, since it has a complete documentation page itself, and it would be hard to keep two pages updated (I mean if going into detail on this page) instead I just explained the difference and some helpful link to help them get started with the RTL.
Please note that I'm not a native English speaker, so, grammar problem is expected even though I tried to check it with some grammar checker software.
Thanks a lot
Mention: @kentcdodds
closes #563