Skip to content
An example of how you can use Mocha and Chai to perform API testing for a Node-ExpressJS-Mongoose app.
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.


Type Name Latest commit message Commit time
Failed to load latest commit information.



Node.js CI

An up-to-date 2020 example of how you can use Mocha and Chai to perform API testing for a Node-ExpressJS-Mongoose app.

Local testing and online automated testing

You will be able to test your code locally and also automatically when you push your code to Github, using Github Action.

Built for easy deployment

Furthermore, the code is set up such that it will automatically switch to Production Keys rather than Development Keys on deployment.

Test suite

In this project, a single test suite: test/productRoutes.test.js is used to test a single Express route: routes/productRoutes.js which handles API calls pertaining to a single Mongoose model: models/Product.js.

Work-in-progress: Currently, there is only a single test case testing the 'GET /api/products' route. A full-fledge implementation and associated testing of POST, GET, PATCH and DELETE routes will be added by end March 2020.

Local testing guide

To deploy this project locally for development purposes, follow the steps below.


To begin, first install and set up the following.

  1. Install MongoDB locally, take note of your mongoURI. Alternatively, you can use mLab instead.
  2. Install NodeJS.
  3. Clone this repository (e.g. enter git clone in your command-line terminal).

Project setup

Next, set up the NodeJS application with the following steps.

  1. Open the server sub-directory (folder) within the mocha-chai-mongoose folder you cloned (e.g. cd server).
  2. Install the required packages (which are specified in package.json) by entering npm install.
  3. Open config/example_dev.js and fill in the mongoURI to your development MongoDB instance.


Finally, you can execute the Mocha test suites under the test/ directory.

  1. Check that you are in the server sub-directory.
  2. Run this project on your local server by running npm test (check out the scripts section in package.json for more info).
  3. To exit the test, press CTRL + C.

Automated testing via Github action

Check out the Github Action workflow for this project: nodejs.yml. This workflow performs automated testing via npm test each time you do a git push to your Github repo.

It uses:

Background info

What is Mongoose

MongooseJS provides a straight-forward solution to modeling your NodeJS application data easily in MongoDB. It handles all MongoDB validation, casting and business logic on your behalf.

What is Mocha

Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser. It is our test environment of choice for this project.

What is Chai

Chai is an assertion library for Node. We use Chai assertions and chai-http in Mocha to perform HTTP testing of our NodeJS API endpoints.

You can’t perform that action at this time.