Karma , Istanbul - code coverage report Unknown% ( 0/0 )

Viewed 8486

I'm getting this Coverage Summary

=============================== Coverage summary ===============================
Statements   : Unknown% ( 0/0 )
Branches     : Unknown% ( 0/0 )
Functions    : Unknown% ( 0/0 )
Lines        : Unknown% ( 0/0 )
================================================================================

I applied the changes instructed in the Angular Documentation for code coverage: https://angular.io/guide/testing#enable-code-coverage-report

but I keep getting the same empty summery.

my karma.conf.js

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', '@angular-devkit/build-angular'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage-istanbul-reporter'),
      require('@angular-devkit/build-angular/plugins/karma')
    ],
    client: {
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    coverageIstanbulReporter: {
      dir: require('path').join(__dirname, './coverage/singleWindow'),
      reports: ['html', 'lcovonly', 'text-summary'],
      fixWebpackSourcePaths: true,
      thresholds: {
        statements: 80,
        lines: 80,
        branches: 80,
        functions: 80
      }
       },
    reporters: ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false,
    restartOnFileChange: true,
});
}
3 Answers

I had the same issue with Angular 7 when running tests using ng test.

As it turns out, Angular CLI disable code coverage by default. You have to start your tests using ng test --code-coverage for it to work.

You can make it always on by adding "codeCoverage": true to the test task of your angular.json file:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "name-of-your-app": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "architect": {
        "build": {
          /* ... */
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.spec.json",
            "karmaConfig": "./karma.conf.js",
            "scripts": [],
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "codeCoverage": true
          }
        }
      }
    }
  }
}

For more informations: https://angular.io/guide/testing#enable-code-coverage-reports

When the coverage is Unknown% that means you've probably enabled it correctly.

Make sure the "sourceRoot" property in the angular.json is set to the root folder for the project's source files.

The ng cli uses the files in "sourceRoot" to compute coverage.

It doesn't seem to do anything else with sourceRoot for tests, so the tests run and pass, but coverage doesn't work.

I had same issue for the code coverage both way command line or configuration through

Solution: Need to check the other component's an spec files having some errors or not bcz when we run the ng test --code-coverage its compiling the all files so please ensure all spec and component's files without error that is reason i not able get code coverage it shows the code coverage like below

==================== Coverage summary =============================== Statements : Unknown% ( 0/0 )
Branches : Unknown% ( 0/0 )
Functions : Unknown% ( 0/0 )
Lines : Unknown% ( 0/0 )

After fixing all component and spec file errors i get success code coverage ================== Coverage summary =============================== Statements : 85% ( 17/20 )
Branches : 0% ( 0/2 )
Functions : 83.33% ( 5/6 )
Lines : 84.21% ( 16/19)

=========================================================

Related