51

7 React Testing Libraries You Should Know

 4 years ago
source link: https://www.tuicool.com/articles/eAJVf2R
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
3QfqEfB.png!webEnIVVby.png!web
Testing React applications in the wild

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.

Tip: Using Bit ( GitHub ) you can virtually isolate, encapsulate and independently test your components with any framework and library. The isolated and tested components will become reusable across your apps :+1:

1. 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.

2. Jest

M3MRRz6.png!webi2aEbiM.png!web

Jest is the testing framework used at Facebook to test Rect 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-suit-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 .

3. Enzyme

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.

4. 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.

5. Bit

Bit: easily isolate, test and reuse components anywhere

Bit isolates and encapsulates React components, making them reusable across your different projects and applications. Bit’s unique ability to virtualluy isolate components from their projects makes it a powerful tool for TDD.

Using a single CLI command, Bit will create a virtual representation for each and every component in your project, encapsulated and bundled with all its files, dependencies, and configurations. Then, the components can be independently tested with a framework of your choice- in complete isolation.

You can use Bit in development to virtually test each component in your project separately in your IDE. Then, you can share the components to Bit’s component-cloud to reuse the components in your different applications.

In the hub, each component will go through independent CI to make sure it’s reusable. The isolated test results will be presented for each component. Since Bit defines and controls the components’ dependencies, you can learn which changes to a component might even affect the tests of its dependents.

Making it easy to isolate and reuse modular components goes hand-in-hand with TDD, so that unit-test becomes a driving force in your workflow.

6. Pupeeter

iy2eueB.png!webMJZfmmI.png!web

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.

7. 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 look 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.

8. 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.

9. Mocha

MVfummM.png!web26VVFzQ.png!web

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.

10. Chai

MriQnuY.png!webjMj6Rr2.png!web

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.

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

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.

Bonus :sparkles:

  • Protractor

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK