10 Tools and Libraries for Testing in React

10 Tools and Libraries for Testing in React

  • 2245

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.

From various JavaScript testing frameworks to useful assertion libraries, choosing the right toolset is key in leveraging TDD in React, from component unit-testing to integration testing and even end-to-end testing React apps.

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.

1. Jest

"Jest"

Jest is the testing framework used at Facebook to test React components and is adopted by Uber, Airbnb and other teams. The React community, therefore, recommends Jest as the React testing framework of choice. Jest itself works with many JavaScript projects our of the box, from create-react-app to NG, Vue and even TS or Babel.

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.

2. Mocha

"Mocha"

Mocha is a JavaScript test framework for Node.js programs, featuring browser support, asynchronous testing, test coverage reports, and use of any assertion library. It is very configurable and gives the developer full control over how they wish to test their code, with which tools, while you can plug in and out most supporting libraries and tools in your chain.

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.

3. Chai

"Chai"

Chai is a BDD / TDD assertion and expectation library for node and the browser that can be delightfully paired with any javascript testing framework. It’s often associated with testing in Mocha with Enzyme, and can also be used with Jest and Enzyme. Chai’s basic interfaces include functionalities such as expect, should and assert, helping you to declare what to expect in a test. If choosing Mocha as your testing framework, it can be useful to give it a try.

4. Karma

Karma is not a testing framework or an assertion library (works with Jasmine, Mocha etc). It launches an HTTP server, and generates the test runner HTML file. With Karma, you can execute JavaScript code in multiple real browsers.

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.

5. Jasmine

Jasmine defines itself as a “JavaScript testing framework for browsers and node.js”.It’s a Behavior Driven Development testing framework for JavaScript. It does not rely on browsers, DOM, or any JavaScript framework. However, it’s been traditionally used in Frameworks like Angular with their CLI tool.

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.

6. Enzyme

It’s hard to dive into React testing, and particularly with testing frameworks like Jest, without crossing paths with Enzyme by AirbnbEngEnzyme 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.

7. React-testing-library

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 act()mockComponent()isElement and more) in React that help to test components using a testing framework of your choice. Test renderer lets you render React components into pure JavaScript objects without depending on the DOM.

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

Cypress is a JavaScript end-to-end testing framework that makes it easy to setup, write, run and debug tests in the browser. It comes with its own dashboard that gives control over the status of our tests, and, because Cypress works in the actual browser, you can use the browser’s dev tools side-by-side.

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 applicationsThis library was even built to make it easier.

10. Pupeeter

"Pupeeter"

Puppeteer is a Node library which provides an API to control Chrome over the DevTools Protocol. Puppeteer runs headless by default but can be configured to run full (non-headless) Chrome or Chromium.

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.

Conclusion

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.

Recommended Reading

Build a Mobile Phone Authentication Component with React and Firebase

How to create a signature pad in React

Full-Stack JavaScript Tutorial for Beginners

How to Add Spinners and Notifications to React app