Jest vs Mocha: Getting started with Testing

It’s important to test our code. We’ve heard it a zillion times, and we all know how integral it is to the healthy growth of an application. The thing that’s tricky is when you’re just getting started and don’t really know what sorts of tests suites are good for the types of projects you’re working on. Mocha and Jest are two of those suites, and I think an overview might help guide you when choosing one for a project.

A note on Jasmine

This blog is mainly going to talk about Jest and Mocha. However, this doesn’t mean that I didn’t check out a third, very popular, test suite called Jasmine. After doing some research, I discovered that since Jasmine works best with angular projects, and since I currently don’t write code with angular, decided to omit it from this overview. That doesn’t mean you shouldn’t check it out! It’s an older testing language that’s implemented in loads of applications!

Now that the elephant in the room has been addressed, let’s check out Mocha!

Mocha

Mocha is a Javascript test framework that runs off of the browser and Node.js. You can check out the official documentation here. By itself, all Mocha really is, is a test runner. That means that out of the box Mocha’s functionality is limited. However, much like anything that uses node, it can be expanded upon with npm packages and other dependencies. This is good because you will only need to add things to mocha that you need, but at the expense of knowing what sorts of npm packages to install. That doesn’t mean that getting started with testing your code isn’t easy! In fact, once you’ve imported the assert function from Node, you can get started writing tests for your code! Also, check out my previous blog on writing async tests in Mocha!

As a tip, a very useful package that works well with Mocha is nyc. Nyc is a code coverage tool. Code coverage tools are great for seeing what functions haven’t been accounted for in your tests. Here’s how you’d add it to your package.json file.

"scripts": {           old --> new        "scripts": {"test": "mocha"                            "test": "nyc mocha"},                                        },

Jest

Jest is another Javascript testing framework. This time developed by Facebook. You can check out the documentation here. Unlike Mocha, Jest offers a lot more out of the box. Not only is it a test runner, but it also has seamless integration with frameworks such as React and Vue without the need to install extra dependencies. In fact, if you’re building a React project, there are hardly any dependencies at all! That means that you kind of get a giant toolbox with all of your tools already inside of it, instead of having to shop around for different tools to use ala Mocha.

Remember that code coverage tool, nyc, that I had mentioned earlier? You don’t even have to worry about tools such as this. Jest has a code coverage tool already built in! It can be accessed by changing your test scripts in your package.json from

"scripts": {           old --> new        "scripts": {"test": "jest"                            "test": "jest --coverage"},                                        },

Which is better if you’re just getting started?

There are a lot of things that you need to ask yourself when choosing between Mocha and Jest. They are both totally useable options, but here’s a review of what we’ve gone over/some other things to consider.

Overall, my recommendation would be to get started with Mocha, especially if you’re a beginner developer using only pure Javascript. It’s simple to install and will give you a good understanding of the types of tools you will prefer to use as you develop as a tester. If you happen to have recently jumped into using more Javascript frameworks, then Jest is probably for the best!

--

--

--

Software Engineering Graduate from Flatiron School. Former expatriate and music teacher.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Left only 1hr before start Nomadland IDO!! 🎉

Linked List with JavaScript — Part 2 (Questions Only)

How to Use React Hooks With the GraphQL API

SocketIO testing using JMeter

Monotonic Queue Algorithms (Google Interview)

Redux Flow

Building Fixtures with Factories

Modern Asynchronous JavaScript — from callbacks to promise and now awaiting the async.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kevin Gleeson

Kevin Gleeson

Software Engineering Graduate from Flatiron School. Former expatriate and music teacher.

More from Medium

Explaining Directus services

My experience with creating a Solid application

Solid

Design and Implementation

System context for the weather station

Why GrpahQL is a better alternative to REST?