A pragmatic approach to QA and OPS

Unit Testing Mongo DB Connection

In our previous post, we talked about MongoDB in detail. Now we will discuss in detail how you can use the power of JavaScript to test basic CRUD operations in MongoDB.


In MongoDB, there is no concept of tables, rows and columns. Since it is a non-relational database, data is stored in different format in MongoDB. Data is saved in what is called Collections .  A database model is a type of data model that determines the logical structure of a database and fundamentally determines in which manner data can be stored, organized and manipulated. A `Schema`  tells the model what to expect.

We’ll use the following libraries to test the normal CRUD operations

  • Mocha
  • Chai
  • MongoDB
  • Mongoose
  • RoboMongo or Robo3T
  • Mochawesome


Let’s talk a little bit about all of these –



Mocha is flexible testing framework that gives flexibility of running asynchronous tests with ease. Mocha gives the describe and it syntax that can be used to make BDD styled test cases.

Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases.



Chai is a assertion library as specifically mentioned in it’s page. Chai has an extensive range of assertions available that can be paired with almost any .js testing framework to assert something .

Chai has several interfaces that allow the developer to choose the most comfortable. The chain-capable BDD styles provide an expressive language & readable style, while the TDD assert style provides a more classical feel.

Chai gives the flexibility of having assertions in different styles. You can have something like







We’ve already talked about MongoDB, so I’m not gonna detail this.



As mentioned in the Mongoose’s web page

Mongoose provides a straight-forward, schema-based solution to model your application data. It includes built-in type casting, validation, query building, business logic hooks and more, out of the box



RoboMongo or Robo3T is a light weight GUI for working with Mongo DB. Robo3T embeds the same engine and environment that is a part of MongoDB shell.

Robo 3T (formerly Robomongo) not only analyzes the semantic of the code, but also executes it in an internal JavaScript VM, allowing us to give you a runtime autocompletion that is impossible to obtain statically.


Mochawesome is a awesome HTML/JSON reporter for Mocha tests. It can be embedded easily with Mocha tests for any format of test cases.


However, before installing any packages or libraries from above, make sure that you have node installed on your system. It would be preferable if the node version is above 8.0.


In next tutorial, we’ll get started with the repository created and installation of packages and write our first tests.

%d bloggers like this: