React Testing Library VS Enzyme

Hi, in this post I’m going to write some of the differences between React Testing Library (RTL) and Enzyme, so in that way you can take a better decisions of which one in implement in your project.

Look this next image; in the right side we can see the Enzyme library that’s testing the same react component as the RTL in the left side:

  • When we use Enzyme library to test our react components this library require another library called “enzyme-adapter-react” while RTL doesn’t require another libraries in this case we just need install “@testing-library/react“.
  • Both libraries have a good online documentation.
  • RTL require less started configuration than Enzyme.
  • Both libraries use Jest, so if we are familiar with Jest this won’t be a problem (remember that RTL and Enzyme each other need Jest)
  • If we are using RTL in our project and a junior developer join the project this person will be able to understand the testing code very quickly because RTL is more clean.
  • RTL code is short, readable, clean and nice.
  • Enzyme takes more lines of code.
  • Enzyme clean the renders automatically while RTL doesn’t, so with RTL we need to specify the cleanup in the testing file.
  • RTL is made from the user perspective.
  • RTL every time has more improvements is recommended by Facebook.
  • With Enzyme we insert a lot of data-test-id and RTL recommend less use the data-test-id in our components.

As conclusion we should ask ourselves: Which one of the 2 libraries make more sense reading & understanding the code?

Also I think that we should be interested to learn how to implement testing code using RTL to update our skills because a lot of companies are moving to use this library.

By Cristina Rojas.