Running Mocha Chai Tests via Gitlab CI

Wavda Aniva
3 min readFeb 24, 2023

--

This article demonstrates how we can run Mocha Chai tests in Gitlab CI Pipeline. In this article, I will go through the step-by-step starting with setting up the test project and the pipeline. Before that, it’s great to start by getting a better understanding of Mocha, Chai, and Gitlab.

What is Mocha Chai?

In software testing, Mocha and Chai are actually two separate testing frameworks for JavaScript. Mocha is a JavaScript testing framework that provides a structure for organizing and running tests, while Chai is an assertion library that provides a set of functions for writing more expressive and understandable assertions.

Mocha and Chai can be used to create and run automated tests for JavaScript code. Mocha provides a framework for writing and organizing test suites, while Chai provides the functions for asserting the behavior of the code being tested.

What is GitLab?

GitLab is a web-based Git repository manager that provides source code management, continuous integration and deployment, issue tracking, and more. It is a complete DevOps platform that enables developers to collaborate on code, test and deploy applications, and monitor their performance.

GitLab is built on top of Git, which is a distributed version control system that allows developers to manage and track changes to their code over time. GitLab provides a web-based interface that makes it easy for developers to work with Git and collaborate with other team members.

Some of the features provided by GitLab include:

  • Git repository management with support for branching, merging, and versioning
  • Issue tracking and project management tools
  • Continuous integration and deployment pipelines
  • Code review and collaboration tools
  • Monitoring and analytics tools for tracking application performance and usage

GitLab can be installed on-premises or used as a cloud-based service, and it is available in both free and paid versions, with the paid versions providing additional features and support.

Pre-requisites

  1. Gitlab account
  2. Node.js downloaded and installed. Reference: link

Setup Mocha Chai Test

1. Create a new folder for your new project. Within the new folder, using terminal, install dependencies for Mocha, Chai, and Mocha Junit Reporter.

npm install mocha chai mocha-junit-reporter --save-dev

2. Create a simple test file for example called basicOperation.spec.js

const assert = require('assert');

describe('Mathematical Operations - Test Suite', function() {
this.timeout(500);
const a = 20;
const b = 10;

it('Addition of two variables', function(done) {
this.timeout(300);
setTimeout(done,100);
const c = a+b;
assert.equal(c,30);
});

it('Substraction of two variables', function() {
const c = a-b;
assert.equal(c,10);
});

it('Multiplication of two variables', function() {
const c = a*b;
assert.equal(c,200);
});

it('Division of two variables', function() {
const c = a/b;
assert.equal(c,2);
});
});

Within the package.json file, add the following configs:

{
"scripts": {
"test": "mocha '**/*.spec.js' --reporter mocha-junit-reporter --reporter-options mochaFile=junit.xml"
},
"devDependencies": {
"chai": "^4.3.7",
"mocha": "^10.2.0",
"mocha-junit-reporter": "^2.2.0"
}
}

Setup Pipeline

Create a file called .gitlab-ci.yml and set up test stages. The following config will allow you to use the default Gitlab runner and store the junit.xml report as the pipeline artifact.

image: node:latest

cache:
paths:
- node_modules/

stages:
- test

test:
stage: test
script:
- npm install
- npm test
artifacts:
when: always
reports:
junit:
- junit.xml

Push your project into a Gitlab project. Your test project directory will look something like this once everything has been set up. The project name in this example is called mocha-basic.

Run your pipeline and within the pipelines report, you should be able to see the following test report.

Test summary
Test job log

--

--

Wavda Aniva
Wavda Aniva

Written by Wavda Aniva

A curious potato exploring new things on software quality

No responses yet