10 July 2021
Edit on GitHub
— הפוסט זמין גם בעברית

Setting Up a Node.JS Development Environment - Setting Unit-Tests

A short manual to set up a standard Project Environment in NODE.JS - Part III - Setting Unit-Tests

Blog picture

Setting Up a Node.JS Development Environment

Part I – Setting TypeScript

Part II – Setting Linter

Part III – Setting Unit-Tests

Part IV – Setting Continues Integration (CI)




After the project environment is ready, it's time to set the most important part for the project stability, the Unit-Tests.

In this article, we will see how to integrate the test with mocha for the test runner environment, and chai for the tests assertions.

First, add the following libraries:

(ts-node used to run test without pre-build)

npm i --save-dev mocha chai ts-node @types/mocha @types/chai

In the package.json file under scripts section replace the test script content with

"test": "mocha -r ts-node/register src/**/*.spec.ts"

From now on, all files in the src directory that ends with spec.ts will consider as test files.

Create a new src/index.spec.ts test file.

In the new file import the test libraries and to the logic we want to test.

import { describe } from 'mocha';
import { expect } from 'chai';
import { addNumbers } from './index';

Describe test collection

describe('#Test addNumbers logic', () => {
	// The tests are here
});

Create a new unit test

it('Test positive numbers', () => {
	// The test logic is here
});

And inside, call to the addNumbers API, and verify the results using chai assertion.

it('Test positive numbers', () => {
    // Prepare test parameters
    const a = 1;
    const b = 2;
    const c = a + b;
    // Call to the API
    const addResults = addNumbers(a, b);
    // Make sure the results is as expected
    expect(addResults).to.be.equal(c, `"${a}" + "${b}" should be "${c}" but "addNumbers" returns "${addResults}"`);
  });

Let's add another test, to make sure the logic is OK also in negative numbers.

Also, another test collection to verify the legacy logic behavior.

The complete file should look like this:

import { describe } from 'mocha';
import { expect } from 'chai';
import { addNumbers } from './index';
import { addNumbersLegacy } from './legacy';

describe('#Test addNumbers logic', () => {
	it('Test positive numbers', () => {
		// Prepare test parameters
		const a = 1;
		const b = 2;
		const c = a + b;
		// Call to the API
		const addResults = addNumbers(a, b);
		// Make sure the results is as expected
		expect(addResults).to.be.equal(c, `"${a}" + "${b}" should be "${c}" but "addNumbers" returns "${addResults}"`);
	});

	it('Test negative numbers', () => {
		// Prepare test parameters
		const a = -1;
		const b = -2;
		const c = a + b;
		// Call to the API
		const addResults = addNumbers(a, b);
		// Make sure the results is as expected
		expect(addResults).to.be.equal(c, `"${a}" + "${b}" should be "${c}" but "addNumbers" returns "${addResults}"`);
	});
});

describe('#Test legacy addNumbers logic', () => {
	it('Test positive numbers', () => {
		// Prepare test parameters
		const a = 1;
		const b = 2;
		const c = a + b;
		// Call to the API
		const addResults = addNumbersLegacy(a, b);
		// Make sure the results is as expected
		expect(addResults).to.be.equal(c, `"${a}" + "${b}" should be "${c}" but "addNumbers" returns "${addResults}"`);
	});

	it('Test negative numbers', () => {
		// Prepare test parameters
		const a = -1;
		const b = -2;
		const c = a + b;
		// Call to the API
		const addResults = addNumbersLegacy(a, b);
		// Make sure the results is as expected
		expect(addResults).to.be.equal(c, `"${a}" + "${b}" should be "${c}" but "addNumbers" returns "${addResults}"`);
	});
});

You can see the full file here index.spec.ts

Now, run the npm run test command, and ... that's it. yes, simple as that 😊

For now, everything is ready, but if we want to get a beautiful report about the tests coverage we can get it.

Add the packages

npm i --save-dev mocha-lcov-reporter nyc

In the package.json under scripts add the following line:

"cover": "node \"node_modules/nyc/bin/nyc.js\" --exclude src/**/*.spec.ts --reporter=lcov npm run test"

This script will invoke the nyc with the test runner command, so the test will run, and meanwhile nyc will get the coverage data.

Run the npm run cover command, then you can see a new folder named .nyc_output that contained the report raw data, and another folder named coverage inside it, there is a lcov-report/index.html file, open it in any browser.

As you can see, we are on the 100% coverage 🥇🥇🥇

What else can we do with this report?

In the next article will see how to build a full CI using all the tools we already integrated into the project and more.