Node.js automated test (Mocha+Istanbul)

Keywords: Web Development npm JSON calculator

Based on Express, Mocha + Istanbul

1. Unit Test

1.1 Install Mocha

npm i -D mocha

1.2 Create test/test.js

var assert = require('assert');
describe('Array', function() {
  describe('#indexOf()', function() {
    it('should return -1 when the value is not present', function() {
      assert.equal([1,2,3].indexOf(4), -1);
    });
  });
});

1.3 Run Test Case

Add test to package.json scripts node as below.

  "scripts": {
    "start": "node ./bin/www",
    "test": "mocha"
  }

Run npm test, will get result in the terminal.

> mocha



  Array
    #indexOf()
      √ should return -1 when the value is not present


  1 passing (5ms)

2. Code Coverage

2.1 Install Istanbul

The new version of the Istanbul command line tool has been changed to nyc.

$ npm i -D nyc

2.2 Run Test

Modify package.json

{
  "scripts": {
    "test": "nyc mocha --timeout=3000"
  }
}

Run npm test, and the results are as follows:

> nyc mocha --timeout=3000



  Array
    #indexOf()
      √ should return -1 when the value is not present


  1 passing (8ms)

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |        0 |        0 |        0 |        0 |                   |
----------|----------|----------|----------|----------|-------------------|

Because no code was tested, the coverage of each item in the above example is 0.

2.3 examples

Add the following code for testing.

//calc.js
let calc = new Object();
calc.add = function(x, y){
    return x+y;
}
calc.minus = function(x,y){
    return x-y;
}
module.exports = calc;

Modify test.js.

//test/test.js
var assert = require('assert');
var calc = require('../calc');

describe('Calculator', function() {
  describe('add', function() {
    it('add(1,2) should return 3', function() {
      assert.equal(calc.add(1,2),3);
    });
  });
});

Run the test and the result becomes:

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |    83.33 |      100 |       50 |    83.33 |                   |
 calc.js  |    83.33 |      100 |       50 |    83.33 |                 6 |
----------|----------|----------|----------|----------|-------------------|

Modify test.js and add test cases.

//test/test.js
var assert = require('assert');
var calc = require('../calc');

describe('Calculator', function() {
  describe('add', function() {
    it('add(1,2) should return 3', function() {
      assert.equal(calc.add(1,2),3);
    });
  });

  describe('minus', function() {
    it('minus(1,2) should return -1', function() {
      assert.equal(calc.minus(1,2),-1);
    });
  });
});

Run the test and the result changes to 100%.

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |      100 |      100 |      100 |      100 |                   |
 calc.js  |      100 |      100 |      100 |      100 |                   |
----------|----------|----------|----------|----------|-------------------|

2.4 pit + +

There is still a problem with the above report, which only contains one file, and no other files in the project appear, indicating that Istanbul only counts the files in the test.

var app = require('../app');

After import ing the App entry file into test.js, the result is as follows.

-----------------|----------|----------|----------|----------|-------------------|
File             |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
-----------------|----------|----------|----------|----------|-------------------|
All files        |    82.93 |        0 |    33.33 |    82.93 |                   |
 autotest        |    83.87 |        0 |       50 |    83.87 |                   |
  app.js         |       80 |        0 |        0 |       80 |    27,33,34,37,38 |
  calc.js        |      100 |      100 |      100 |      100 |                   |
 autotest/routes |       80 |      100 |        0 |       80 |                   |
  index.js       |       80 |      100 |        0 |       80 |                 6 |
  users.js       |       80 |      100 |        0 |       80 |                 6 |
-----------------|----------|----------|----------|----------|-------------------|

Reference:

https://mochajs.org/
https://istanbul.js.org/

Posted by wama_tech on Sun, 15 Dec 2019 08:45:30 -0800