Request a Call Back
Fields marked with an * are required
Testing Node.Js APIs with Chai Assertion Library - Habilelabs

Let’s start Testing Node.Js APIs with Chai Assertion Library. I will discuss it step by step.

Installation to setup with Chai Assertion Library:

Required npm Modules: –
a. Mocha
b. Chai
c. Chai-HTTP


a. Mocha –
Mocha is a JavaScript framework which runs on Node.Js for the testing purpose of the node APIs. Mocha gives the accurate testing results, run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases.


b. Chai –
Chai is the assertion library, similar to Node’s build in assert. It makes testing much easier by giving lots of assertions like should, expect, assert etc. so that we can run this against our code.

c. Chai-HTTP –
Chai-HTTP is a addon plugin for the Chai Assertion Library. It is used to test HTTP apps or external services. This will helpful for integration testing. We may use a function (such as an express or connect app) or a Node.Js HTTP server as the foundation for our request. If the server is not running, chai-HTTP will find a suitable port to listen on for a given test.

Let’s Start with Testing Node.Js APIs :

We can also insert this npm module in package.json file as like below: –

and then run npm install.

Database: –
In this, we are using a test database for the testing purpose. It may be helpful for the better testing. We include a constant named MONGO_URI_TEST in our config file for example: –

And in the server.js we are using node environment for the test database like this: –

How to set NODE_ENV: –
In the package.JSON file we include scripts like this: –

Here the test is the node environment we are using the testing APIs.

Server.js File: –
In the server.js file, we have to include


This line of code exports the server for testing purposes.

Test Cases: –

First, make a separate directory named test in the route. In this directory, we have to write all our test cases for each modal of a project separately.
Like we have a Book Schema So we make a file book.js and in this file, we wrote all the APIs test or we can make separate files for all the test cases.

1.For Getting API

2.For Post API

3.For Delete API

If you have any query and question in mind about Node JS APIs Testing then ask in comment box.

Know 4 Types of Mistakes a Node JS Developer Makes!

Hope you enjoy this post then don’t forget to share with friends.