Facebook Jest, the JS testing tool for people who hate writing tests

Jest is a Javascript testing tool for people who thinks writing tests is boring and they want the experience to be as painless and short as possible.

Ignacio Segura
CloudBoost
Published in
5 min readDec 20, 2017

--

Update: Code example for your convenience! I just published a shortened version of the real JEST project this article is based on. Here’s the repo on GitHub.

I’m pretty sure there are people out there that are passionate about testing suites and writing tests. Thank God this people exist, because every coder knows automated testing is a must for projects that grow beyond a certain size… but most them don’t want to be the person who writes those tests. I’m one of those. In my personal ranking of enjoyable tasks, testing is just one step over writing documentation.

Well, maybe two steps.

Anyway. We know we need an automated mechanism to check nothing gets screwed when we change some code -notice I said “gets screwed” and not the more honest “we screwed”-. We know it the hard way because… well, we know it the hard way.

So, despite what I said before, automated tests are awesome because they save a lot of pain on the most important people of any project: us, the developers. But we also know we want to get rid of this task as soon as possible because having tests is much cooler than having to write them one by one.

So, when I had to deal with the task of finding a good Javascript testing suite I not only looked for a solid tool. I also wanted something that could take us to cruising speed in short time and with short pain. Setting up, studying the framework, getting results, understanding cryptic error messages… all these are potential pain points I wanted to avoid. After checking a few alternatives, I decided to give Jest a run. And I saved myself a lot of stress.

Our use case

I was working on SIASAR, a data mining and analysis project funded by World Bank focused on water supply and sanitation systems in rural areas. Many pieces of software were being developed at the same time, but all them rested on one key component: a Drupal 7 powered API for data storage and retrieving. If the API failed, nothing else could work. If it failed the same morning a hundred surveyors were going into the field for data collection, that was catastrophic.

So our priority was to monitor our API health. Loggin in, fetching data, checking data structure, saving some dummy data, cleaning up, logging out. All this done had to be done using asynchronous calls, sending and reading JSON text. The tests needed to be able to run on both server and local with no changes in configuration. Any local: Windows, Linux, Mac.

Setting up is a one-liner

Jest is the defacto testing tool for React, but it can be used easily with any other projects such as ours. The only thing you need to use it is Node.JS installed in your machine. At the time of writing this, Jest uses Jasmine2 as default testing engine, but don’t think Facebook just took it and put a name on it. Quite the opposite, there is a huge amount of work on it with just one objective in mind: make the whole process easier.

For example, let’s add Jest to your project, managed by Node NPM.

npm install --save-dev jest

That’s it. No dependencies, no “you also need to install blablabla and edit blablabla”. If you have Node.JS (who doesn’t?), you’re set.

If you need integration with Babel, instructions for integration are included in Jest documentation. And you can also use Jest with Typescript using an additional component, ts-jest.

Jest finds your tests automatically

Going further on the idea of as little setup work as possible, Jest has an auto-discovery mechanism. Quoting the documentation:

Place your tests in a __tests__ folder, or name your test files with a .spec.js or .test.js extension. Whatever you prefer, Jest will find and run your tests.

Just put your tests in whatever folder or group of folders you need, and Jest will find them.

Jest runs in parallel

All tests are launched at once, and all them are independent processes, so they can go really fast. But there’s some flexibility with that parallel execution. For example, it offers mechanisms to run a single process before or after running all tests just once, and not once for every test. For example, authenticating before running the tests and logging off and cleaning temporary files after that.

Besides that, if you want to run just one test, Jest also offers you a way for that. Great for debugging.

Jest looks simple, like plain English

Jest keeps the good parts of Jasmine’s syntax and try to keep it “human friendly” for everything built on top of that. Example of a real test of mine:

This is an example of a real test we use daily. Jasmine users will find the syntax very familiar. First, we give our test a name. Then, we declare an asynchronous arrow function to be processed when the data is available, as explained here.

Inside our function, we do a couple things. First, we tell Jest how many things we’re going to evaluate in this test. Second, we get our data, and finally, we check our stuff. It almost looks like plain English. And that’s great.

I love async/await, by the way, things get much easier. You need Node 7.6 or higher to use it.

Jest can be debugged just like any other Node.JS module

The other problem I was worried about was about debugging the test while I was writing it. Nothing to worry about, actually, just google for help on how to debug Node modules on your particular code editor and you’re all set.

Jest is watching

Another handy detail when you’re writing a new test. Jest has a watch mode, which means it is able to watch the project folder, so when you save changes in a file, it runs again either all the tests (watch all) or just the ones that changed (watch).

Jest is highly recommended

We needed a testing suite to check the output of a API service we were developing. The stability of the API was mission critical, so any unexpected change or issue needed to be found ASAP. But at the same time, we were short on people, so any time spent testing was being taken from other developments.

So, to summarize, we needed something easy to understand, fast to setup, rock solid and well documented. Jest covers all four requirements we had. I totally recommend it.

Bonus

Example code available: you can find a reduced, simplified copy our real JEST project for SIASAR on GitHub.

--

--

Graphic design nerd focused on all things visual and interactive. Currently working as consultant for World Bank.