DEV Community

Cover image for React Application Testing Strategy
AK DevCraft
AK DevCraft Subscriber

Posted on • Edited on

React Application Testing Strategy

Introduction

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

Test pyramid
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
Image Credit: https://www.udemy.com/course/complete-react-developer-zero-to-mastery/

Jest is one of the libraries that provide various capabilities, which is shown in the above comparison. Jest (4 million weekly downloads) 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 (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 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 and Playwright 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 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:

Top comments (0)