DEV Community

Himanshu Kanojiya
Himanshu Kanojiya

Posted on

What is the better testing library for React, Enzyme (from Airbnb) VS React testing library (from Kent C. Dodds)?

I wish, if I did not ignore this earlier, if I took it seriously, then I was not confused to suggest which library we should use in the React for the project.

A few weeks ago, I was surfing and posted some posts on Linkedin on which react testing library I should use "React testing library or Enzyme library by Airbnb.

Research on testing library

I spent a lot of time researching this. I visited so many blogs, did a post on community, and all.

I have received some replies, but still not convinced at all. Then, I did this: I tried both testing libraries.

Here are some things I have found during research and using both of them that can help you too:

Enzyme:

  1. Focuses on unit testing means it tests components prop & states value, not actual user flow.

  2. As it depends on components prop and state for testing, if you perform any changes in the code, then all test cases for that component will break. Still, If user behavior (user flow) is the same, your test cases will not work.

  3. Can perform isolated testing, depends which rendering method you are using. If using shallow rendering, then will render parent component. If using the mount rendering, then it will load parent to child components to test.

  4. As it tests components props & states, finding the bug in code is easy.

  5. You can access components by using CSS selectors for testing.

  6. Enzyme popularity is decreasing day by day (Not even in top 10). To support this statement here is the survey link: Enzyme Trend.

  7. There is no official adapter (it helps the Enzyme distinguish which React version you are using and which necessary files are required to test files for that react version) for React version 17.

  8. As react version 18 beta announced and there are a lot of changes in React API, I don't think version 18 adapter will be possible as changes in API will require a lot of rework for these: Enzyme Adapters, Enzyme itself, enzyme-adapter-utils, and enzyme-adapter-react-helper.

  9. Enzyme depends a lot on React internals, one change in React internal can break the whole Enzyme library.

  10. If you are not planning to migrate to React version 18, using Enzyme with an unofficial adapter will be enough. In case you are planning to upgrade, then consider React testing library.

React testing library:

  1. Popular and comes with React if you install react with npx create-react-app.

  2. Popularity is increasing day by day. Source: Survey by state of js
    Survey by state of js

  3. Recommend by React team for testing. Source: https://reactjs.org/docs/testing.html

  4. It focuses on user flow-based testing means it performs testing as a real user interacting with your app.

  5. It gives you more confidence in your code as it tests the same as real users interacting with your app.

  6. It depends on the user flow, not on the code, which means if you make any changes in the code, not in user behavior (user flow), then your test cases will not break.

  7. As it tests the same as a user interacting with your app, finding the bug in code can become difficult to find.

  8. By default, it has only one type of rendering for testing, "render" same as mount in Enzyme.

  9. Accessing components is not easy like Enzyme, as it is not using CSS selectors instead, it uses other methods such as findBy, and getBy.

  10. If you need to wait for an element to appear/disappear or a promise has to resolve first, then using findBy methods along with await will help you a lot. Super Easy, right? Yes, it is

  11. Performing user events are almost the same as Enzyme. The only difference is that, in Enzyme, the user needs to pass the event name as a string, and for that, you need to visit the documentation to find an event, but in React testing library, you don't need to. Just access the internal method like this "userEvent.click". It is a Timesaver for me.

  12. If you look in Stack Overflow trends, you will see a lot of progress for the react-testing-library, which means if you are stuck somewhere in testing, then there is a huge community to help you. Source: React testing library & Enzyme Trends
    React testing library Vs Enzyme Trend on StackOverflow

My views:

I am using both libraries, personally like both libraries. There are still many companies out there using Enzyme as they are more interested in traditional testing. So might be there are some chances that you need to use Enzyme over react-testing-libraries, but it is upto us to share pros & cons for it.

In the long run, I recommend you to use react-testing-library as it is growing rapidly, can perform testing the same as a user interacting with your app, not going to be deprecated or outdated, and has community support.

Oldest comments (1)

Collapse
 
srshifu profile image
Ildar Sharafeev

If you are looking for a tool to automate monitoring of Enzyme deprecation, you can find link here: thesametech.com/migrate-away-from-...