10 Tools and Libraries for Testing in React
Useful frameworks, libraries and tools for testing your React components and applications- not your patience.
Most people use Jest to test their components. Probably, with Enzyme and a couple of other utils. But, while FB recommends Jest as their React testing Framework of choice, the open source ecosystem for testing React applications is rich in frameworks and tools.
To help navigate the landscape, here are some popular Testing frameworks and libraries for React. Please feel free to comment and suggest your own favorite tools, or share your insights from working with any of them.
Jest’s UI snapshot testing and complete-API philosophy combines well with React. Performance is also a plus with process-based parallel testing and optional priority to failed tests. With over 16M downloads a week, Jest is probably the most popular testing framework for React. Here’s a useful guide into testing React applications with Jest and Enzyme.
Just like Jest and other frameworks, when used it React, Mocha can be combined with Enzyme, and also with Chai and other libraries for assertion, mocking etc. Some prefer it to test their React apps as well, often when in need of specific advanced configurations with a mature and rich toolset. Yet, this also can become a pain, as common tasks (like snapshots) will require adding more tools and further configurations in your workflow.
It was built to simplify the feedback loop between writing code and getting information from your tests, without having to dabble in configurations. You can run tests locally and check them in real browsers and devices, from your phone to tablets and desktops, while the entire feedback loop happens in your IDE. Karma also works with continuous integration to Jenkins, Travis etc.
Jasmine can be used to test React applications, for example with Babel and Enzyme. There’s even a designated helper util library built to make this workflow smoother. Asking yourself whether or not there’s a reason to choose Jasmine over Jest? Here’s a nice Reddit discussion about this very question.
It’s hard to dive into React testing, and particularly with testing frameworks like Jest, without crossing paths with Enzyme by AirbnbEng. Enzyme isn’t a testing framework, but rather a testing utility for React that makes it easier to test the outputs of components, abstracting the rendering of components.
Much like JQuery used to do on the DOM, Enzyme lets you manipulate, traverse, and in some ways simulate runtime given the output. In short, it can really help you render components, find elements and interact with them. Try using Jest and Enzyme together, or check out Enzyme with Karma, Mocha etc.
For those of you who don’t use Enzyme, like Facebook itself, the React team recommends using the react-testing-library to simulate user behavior in your tests. Much like Enzyme, this library is a simple and complete set of React DOM testing utilities aimed to imitate actual user actions and workflows. Choosing between react-testing-library an Enzyme? Perhaps this will help.
8. React test utils and test renderer
True, this isn’t a library but rather a collection of useful utilities (like
Knowledge of these useful tools is key in successfully testing your components and apps in React, and without having to force external tools into doing more than they should. Your decision-tree journey should probably start here.
9. Cypress IO
You can time-travel with snapshots, automatically reload test changes and await assertions, and easily debug your code. With built-in parallelization and load balancing, Debugging tests in CI becomes much that easier too. However, you can’t use Cypress to drive two browsers at the same time, which might hurt. While not as popular as Puppeteer, Cypress can be useful for end-to-end testing your React applications. This library was even built to make it easier.
This means, you can use Puppeteer to do pretty much anything you can manually do in the browser, such as generating screenshots, generating pre-rendered content from SPAs and even automating actions like form submissions, keyboard inputs etc.
Puppeteer is often used with Jest (and Faker) to test React applications end-to-end, while puppeteer provides screenshots and more for testing your app.
With the modularity of React comes better TDD. From components to integration and end-to-end testing, choosing the right tooling can help to put this theory into practice without pain, and enjoy its benefits.
Combining the right testing framework (e.g. Jest etc) with the right assertion/manipulation libraries (e.g. Enzyme etc) is the key to creating a smooth yet flexible workflow that can adapt while you upgrade, extend and modify your code. By isolating virtually isolating components from their projects (e.g. Bit etc) you take modularity and TDD to a whole new level.