Automatic REST JSON API test using Node.js + Mocha + Chai
This text was originally posted in Brazilian Portuguese here.
REST API is present in the majority of the projects that we work nowadays, and achieve a good sleeping night knowing that all endpoints are ok is part of the job. To reach this goal, automation is crucial.
In this article I will list the steps I made to create my first automated test using Node.js, the test framework Mocha and Chai, the lib that help us to write the test cases.
Installation
All the steps bellow were executed using a PC running Windows 10 and MinGW, but since these tools are cross platform, it shouldn't be so different in other systems.
First of all, install Node.js, you can download it in the oficial web site, an after that, open your terminal and run the following command to check the version (your version may vary, this was the version I got)
$ node -v
$ v9.5.0
next, you need to create a new folder to your project
mkdir api_test
cd api_test
Once in the folder, initialise the project
npm init
It's possible to provide the information about the project, but I will let all the default information, in the end type yes to conclude the process and create the package.json
file.
The package.json
content should be something like this
If you are not familiar with package.json
, this file holds all dependency references used by our software, helping other developers to install and run it in an "automagic way".
Now its time to install the items we need to proceed with the tests
npm install mocha -g --save-dev
npm install chai --save-dev
npm install should --save-dev
npm install request --save-dev
The installed packages are:
- Mocha — Test Framework
- Chai — Test Framework
- Should — Library to extend the tests
- Request — To execute API calls
At this moment, all dependencies are stored inside package.json
and a new folder called node_modules
was created.
Project folder structure
Mocha runs the tests automatically since the files are inside the folder test, so lets create it
mkdir test
What are we going to test?
We are going to use an API that returns info about the card game Magic the Gathering (which I like a lot).
That is a REST FULL API, so we will be able to test a various test scenarios, which is great to learn the concepts (but just to be aligned, this tutorial is just an entry point, the limits are up to you)
Writing the test case
Using your favorite text editor (mine is VS Code), let's create our first test case.
As I said earlier, mocha runs any file inside the folder test, so, let's create our first test case, to do that, we need to create a file called card.spec.js, with the following structure.
Running
To run the tests, we just need to execute the following command in the project's root folder:
mocha
The test result will be like bellow (success case)
Simulating an error (compares 400 with 200 in the test result) we will get the error:
Finalising
For sure there a lot of other functions details of Chai, but this post is just to get the basic needed structure to create your tests
Take a look in the code comments, they will help you and also the Chai's documentation is really good
The project can be downloaded on GITHUB.
If you want to talk to me regarding Node or Magic, send me a message on twitter, and if you are looking a good place to host your apps, click here and get USD100 to test =).
Photo by Glenn Carstens-Peters on Unsplash