Skip to content

How to: Code Coverage

Srishti Hunjan edited this page Nov 27, 2017 · 3 revisions

Code Coverage

Front-end (Angular)

With the Angular CLI we can run unit tests as well as create code coverage reports. Code coverage reports allow us to see any parts of our code base that may not be properly tested by our unit tests.

To generate a coverage report run the following command in the root of your project:

ng test --watch=false --code-coverage

We could even chose to keep the watch on if the developer wants to see the various unit tests run in the the browser:

ng test --watch=true--code-coverage

Once the tests complete a new /coverage folder will appear in the project. This folder contains the index.html file, which contains a report with the source code and code coverage values.

Setting the code coverage percentage

Using the code coverage percentages we can estimate how much of our code is tested. Our team has decided to ensure that a minimum of 80% of the code must be tested for it to be merged and moved to production.

For this we added the following code in karma.conf.js:

coverageIstanbulReporter: {
  reports: [ 'html', 'lcovonly' ],
  fixWebpackSourcePaths: true,
  thresholds: {
    statements: 80,
    lines: 80,
    branches: 80,
    functions: 80
  }
}

Back-end (Node.js)

Note: This is likely to get changed as our understanding of node.js integration tests increases.

Install istanbul globally:

npm i -g istanbul

Install mocha globally:

npm i -g mocha

In the test folder, run:

istanbul cover mocha test.spec

Authors:

Clone this wiki locally