How to use Mocha/Chai to test Node.js apps

How to use Mocha/Chai to test Node.js apps

In this guide, we'll create a simple Node application with the Express.js framework, and cover it with Mocha/Chai tests. In the latter part of the article, we'll show you how to automate the testing using Buddy CI/CD.

Automate Mocha testing in 15 minutes ⏰
Try Buddy for Free

What is Mocha framework

Mocha is a feature-rich JavaScript testing framework and one of the simplest suites for Node.js test cases available. It is designed for unit testing and integration testing, and supports both Test Driven Development (TDD) and Behavior Driven Development. Contrary to other frameworks, such as Jasmine or Jest, this test suite allows for flexible and accurate reporting, asynchronous testing, test coverage reports and can use any assertion library.

What is Chai library

Chai is a behavior and test-driven development library that which works perfectly well with Mocha – and Jasmine – frameworks. It allows you to make assertions about values, types, and behaviors in your tests, helping you verify the expected outcomes of your code.

Configuration

Install Node.js

Make sure you have the npm manager installed: nodejs.org/en/download/package-manager

Install npm and Mocha

Create a directory for the application:

bash
mkdir myapp && cd myapp$

Now, initalize npm. We'll use it to create a package.json with the Mocha framework:

bash
npm init$

When asked for the details of the application provide the following:

  • name: hello-world
  • entry point: app.js
  • test command: ./node_modules/.bin/mocha We shall use this framework to test the application.

You can confirm the rest of the values with enter.

Create Hello World with Express framework

To build the app, we'll use Express Node.js web application framework:

bash
npm install express --save$
Hint
Using --save adds this package to package.json where all dependencies are stored.

TableMain of Hello World

With everything installed, we can create app.js, a JS file with a simple HTTP server that will serve our Hello World website:

js
//Load express module with `require` directive var express = require('express') var app = express() //Define request response in root URL (/) app.get('/', function (req, res) { res.send('Hello World') }) //Launch listening server on port 8080 app.listen(8080, function () { console.log('App listening on port 8080!') })

Run the app

The application is ready to launch:

bash
node app.js$

Go to http://localhost:8080/ in your browser to view it.

Configuring unit tests with Mocha and Chai

Every application requires testing before the deployment to the server, especially a welcome site that determines the first impression. In this test case, we shall use Mocha as the test running framework, and Chai as the assertion library to apply unit testing to our application.

Install Mocha and Chai

Let's add Mocha and Chai packages to the package.json:

bash
npm install mocha chai --save-dev$

Add a test file

Time to define our first unit test. To keep things in order, we shall store all test files in a separate test directory:

bash
mkdir test$

Now, add the first test file:

bash
touch test/test-pages.js$
Success
Writing and properly covering your application with tests is the best practice to delivering quality software. Make sure to add test files for new test cases regularly to keep your code bug-free and high performing.

The test script verifies the content of the website. For that, we need an HTTP client: https://npm.io/package/request

bash
npm install request --save-dev$

Our sample Mocha test file with the unit test should look like this now:

js
var expect = require('chai').expect; var request = require('request'); it('Main page content', function(done) { request('http://localhost:8080' , function(error, response, body) { expect(body).to.equal('Hello World'); done(); }); });

Run the file to trigger the tests:

bash
npm test$

Image loading...Running npm test

Let's add some more tests that will check the status of the homepage and /about page:

js
var expect = require('chai').expect; var request = require('request'); it('Main page content', function(done) { request('http://localhost:8080' , function(error, response, body) { expect(body).to.equal('Hello World'); done(); }); }); it('Main page status', function(done) { request('http://localhost:8080' , function(error, response, body) { expect(response.statusCode).to.equal(200); done(); }); }); it('About page content', function(done) { request('http://localhost:8080/about' , function(error, response, body) { expect(response.statusCode).to.equal(404); done(); }); });

Run npm test again and see the results. The /about page is not ready yet, so it will return a 404:

Image loading...Running npm test (expanded)

Grouping tests

A very useful feature in Mocha testing is describe(), a function that allows you to better control your tests by grouping them – like in this code snippet:

js
var expect = require('chai').expect; var request = require('request'); describe('Status and content', function() { describe ('Main page', function() { it('status', function(done){ request('http://localhost:8080/', function(error, response, body) { expect(response.statusCode).to.equal(200); done(); }); }); it('content', function(done) { request('http://localhost:8080/' , function(error, response, body) { expect(body).to.equal('Hello World'); done(); }); }); }); describe ('About page', function() { it('status', function(done){ request('http://localhost:8080/about', function(error, response, body) { expect(response.statusCode).to.equal(404); done(); }); }); }); });

Run npm test yet again. Looks like the results are different now:

Image loading...Test results with `describe()`

Automating Mocha tests

The main principle of CI is that every change to code should be automatically tested for errors. Contrary to popular belief, this does not mean employing a DevOps expert to set up a CI server and script the whole process in the terminal window.

Instead, you can use Buddy, a tool which provides a preconfigured test environment for your JavaScript code and speeds up configuration and running tests.

Version control

Before we get down to automation, though, we need to start with the very basis of CI/CD. All source code, including package.json, must first be put under version control.

bash
git init echo 'node_modules' > .gitignore git add . git commit -m 'my first commit'$$$$
Warning
node_modules have been added to .gitignore as dependencies should never be stored with in the source code repository.

Now, if you want to cooperate on a project with other developers (and automate your tests!) you need to add a remote location on your Git hosting service and push the code to it:

bash
git remote add origin URL git push origin --all$$

Pipeline configuration

With your project under version control, you can configure a pipeline that will streamline the whole testing process to a git push. Configuration does not require scripting as it's fully visualized in the UI:

Image loading...Mocha/Chai testing pipeline in Buddy CI/CD

  1. Sign in to Buddy with your Git provider and sync it with your project:

  2. Add a new pipeline and set the trigger mode to the git push event.
  3. Add the Node action and define your commands just like you did locally.
Success
And that's it – from now on, Buddy will automatically run your Mocha tests whenever someone makes a push to the selected branch. 🚀
Hint
On top of unit testing, Buddy supports all types of tests: unit tests, functional tests, integration tests, and so on. You are also free to run tests in any type of test framework, web browsers, as well as test mobile apps.

Additional resources

Actions used in this guide:

Jarek Dylewski

Jarek Dylewski

Customer Support

A journalist and an SEO specialist trying to find himself in the unforgiving world of coders. Gamer, a non-fiction literature fan and obsessive carnivore. Jarek uses his talents to convert the programming lingo into a cohesive and approachable narration.