Unit testing nodejs CRUD APIs in 2019 (Part 1)

In my previous article, we covered unit testing front end react native applications using Detox. I would like to continue on the subject of unit testing but for server side applications. In this article, I’m going to look into unit testing strategies for a nodejs CRUD API that is built on express and mongodb. My API currently has JWT authentication implemented along with a photo upload route that uploads an image to Amazon AWS.

The state of server-side js unit tests in 2019

Let’s look at some of the options available to us for testing node applications. A quick google search on node testing leads us lots of different testing strategies and frameworks. Mocha, Chai, and Sinon to name a few. But before we can talk about them, let us familiarize ourselves with some of the terminologies that you might encounter when it comes to the topic of software testing.

Test Runner — A test runner is a software library or tool that targets a directory in your source code and executes the test scripts located within. This process usually happens in the console.

Testing Framework — In order to write testing scripts that the test runner can pick up and execute we need to actually write some code. You’ll be using a testing framework to write these scripts. It is the testing framework that can understand the code that we write, it handles setup, teardown, and structure.

Assertion Library —In order to test software we have to write assertions. Assertions are boolean expressions that will either be true or false. When our assertions are valid, the test can be marked as passed. If there is a bug in our software our assertions will fail. Some frameworks will come with their own assertion library, like Jasmine.

Code coverage — Code coverage is a measurement based on the number of lines that your automated tests cover. It is usually a report that shows you the percentage of code executed.

So now that we have that out of the way let’s pick a testing strategy. We need a test runner, a testing framework and an assertion library to start out with. Since we’re testing a crud api, it would make sense to pick a terminal based testing framework. Our first immediate candidate is Mocha. It is by far the most popular node testing test runner/framework bundle. Its always a good idea to look at the github repository to make sure its actively being maintained. But just to make sure let’s look at some alternatives to Mocha. I found this link that gives us a nice list of candidates.

After looking at this list, Based on our criteria, Jasmine seems like a good candidate; it includes its own assertion library, while Mocha doesn’t. Jasmine also includes spies, which are test double functions. Which one will we choose?

Enter SuperTest

Supertest is a library that was created just for testing APIs. It is well documented and it is commonly used with Mocha and Chai. It makes API testing super concise. I think it is a good fit for our project, so this is the reason why I’m choosing to go with Mocha and Chai. Without further ado, let's begin installing our testing environment.

To be continued in Part 2…