Difference between enzyme, ReactTestUtils and react-testing-library

Black picture Black · Jan 11, 2019 · Viewed 15.3k times · Source

What is the difference between enzyme, ReactTestUtils and react-testing-library for react testing?

The ReactTestUtils documentation says:

ReactTestUtils makes it easy to test React components in the testing framework of your choice.

The enzyme documentation just says:

Enzyme is a JavaScript Testing utility for React that makes it easier to assert, manipulate, and traverse your React Components' output.

React-testing-library documentation:

The react-testing-library is a very light-weight solution for testing React components. It provides light utility functions on top of react-dom.

Why is actually every solution easier and what can't be achieved with the other one?

Answer

Giorgio Polvara - Gpx picture Giorgio Polvara - Gpx · Jan 11, 2019

ReactTestUtils gives you the bare minimum to test a React component. I haven't seen it being used for big applications.

Enzyme and react-testing-library are both good libraries that give you all the tools you need to test your application. They have two different philosophies though.

Enzyme allows you to access the internal workings of your components. You can read and set the state, and you can mock children to make tests run faster.

On the other hand, react-testing-library doesn't give you any access to the implementation details. It renders the components and provides utility methods to interact with them. The idea is that you should communicate with your application in the same way a user would. So rather than set the state of a component you reproduce the actions a user would do to reach that state.

In my experience Enzyme is easier to grasp but in the long run, it's harder to maintain. react-testing-library forces you to write tests that are a bit more complex on average but it rewards you with higher confidence in your code.