Jest vs Mocha: Which Should You Choose?
A brief overview of two major testing frameworks and a little bonus called Enzyme
Jest is an open-source testing framework that has become increasingly popular recently. Developed by Facebook and built into the popular
Want to read this story later? Save it in Journal.
Jest also provides snapshot testing. Snapshot testing is a great way to ensure that your UI doesn’t change unexpectedly. Snapshots render a UI component, take a screenshot of it, and compare the component to a reference image stored with the test. Snapshots make testing basic UI components extremely simple with one line of code:
The greatest thing about Jest is that it just works right out of the box. There’s no up front configuration that you have to do. You just install it through npm or yarn, write your test, and run
jest. It’s that simple. If you do want to change the configurations, Jest allows you to do so in the
package.json with many configuration options.
So, with a completely configurable testing framework, which assertion, mocking, and spy libraries should you use? Chai is one of the most popular open-source assertion libraries used with Mocha. Chai is a BDD/TDD assertion library with an abundance of plugins and extensions. Sinon is a popular mocking and spy library. With robust documentation on fakes, stubs, spies, and mocks, Sinon is easy to pick up and integrate with Mocha.
Mocha is an older and more mature open-source project than Jest. The community is large and provides a vast variety of documentation, StackOverflow questions, and support articles for if you get stuck in configuration.
Which should you choose?
Which framework will work best for you is entirely dependent on what your project demands. If you have a large project with the need for flexibility and customization then Mocha is probably the choice for you. If you have a smaller project and don’t need the extra setup and configuration up front, Jest is probably the better option. The choice is yours as to which framework is going to be the best solution for you and your team.
Enzyme was developed by Airbnb for testing React components’ outputs. Enzyme shallow renders your components so that you ensure that your component is being passed the correct props and behaviors. Enzyme can be used in addition to any testing framework. Enzyme is easy to configure and has additional libraries for integration with popular libraries (like
Note: If your project is using
React 16.x, you will need to configure an Adaptor in your project with the
More from Journal
There are many Black creators doing incredible work in Tech. This collection of resources shines a light on some of us: