20

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,
});
}
Mustafa Tawfig
  • 353
  • 1
  • 3
  • 9

3 Answers3

29

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

Raphaël
  • 3,646
  • 27
  • 28
6

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.

sorohan
  • 786
  • 6
  • 12
2

I had same issue for the code coverage both ways: command line or configuration.

Solution: We need to check the other component and spec files for errors because when we run ng test --code-coverage it's compiling the all files. So please ensure all spec and component files are without error.

Files with errors can be the reason you're not able get code coverage. If so, it shows the code coverage as 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 code coverage successfully:

================== Coverage summary ===============================
Statements   : 85% ( 17/20 )    
Branches     : 0% ( 0/2 )   
Functions    : 83.33% ( 5/6 )    
Lines        : 84.21% ( 16/19)   
ruffin
  • 16,507
  • 9
  • 88
  • 138