Unit test is a way to make sure your code is running well before integration into core application. The most popular library to achieve this is Mocha, NYC, Travis-CI, Gitlab-CI and Coveralls.

This article was updated at 07 Aug 2021.

If you are working as backend developer, unit test is a must thing to do. So before we going to the main tutorial, firstly we must know about :

Mocha

Mocha is the Javascript test framework which is running in NodeJS also in browser.

NYC

In the past we knew this as Istanbul, this is a command line interface for unit test.

Travis-CI

Travis CI is a hosted continuous integration service used to build and test software projects hosted at GitHub. We needed this to make sure our application is build passing or not. You have to register first and don’t worry, Travis is always free for open source projects.

Gitlab-CI

Gitlab CI is a continuous integration service used to build and test software projects by Gitlab. This Gitlab-CI only work if your source code is hosted at Gitlab.com only. If you don’t host at Gitlab, you can skip this and use Travis-CI.

Coveralls

Ensure that all your new code is fully covered, and see coverage trends emerge. You need this to know which line of your code is uncovered or skipped of being test. You have to register first and don’t worry, this free as long as your projects is open source.

TLDR

You are able to directly learn from source at here.

Setup Tutorial

Install Library

Make sure you install this library from NPM.

1
$ npm install mocha mocha-lcov-reporter coveralls nyc --save-dev

.gitignore

Create or edit your .gitignore file like this

1
2
3
node_modules
coverage
.nyc_output

.travis.yml

Create or edit your .travis.yml file like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
language: node_js
node_js:
- "node"
- "10"
env:
global:
- NODE_ENV=development
before_install:
- export TZ=Asia/Jakarta
install:
- npm install
script:
- npm test
after_success: npm run coverage

Travis CI will build and test your source code under NodeJS 10 and Latest version.

.gitlab-ci.yml

Create or edit your .gitlab-ci.yml file like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
stages:
- node10
- latest

cache:
paths:
- node_modules/

node_10:
stage: node10
image: node:10
script:
- npm install
- npm test
artifacts:
paths:
- coverage
coverage: /Statements\s+:\s(\d+.?\d+)%/

node_latest:
stage: latest
image: node:latest
script:
- npm install
- npm test
artifacts:
paths:
- coverage
coverage: /Statements\s+:\s(\d+.?\d+)%/

Gitlab CI will build and test your source code under NodeJS 10 and Latest version.

Note:

  • If your source code is not hosted at Gitlab, you can skip this and use Travis CI only.
  • This configuration also work with Gitlab Self Hosting or Community Version.

package.json

Modify your package json only on the scripts part.

1
2
3
4
"scripts": {
"test": "echo 'NodeJS:' && npx node -v && nyc --reporter=html --reporter=text --reporter=text-summary mocha",
"coverage": "nyc report --reporter=text-lcov | coveralls"
},

Run Test

You can just run your unit test with use command below

1
$ npm test

Badge

To make your project looks more confident and professional, you must put the badge from Travis and Coveralls into your readme.md file like this below

1
2
[![Build Status](https://travis-ci.com/YOUR_USERNAME/YOUR_PROJECT_NAME.svg?branch=master)](https://travis-ci.com/YOUR_USERNAME/YOUR_PROJECT_NAME)
[![Coverage Status](https://coveralls.io/repos/github/YOUR_USERNAME/YOUR_PROJECT_NAME/badge.svg?branch=master)](https://coveralls.io/github/YOUR_USERNAME/YOUR_PROJECT_NAME?branch=master)

If you are using Gitlab CI, badge are available by Gitlab and you don’t have to put manually into your readme.md file.
To add this badge to your project:

  • Navigate to your project’s Settings > General > Badges.
  • Under Name, enter Pipeline Status.
  • Under Link, enter the following URL: https://gitlab.com/%{project_path}/-/commits/%{default_branch}
  • Under Badge image URL, enter the following URL: https://gitlab.com/%{project_path}/badges/%{default_branch}/pipeline.svg
  • Submit the badge by clicking the Add badge button.

If you want to add Coverage Badge in Gitlab CI, everything is the same as above but,
Under Badge image URL, enter the following URL: https://gitlab.com/%{project_path}/badges/%{default_branch}/coverage.svg.

Code Standard or Code Style

Sometimes if you want to go for open source, you have to use code standard to make easier maintainer and contributor to improve your project. The most popular code standard for NodeJS is JSStandard, because it very easy to use.

So how to implement this? Follow this tutorial below.

Install JS Standard

1
$ npm install standard

package.json

Modify your package json only on the scripts part. Just add the standard into test part like this below.

1
2
3
4
"scripts": {
"test": "echo 'NodeJS:' && npx node -v && standard && nyc --reporter=html --reporter=text --reporter=text-summary mocha",
"coverage": "nyc report --reporter=text-lcov | coveralls"
},

Done, now your project is better, having code standard and far from bug.

Conclusion

In this article I just tell you how to setup unit test only. So actualy you have to create your unit test first by your self.

If you need the source code of this example, just download at my github here. Actualy, I use this for almost my open source library at NPM.

In the next article, I will write tutorial create simple app with unit test. So don’t forget to bookmark this website. I will always happy to share my knowledge for all of you guys.

Thank You.