React has made web development very easy, accessible and fast these days having a large ecosystem that comes together. However, writing tests for react is not so pretty as developing the frontend of a web app with it.

This article tries to give some hints from a bunch of articles that I have read, trying to find a fast and scalable way to write react tests, with a bit of personal experience and thoughts.

Note: these are my personal ideas, I don't think they're the "ground truth" to this topic. Feel free to give some thoughts and ideas on the comments :)

Testing & Existing Libraries

In Javascript, there are many libraries and frameworks that help us to write clean and fast tests, but, not all of them are designed for the same purpose. Some are just assertion engines, others do mocks, stubs and mocks, and so one.

Some of the common ones that I have seen are the following:

Comes with an assertion engine, mocks functionalities and so one. It served as the influence to Jest. It's a little older, which means it has a large community around it, as Mocha does, and integrates with multiple other libraries and languages(the GitHub repo cites integrations with Python and Ruby).

One of the things that makes Jasmine a little odd to me is that it "pollutes" the global space with multiple constants and imports, which, to me, can drive to write something without knowing whether that thing is defined. As says Python's zen:

"Explicit is better than implicit"

Framework with support from large companies, as Yahoo and GitHub, it's very popular in the community. It's very stable and mature, based on its time in the community.

It doesn't come with an assertion or mocking library, but is very flexible and allows the user to integrate with a lot of options (chai and sinon are some examples, respectively).

Currently, the most popular testing framework(based on GitHub stars), it's written and maintained by the Facebook, which already maintains React. It is an assertion engine, using words like describe and it to write your tests(similar to RSpec's API if you're used to rails testing).

Has an awesome snapshot feature, which we will talk about soon, and is blazingly fast, running your tests in parallel (which brings a few pros and cons). Is the chosen one, for me.

It's a very new library (at the moment of this writing, it's 1.0 version is in beta), but with a good approach. Very minimalist, without implicit globals and has TypeScript support.

There's not much of what I can write, as haven't worked with it and it is still in beta, but would like to mention it.

Enzyme

A testing utility written by Airbnb, is an excellent tool used to simulate and renders out react components. It's not an assertion library, so we don't need it if we are not working with react.