Leena Bhegade
3 min readJul 7, 2019

Unit testing Node.js api using mocha, chai, sinon and nock!

Recently I worked on a node.js project which had api’s written without any proper test cases. Before making any changes to the api I refactored the existing code and added unit tests for the amended code. This api had authentication middleware setup on all endpoints and made HTTP calls to some external api’s. I am writing this article to provide some help with writing basic unit test for such node.js endpoint.
Follow below steps to start unit testing your node.js api’s. Before starting to write any unit-tests lets first make sure we have installed below modules:

npm install chai
npm install nock
npm install sinon
npm install mocha

Let’s write a simple test now that all our test modules are installed. Let’s say we have an api endpoint which needs to be tested and this endpoint makes http calls to some external api and also has authentication middleware setup.

Let’s go through this unit test file step by step:
We will require all the modules we need to unit test the code in the beginning of the file. Use describe to give title to your tests and logically group your tests. Now let’s do some setup for each of the tests.

We definitely need to stub the authentication middleware and mock the HTTP calls as we want to run all the unit tests in complete isolation.

In the before step we will stub authentication middleware method to run next() every time it gets called when the unit tests are run. If your endpoints don’t have authentication on them then you don’t need to do this. Also I have required my server (this is your main server.js file that starts up your express server) after stubbing authentication. I have done this so that when my server file is initialised it calls the stubbed authentication method instead of the real one.

The before step will be run once before all the tests are run inside the describe. If you want to stub different method’s and get different values from each test then it will better to use beforeEach before every test.The afterEach step is added just to make sure we do nock.cleanAll() which will cleanup state after a failed test.

Let’s now start writing our actual test. Use nock firstly to mock the external endpoint that is being called from our api. In the nock function specify the base url of the external server and in the get function pass the exact endpoint you’re trying to reach and in reply function pass the status code and response body you’re expecting to get.

Now using chai.request make a call to your api which you are trying to test. Specify the endpoint path in the get function and store the results of the request in a response and then validate your response using expect.

Use mocha command to run your tests as below:
mocha — exit path/to/your/test/files/*.js

This simple unit test example was testing a get endpoint. I hope it helps you to setup simple tests if you’re just started working on node.js applications 😊

Leena Bhegade

I like to code and cook.. Learning is everyday process for me.