Deep dive into Testing React
Deep dive into React Testing
There are many libraries and combinations of these libraries that you can use for bare bones React Testing:
Only Jest (Tutorial) (1)
Jest + Enzyme (Tutorial) (2)
Jest + RTL (3)
Mocha + Chai + Sinon (Tutorial) (4)
The last three months, I worked with one larger client of mine and we settled on only using Jest for testing React components (1). It went surprisingly well and I would do it again for my next projects. Sometimes there is no need to throw yet another library on top of it.
However, if you are looking for more conveniences, testing React with Enzyme (2) or RTL (3) gives you more abstractions on top. But again, your whole development team needs to adjust to yet another testing library.
If you are coming from Node.js, testing your React components with Mocha/Chai/Sinon (4) may be more familiar to you. That's how I learned testing frontend applications at least; but times have changed and thus I think the other solutions are more popular now.
Regardless of which solution you pick, throwing Cypress on top for E2E Testing isn't a bad idea. Especially if you are working on more complex user interactions and/or larger web applications, it saves you lots of time in the aftermath, because you don't have to do much manual testing for every deployment anymore. Read more about testing React with Cypress.
In one of my last projects, the PM enforced a testing coverage of 95%. Generally, I don't agree with a threshold as high as 95%, but then again I just shifted my perspective on things and took it as opportunity to learn more about testing. Read more about setting up a CI + Test Coverage.
If you are like me and you want to double down on a simple renderer + Jest for testing React applications, you may enjoy these tutorials to boost your Jest knowledge:
That’s it from my side for this month with the latest updates for React and the JS ecosystem. Let me know what you want to learn about next and I try to cover it in one of my tutorials. Wishing you a good time.
PS.: If you read one of my books, it would mean the world to me if you would leave a nice review for one of them.
Just recently I watched Mortal Engines and Ready Player One. I am not sure about the performance of the actors, because I am not that much into movies, but it's just amazing how these kind of movies let you escape into another world. So if you feel like shutting your systems down, give them a try if you haven't already.