question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Convert all tests to a library that we agreed on and abandon others

See original GitHub issue

Currently we are using Enzyme, react-testing-library and react-test-renderer to test behaviors of react components. Enzyme proved more useful for triggering events and manipulating the component. Also @julienw suggests that react-testing-library is built around the idea that we should test using the same things a user would use. Having 3 libraries is not a manageable situation and we should reduce it to one.

It’s also a pain when we try to upgrade the tests one by one. It would be great if we can do all at once. After the migration, we can abandon others.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
julienwcommented, Jan 9, 2019

Thanks @canaltinova. I spent some weeks writing tests using react-testing-library and I should try to explain why I think it would be valuable to use this library instead of enzyme. Teaser: it’s not just because dan abramov said so 😃

First, a bit of history

At the beginning we used react-test-renderer which is the test library that React publishes. It’s very simple, and allowed us to test how a component renders from a Redux state. But we can’t test user interactions because it doesn’t support events.

Then we used Enzyme especially for this. Enzyme is a library that makes it easy to change the props and/or the state of a component from the “outside” of the component, and then provides an API to easily and deeply find elements in the virtual DOM tree.

And then lately kentcdodds introduced react-testing-library, that aims to be very simple, especially compared to Enzyme, but still providing a useful API to fully test components on the contrary to react-test-renderer.

Enzyme’s caveats

Connected components

I think Enzyme doesn’t work well for connected components. Because of how enzyme v3 works (essentially it uses a layer) we need to call update whenever the redux store changes to actually “see” the changes in the enzyme-based test. This can be very cumbersome, eg: https://github.com/devtools-html/perf.html/blob/4c11b8ffb553950ddb8c859365b537ee55d5f864/src/test/components/CallTreeSidebar.test.js#L89-L107

Shallow rendering

This is a false good idea. We used it in the past for these components that are towards the Root of the application: if we’d render everything it would be too slow, and also give too much detail in our snapshots. But I now believe this was a bad idea. kentcdodds writes about it here. I’d like to add that the shallow API in Enzyme essentially replaces React. As a result we don’t test our component with React but with another imperfect library that may (and does!) behave differently. For example it’s not possible to get componentDidUpdate called if the props changed after a Redux store change.

Enormous API and library

The API is very big and it’s very difficult from the documentation to understand what we should use and what we shouldn’t. This could be worked around by some best practices on our end, but this is isn’t very easy. The library itself is big, so it’s difficult to look at its source code if we encounter a problem.

react-testing-library’s strengths

For these reasons I believe this library is better suited to our project, that’s why I’m strongly in favor of switching to this library for all our tests. I’m willing to do this work after working on #1575 and I don’t think this is a lot of work as most of our tests are still simple for now.

0reactions
julienwcommented, Jan 14, 2019

I started here => https://github.com/devtools-html/perf.html/compare/master...julienw:migration-to-react-testing-library

As expected the migration from react-test-renderer is quite strightforward, but a bit more challenging from enzyme. No real roadblocks so far.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unit Tests, How to Write Testable Code, and Why It Matters
In this unit testing tutorial, I intend to demonstrate that unit tests are quite easy; the real problems that complicate unit testing, and...
Read more >
How to unit-test third-party library wrappers? - Stack Overflow
I mean, if I write unit tests for it and will mock those 3rd party interfaces to test my wrapper, nothing will change....
Read more >
Common methods for swapping out a library
1 Answer 1 · you can keep the old API and convert any arguments/return values within the methods · you can change the...
Read more >
All The Chapter Approved 2022 40k Rules, Missions & Points
Here are all the new Chapter approved Warhammer 40k rules updates, points changes, missions, and Secondary Objectives in the CA GT 2022 book ......
Read more >
Remarks and a Question-and-Answer Session With Broadcast ...
For the first time we got Soviet agreement to a worldwide figure of 100 ... but General Secretary Gorbachev said that his demand...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found