Mocha, Jest, Jasmine, Enzyme... there are plenty of options, but which one fits your needs? Read on for analysis of these great testing tools.
Join the DZone community and get the full member experience.Join For Free
Trust, but verify. A Russian phrase made popular by president Reagan fits perfectly when we are talking about testing or QA. Everyone trusts themselves, every programmer or a developer has trust in code. And why shouldn’t they?
But, we know how it goes. In the programming world, even a miniscule mistake could cost a lot. And that is why we have testing.
The front-end ecosystem is advancing and is not getting any simpler. Companies today compete fiercely to hire front-end developers well-versed in their field. With each passing year the intricacies tend to double.
Let’s look at some of the tools to you help in your React.js project
Another choice for simplicity and ease of use is Mocha CLI. Which is fairly agreeable because Mocha CLI easily runs your Mocha tests in a Node.js environment. Unfortunately, a mechanism to run tests in the browser using the Mocha CLI does not exist.
If you want to perform cross-browser testing or you want to test significant DOM manipulations, go for Karma. Or else, you can use a combination of the default CLI and additional libraries like Enzyme and JSDOM.
Make sure your test framework supports testing both asynchronous and synchronous code and make it quite easy to disable the test. Jasmine, Mocha, and Jest meet these requirements.
Here is some sample code from Jasmine.
Jasmine contains assertion abilities, called matchers, baked in. These assertions eliminate the need to include external assertion libraries such as Chai and Should.js. It also allows you to mock functions to cut a few dependency trees out of the scope.
Facebook’s test framework, Jest, was specifically designed for React. Jest responds to basic testing needs pretty well, but the highlight of the day is snapshot testing. It lets you serialize and save the output of components while easily detecting when that output changes. Snapshots are text files saved into source control and checked against future test runs.
Additionally, Jest offers basic assertions and mocking functionality like Jasmine.
Coming back to Mocha, it focuses on being a solid test framework. When you look at it, there aren’t any assertion libraries or fancy types of testing it can do. However, what it does, it does well.
If you choose Mocha, it means you also have to exclusively choose assertion libraries and mocking libraries. You can probably just use Chai and Sinon.js in this case. If you want to look for more options, there is a wiki page for Mocha that outlines the common ones.
If you want to perform shallow rendering and assertions on the component, there are two options:
React-test renderer for shallow rendering.
React-test-utils for assertions.
There is another option, the Airbnb library called Enzyme. It is a testing utility for React that makes it easier to assert, manipulate, and traverse your React components' output.
Another option is using Sinon.js, a powerful mocking library with detailed assertions. It also provides a great mock implementation of the clock and easy mocking for AJAX requests.
Opinions expressed by DZone contributors are their own.