MongoDB Module Unit Tests – Node.js API with TDD Tutorial

In this post, we will begin with writing unit test cases for the mongodb.module.js file to test and write the functional code for its expected behaviour.

Unit Test 1: Reading MongoDBModule Object

Let’s start the first test for the mongodb.module.js file. We want to make sure that the mongodb.module.js file is a module that exposes an object. So we can write the spec like this. This test will be written inside the inner describe() block of the base file content that we just created in the above section.

it('should read the mongodb.module file', function () {

Since we completed the first test for our codebase, let’s run the test suite. To run the test for the whole application we need to add some script in the package.json file. Below is the line that needs to be inserted within the ‘scripts’ of the package.json file for running the test.

"test": "mocha --recursive ./tests"

Once it’s added, go to the root directory of the application and run the below command in the terminal. It will run the mocha test runner with all the specs available inside the /tests directory recursively. 

npm test

Already we have added the functional code in the mongodb.module.js file. So this time the test will run successfully with the one passing test spec as below. 

Unit Test 1 - Reading MongoDBModule Object
Unit Test 1: Reading MongoDBModule Object

Unit Test 2: Reading MongoDBUtil Object

The purpose of the mongodb.module.js file is to expose all of its files. So that we don’t have to ‘require’ any of this module’s files separately in other modules’ files wherever needed. So far, we have one another file mongodb.util.js file along with this file. 

Test Script

Next step would be to add a spec in this test file to read the MongoDBUtil object via the MongoDBModule object. For this, the following it() block with that specific expectation would be appropriate to go with.

it('should confirm MongoDBUtil exist', function () {

The spec is defined to expect the attribute MongoDBUtil of the MongoDBModule object is an object type just like any module file. This way we are confirming that MongoDBUtil attribute is existing in the MongoDBModule object. 

If we run the mocha test now, we will eventually get the new test failed. Below is the screenshot of the test run with the current state of the javascript files.

Unit Test 2 - Check MongoDBUtil Exists - Failed
Unit Test 2 – Reading MongoDBUtil Object – Failed

As per TDD, we are on the right track now. We just wrote a failing unit test. The next step would be to write minimal code in the mongodb.module.js to make this test pass. 


Since this test is expecting an attribute MongoDBUtil in the MongoDBModule object, we can accomplish this expectation with the below code added within the module.exports object block in the mongodb.module.js file.

MongoDBUtil: require('./mongodb.util')

Let’s run the test once again, and this time it will pass as we just added the code for the spec’s expectation. Following is the screenshot of the passing test.

Unit Test 2 - Reading MongoDBUtil Object - Passed
Unit Test 2 – Reading MongoDBUtil Object – Passed

We got the second test run successfully. To abide by the TDD philosophy, let’s see what we can do to refactor the code if there is an opportunity. By looking at the mongodb.module.js, there is not much we do to refactor it. So we can leave it as it is for now.

Although there is no need to refactor the main javascript file, there is one little thing we can to do in the mongodb.util.spec.js file, which is the test suite for the mongodb.util.js file. You may have noticed in the mongodb.util.spec.js file that we require the mongodb.util.js directly.

var MongoDBUtil = require('../../../modules/mongodb/mongodb.util');

As we have seen in the above section, the main purpose of the mongodb.module.js file is to expose all other files that belong to the module. So it would be appropriate to refactor the line as mentioned earlier as below. What we are doing here is getting the MongoDBUtil object via the mongodb.module.js file.

var MongoDBUtil = require('../../../modules/mongodb/mongodb.module').MongoDBUtil;

Rerunning the mocha test runner will make all the existing specs to pass successfully as we just refactored the scripts without changing any of the behaviours of the functional code base.

Please check out the index page for this Node.js RESTful API development with TDD approach tutorial with all the posts in sequence in one place.

This blog post is an excerpt from the book Building Node.js REST API with TDD approach. Please check out the link for more information.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.