10
import { Component, OnInit } from '@angular/core';
import { Title } from '@angular/platform-browser';

@Com(I)ponent({
  selector: 'app-fruit-cake',(E)
  templateUrl: './fruit-cake.component.html',
  styleUrls: ['./fruit-cake.component.scss'],
})
expo(E)rt class FruitCakeComponent {
  constructor(private _title: Title) {
    _title.setTitle('Cake');
  }
}

My code coverage report shows (E): else path not taken. (I): if path not taken. What does it mean in the context of import statements and decorators? My code coverage is not reaching to 100% in any of my tests due to this. How do I rectify it?

Vishal Anand
  • 1,431
  • 1
  • 15
  • 32

2 Answers2

20

For Angular 6, it seems that you need to "sourceMap": true to your test like this:

    "test": {
      "builder": "@angular-devkit/build-angular:karma",
      "options": {
        "main": "test/test.ts",
        "karmaConfig": "./karma.conf.js",
        "sourceMap": true,
        "polyfills": "src/polyfills.ts",
        "tsConfig": "test/tsconfig.spec.json",
        "scripts": [],
        "styles": [],
        "assets": [
          "src/assets"
        ],
        "codeCoverageExclude": []
      }
JFPicard
  • 5,029
  • 3
  • 19
  • 43
5

I had to remove:

--sourceMap=false

Previously, I had added this to help diagnose other issues. So my test script in package.json looked like this:

"test": "ng test --code-coverage --sourceMap=false"

I changed it to be:

"test": "ng test --code-coverage"

Then, all of my code coverage started reporting correctly. You may also want to verify your devDependencies in package.json. I am using Angular 6 so they look like this:

  "devDependencies": {
    "@angular/compiler-cli": "^6.0.3",
    "@angular-devkit/build-angular": "~0.6.8",
    "typescript": "~2.7.2",
    "@angular/cli": "~6.0.8",
    "@angular/language-service": "^6.0.3",
    "@types/jasmine": "~2.8.6",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "codelyzer": "~4.2.1",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~1.7.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.0",
    "karma-jasmine": "~1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.3.0",
    "ts-node": "~5.0.1",
    "tslint": "~5.9.1"
  }
Todd Palmer
  • 1,002
  • 10
  • 20
  • 1
    Please refer to this answer https://stackoverflow.com/a/51676221/4695469 below by @JFPicard for the best way to configure this. – Todd Palmer Nov 21 '18 at 14:06