Cloud costs
September 16, 2022
min read

Node.js Unit Testing Automation With Drone CI Using Mocha Framework


There’s no way around it: unit testing is an essential piece of any software development lifecycle. In today’s modern cloud-native space, automated testing is in high demand with many platforms and tools available. This testing automation can be done through a continuous integration (CI) process. CI is an essential method in the overall DevOps process. CI helps developers test and build their code, so they can make sure that nothing is breaking and the code can be confidently integrated into the main branch with a simple approval by an approver. The CI process can be seamlessly automated with Drone CI to test, build, and give confidence to your developers. CI lets developers save time on manual testing and focus on building the rich features that their customers need. Today, we’ll demonstrate how to do CI using Drone CI, and taking a simple Node.js application and Mocha as our unit testing framework.


  • Download and install Node.js from the official website here.
  • Drone CI set-up.


Step 1: Create a folder for all of your application files:

mkdir myapp
cd myapp

Step 2: Write a simple Node.js application.

Initialize your project by running the following command:

npm init

This creates a package.json file with all of the dependencies that you downloaded for your project.

Step 3: Install express, the Node.js web application framework: 

npm install express --save

Step 4: Next, we must create a file named app.js with the following code:

var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
app.listen(3000, function () {
console.log('Example app listening on port 3000!');

Step 5: Run the app:

node app.js

Mocha Unit Testing Framework

The Mocha testing framework is a JavaScript library which is used for testing JavaScript code. It’s an open source framework and gives developers a lot of flexibility. It has a simple API and can be used to test both synchronous and asynchronous code. It also has features such as chaining, callbacks, and promises. Furthermore, it provides an API for running tests in parallel, which speeds up the process of writing tests for a large codebase. The Mocha testing framework can be installed by using the Node Package Manager (NPM).

Step 6: Let’s add the test folder to our app directory where all of our tests will reside.

Create a folder named test, and under that, create a file test.js.

You can install Mocha and Chai as part of the Node.js test framework:

npm install --save-dev mocha

Add Mocha as a dependency in the package.json file for the particular project.

Step 7: Add the following basic Mocha string test spec to your test.js file:

var assert = require('assert');
describe('Basic Mocha String Test', function () {
it('should return number of characters in a string', function () {
assert.equal("Hello".length, 4);
it('should return first charachter of the string', function () {
assert.equal("Hello".charAt(0), 'H');

Step 8: You can go back and check your package.json file. It should have the Mocha part included, and it should look like this:

"scripts": {
"test": "mocha"

Step 9: Now, run the test.

Go to your app directory and run the following command:

npm test 

My output is as follows:

It’s failing, as there seems to be an error when Mocha was run. You can see 1 failing.

The source code of the example project is shared here.

CI for Node.js application

Until this point, we locally tested the Node.js application, and this can be automated through a CI server. Drone CI is our preferred CI platform that we already know how to run locally. It’s open source and easy to install in just minutes.

If you don’t know how to set up a Drone server on your local machine, then here is my colleague’s article that will help you have your own Drone CI up and running

Every time a developer pushes the code to the main branch of the application, using a CI server like Drone helps companies find bugs and rectify the errors and mistakes before they reach customers. 

Let’s continue with the next steps.

Step 10: Push your application to GitHub.

We should make sure that no file or repository is missing. In our GitHub repository, we still need to add two files:

  1. .drone.yml as part of the Drone configuration files. Pipelines are configured by placing a .drone.yml file in the root of your Git repository.
  2. Dockerfile to build and publish a Docker image as a step in our build pipeline. Therefore, add a simple Dockerfile, as it contains instructions to build your container. 

FROM node:14-alpine

ENV NODE_ENV development

# Add a work directory


# Cache and Install dependencies

COPY package.json .

RUN npm install

# Copy app files

COPY . .

# Expose port


# Start the app

CMD [ "node", "app.js" ]

The .drone.yml file looks like this:

kind: pipeline

type: docker

name: default


 os: linux

 arch: arm64 


- name: test

 image: node


 - npm install

 - npm test

 - name: docker 

 image: plugins/docker


   # registry:

   repo: pavansa/myapp


     from_secret: docker_username


     from_secret: docker_password


     - pavans

Pipelines are configured by placing a .drone.yml file in the root of your Git repository. The YAML syntax is designed to be easy to read and expressive, so that anyone viewing the repository can understand the workflow. See here for a simple node example.

Step 11: Now, let’s open the Drone dashboard and sync our new repo. Then, we’ll activate it with secrets.

After syncing the repos, you should see all of your repos as follows, and pick the one that we’re building.

The next step is to activate the repository by adding secrets.

After all of the above steps are done, select the NEW BUILD button and it will start building the pipeline.

Here you will see Drone doing its magic by building the code and testing it against the tests that we specified for our application code (remember, we have added a plain Mocha test framework to our application). If the tests pass, then it should go ahead and push a newly built image to our DockerHub as specified by us in the .drone.yml steps.

Moreover, if the test fails, then the application pipeline shouldn’t build and should stop there.

Let’s see what happened to our build.

As expected, the pipeline failed without carrying out the next steps. You can go back, correct the errors, and Drone automatically runs the pipeline. If everything is good, then it pushes the new build to Docker Hub as expected. 

When everything is passed, go check your Docker Hub repo that you mentioned in the .drone.yml. It should have pushed the image there. There is the tag that we gave in pavans, and you can see it here: 

Congratulations! We’ve successfully built a simple Node.js application, added a simple Mocha test, used Drone to do CI, and pushed the new image to the Docker Hub. 

To learn more about how Harness can help you with your CI processes, request a personalized demo.

Sign up now

Sign up for our free plan, start building and deploying with Harness, take your software delivery to the next level.

Get a demo

Sign up for a free 14 day trial and take your software development to the next level


Learn intelligent software delivery at your own pace. Step-by-step tutorials, videos, and reference docs to help you deliver customer happiness.

Case studies

Learn intelligent software delivery at your own pace. Step-by-step tutorials, videos, and reference docs to help you deliver customer happiness.

We want to hear from you

Enjoyed reading this blog post or have questions or feedback?
Share your thoughts by creating a new topic in the Harness community forum.

Sign up for our monthly newsletter

Subscribe to our newsletter to receive the latest Harness content in your inbox every month.

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Continuous Integration