React Application Testing Strategy
Tags: javascript, testing, react, tutorial
Published: 2021-05-30 00:40:56

## Introduction 

_Any post on testing strategy will be incomplete without mentioning Mike Cohn’s Test Pyramid diagram, so here we go:_

![Test pyramid](https://dev-to-uploads.s3.amazonaws.com/i/g0d447qk2tsl15lkaspg.png)
Image Credit: https://martinfowler.com/bliki/TestPyramid.html

We need more unit tests, lesser integration tests, and even lesser end-to-end tests, **why?** as it costs time and money to write more integration and end-to-end tests. Also, unit tests are faster compared to other test siblings. And not to mention, no one wants to wait for hours for the CI-CD pipeline execution.

Now getting back to our main topic, the bigger question is **which library to use for unit, integration, and end-to-end testing?** Well! there are various options available and everyone has their own view (which is great :D). Based upon my experience in creating enterprise-level modern JavaScript applications, the Single Page App (SPA), here is my opinionated view.

## Dive Deep

Before, we dive deep in, here is the image that nicely illustrates the testing libraries per test tool abilities like test framework, assertion, test runner, mocking, code coverage (from left to right).

![Testing tools](https://dev-to-uploads.s3.amazonaws.com/i/hbfhll4cr4c203idmx4a.jpeg)
Image Credit: https://www.udemy.com/course/complete-react-developer-zero-to-mastery/

[Jest](https://jestjs.io/) is one of the libraries that provide various capabilities, which is shown in the above comparison. Jest ([4 million weekly downloads](https://www.npmjs.com/package/jest)) is a powerful tool for writing unit tests for pure functions. However, for more complex scenarios, especially when working with React apps, [React Testing Library](https://testing-library.com/docs/react-testing-library/intro/) (1 million weekly downloads) is an excellent choice.

React Testing Library is built on top of DOM Testing Library, which helps writing tests using selectors. In places where you can’t use the React Testing Library, [Enzyme](https://enzymejs.github.io/enzyme/) library can rescue you (though Enzyme is the competitor).

So, with the help of Jest, React Testing Library, and Enzyme, one can pretty much cover any unit and integration test case scenarios. So, **what about end-to-end testing?** Well! [Cypress](https://www.cypress.io/) and [Playwright](https://playwright.dev/) libraries are great tools for end-to-end testing.

Awesome documentation, ease of implementation, less learning curve, I can’t even think of Selenium or any other libraries. Cypress has a nice interface where one can see the test execution with minimal test writing effort.

**So, with this are we done?** of course not, with the growing popularity of micro-service architecture, contract testing is an essential part of it. And [Pact](https://docs.pact.io/) is the consumer-driven contract testing library that provides assurance that consumer application or provider service can be deployed to production without breaking each other’s trust or in other terms the contract.

_If you have reached here, then I did a satisfactory effort to keep you reading. Please be kind to leave any comment or ask for any correction. **Happy Coding!**_

## Reference:
- https://martinfowler.com/articles/practical-test-pyramid.html