How create tests for Restful APIs with Nodejs

Vinicius Gularte
Jan 10 · 3 min read

We will use the Mocha framework and the Chai library for make easy our tests.

Little explanation about chai and mocha

What is mocha ?

Mocha is a JavaScript test framework running on Node.js and in the browser,making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases.

And chai ?

Chai is a BDD / TDD assertion library for Node.js and the browser that can be delightfully paired with any JavaScript testing framework.

The combination of Mocha and Chai today is best known for performing API Rest tests developed in NodeJS.

Creating our environment of tests

Let’s start our environment of tests, we make a folder called awesomeRestfulTests, inside type NPM init to create our package.json.

Now he have to install chai and mocha in our project.

npm install --save-dev mochanpm install --save-dev chai 

We too have to install a chai plugin called Chai HTTP for integrating HTTP with chai assertions.

npm install --save-dev chai-http

Now he have this package json.

Nice, now we are create our structure folders like this

For execute the tests we will use the command mocha, followed by the file we want execute, however, its more simple create a command for execute all tests in one time, it is not? For accomplish this we will create the follow command in our package.json:

Running the command npm test in terminal, will execute all tests inside the folder tests.

Obs: If you have a slow Internet, use --timeout 15000 after mocha and before the path of tests.

Making the tests

credit: Novastock/REX/Shutterstock

We use this fake online API for make the tests called jsonplaceholder, we use this routes for make the tests:

GET(all) https://jsonplaceholder.typicode.com/posts

GET https://jsonplaceholder.typicode.com/posts/1

POST https://jsonplaceholder.typicode.com/posts

PUT https://jsonplaceholder.typicode.com/posts/1

DELETE https://jsonplaceholder.typicode.com/posts/1

Testing get posts method

In the file getPost.test.js, we call the libraries to make the tests, in the first test we expected to receive a post with id = 1, he must have a title and a body in his response too.

He must return the status equal to 200 in the call, like the HTTP protocols says.

Now we have to make another test in get method, we will receive all posts, the only thing will change is expected to receive an array of objects, not only one like before.

Testing post posts method

Now we create the test for the post method, in the post method we will send these parameters:

In our test we expected to return a status 201(Successful Created), and the same parameters with same values as we have sent.

Testing put posts method

To test the update method of our posts, the only thing that will change in relation to the post method is that this time we will send the id of the post that we want to update, besides using the method .put.

Testing delete posts method

Test delete methods, is very simple, we use the method .delete and expected receive a status equal to 200, which means the success of the requisition.

Conclusion

Each of these libraries are very useful for validating your code and should be used in just about all of your Node projects, this is a small demonstration of how they can help us in our day to day.

All this project you find here, feel free to 👏 and share with others, see you soon 😃

Vinicius Gularte

Written by

Developer From Brazil and passionate writer. https://viniciusgularte.github.io/

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade