NYC

Test coverage report in Typescript with Mocha and NYC

0 0
Read Time:2 Minute, 46 Second

We will review how to configure NYC as a coverage report tool for a simple Typescript project using Mocha for unit tests.

$ mkdir typescript-mocha-nyc
$ cd typescript-mocha-nyc
$ npm init

Install the Typescript dependencies.

$ npm install typescript --save-dev
$ npm install @types/node --save-dev

Create a tsconfig.json file and add the configuration below

{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"rootDir": "src",
"outDir": "build",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}

src will be the root directory where our .ts files will reside.

build will be the output directory where our .js transpiled files will be stored after being generated by tsc.

The code to be tested will be very simple, create the file src/index.ts and place the following.

export function sum(a: number, b: number): number {
return a + b;
}
export function substract(a: number, b: number): number {
return a - b;
}
export function multiply(a: number, b: number): number {
return a * b;
}
export function divide(a: number, b: number): number {
return a / b;
}

Create tests folder

$ mkdir tests

Add the dependency.

$ npm install --save-dev mocha @types/mocha ts-node

Configure the test script in the package.json file.

Here we are indicating mocha to use the Typescript execution engine and to execute all the *spec.ts files inside the tests folder including subfolders.

"scripts": {
"test": "mocha -r ts-node/register tests/**/*.spec.ts"
}
import { divide, multiply, substract, sum } from "../src";
var assert = require('assert');
describe('Index tests', function() {
it('2 + 2 should return 4', function() {
assert.equal(sum(2, 2), 4);
});
it('2 - 2 should return 0', function() {
assert.equal(substract(2, 2), 0);
});
it('2 * 2 should return 4', function() {
assert.equal(multiply(2, 2), 4);
});
it('2 / 2 should return 1', function() {
assert.equal(divide(2, 2), 1);
});
})

Verify everything is working fine by running our tests.

$ npm test

Output

> mocha -r ts-node/register tests/**/*.spec.ts
Index tests
✔ 2 + 2 should return 4
✔ 2 - 2 should return 0
✔ 2 * 2 should return 4
✔ 2 / 2 should return 1
4 passing (5ms)
$ npm install --save-dev nyc @istanbuljs/nyc-config-typescript

NYC configuration can be placed in a separated file .nycrc or included in the package.json file, for this example let’s just place the following in package.json.

"nyc": {
"extends": "@istanbuljs/nyc-config-typescript",
"check-coverage": true,
"all": true,
"include": [
"src/**/!(*.test.*).[tj]s?(x)"
],
"exclude": [
"src/_tests_/**/*.*"
],
"reporter": [
"html",
"lcov",
"text",
"text-summary"
],
"report-dir": "coverage"
}

extends adds instrumentation for Typescript files support.

include specifies the files to be included in the report.

exclude specifies the files to be committed from the report, we don’t have to report coverage on tests files itself.

report-dir specifies the folder where the reports will be generated.

Finally, edit the scripts section on package.json to include the coverage script.

nyc will use the previous test script to run mocha tests.

"scripts": {
"test": "mocha -r ts-node/register tests/**/*.spec.ts",
"test:coverage": "nyc npm run test"
},

Generate the coverage report by executing.

$ npm run test:coverage

In the coverage folder, you’ll find the HTML report generated.

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %