MongoDBUtil Unit Tests – Node.js API with TDD Tutorial

After the unit tests for MongoDB Module file, we will continue with writing unit test cases for the MongoDBUtil file to test and write the functional code for its expected behavior.

Unit Test 3: Reading MongoDBUtil Object

Just like the Unit Test 1 for the MongoDBModule, we could write the same spec for the mongodb.util.js file as well. In this test, MongoDBUtil will be expected to be an object.

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

Below will be the test result, and it will successfully be running the three specs that we wrote so far.

Unit Test 3 - MongoDBUtil Object Spec - Passed
Unit Test 3 – Reading MongoDBUtil Object – Passed

Unit Test 4: Initializing MongoDB Connection

So far we have covered the basic test suites for both the javascript files. The fundamental reason for this module is to initialize the MongoDB connection while the application is starting up. This test will cover this functionality/behavior.

Even though we could write a unit test for testing the MongoDB connection initialization code block, somehow I am not able to mock or stub the mongoose library-related objects and functions and also not able to assert the result of the connection with the spec. If you can get this unit tested, please let me know, will include that in this tutorial.

Test Script

So, for the connection initialization behavior, we will write the spec to check that there is an init function that takes care of the MongoDB connection within itself. Also, we will write the complete required script in the module.util.js (MongoDBUtil) file for which we will not have any expectations written in the spec.

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

Moreover, this re-running the mocha test will show one error as shown in the below picture as the new spec didn’t pass. 

Unit Test 4 - Initializing MongoDB Connection - Failed
Unit Test 4 – Initializing MongoDB Connection – Failed


We will have to add the code block in the mongodb.util.js file so that all the tests will pass again.

module.exports = {
    init: init

function init() {

Let’s fill in this init() function with the actual code in just a while. 

Meantime, let’s try to make running the mocha tests automatic. As we cannot keep running the npm test command manually every time we change some code in the module files. We could save time if the tests run whenever there is a change in the javascript. It could be achieved by updating a line in the package.json file.

Replace the below in the package.json file,

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

With the below line. 

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

By merely adding –watch with the modules directory as an additional parameter to the mocha command, we are instructing the mocha test runner to keep a watch on the files under the modules directory, and it will run the test suites whenever there is any change in the javascript files.

After applying the above changes, once changing any of the code will trigger the tests automatically and will show whether that change affected the test suites or not either passing or failing the tests.

Here are the test results so far for the MongoDB module files based on the TDD approach.

Unit Test 4 - Initializing MongoDB Connection - Passed
Unit Test 4 – Initializing MongoDB Connection – Passed

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.