angular-cli: ng test --code-coverage in 6.1 improperly detecting branches

Bug Report or Feature Request (mark with an x)

- [ X ] bug report -> please search issues before submitting
- [ ] feature request

Command (mark with an x)

- [ ] new
- [ ] build
- [ ] serve
- [ X ] test
- [ ] e2e
- [ ] generate
- [ ] add
- [ ] update
- [ ] lint
- [ ] xi18n
- [ ] run
- [ ] config
- [ ] help
- [ ] version
- [ ] doc

Versions

OS: macOS Sierra Node: v8.9.4 NPM: 6.2.0

Ng Version:

Angular CLI: 6.1.0
Node: 8.9.4
OS: darwin x64
Angular: 6.1.0
... animations, cli, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.7.0
@angular-devkit/build-angular     0.7.0
@angular-devkit/build-optimizer   0.7.0
@angular-devkit/build-webpack     0.7.0
@angular-devkit/core              0.7.0
@angular-devkit/schematics        0.7.0
@angular/cdk                      6.4.1
@angular/flex-layout              6.0.0-beta.16
@angular/material                 6.4.1
@ngtools/webpack                  6.1.0
@schematics/angular               0.7.0
@schematics/update                0.7.0
rxjs                              6.2.2
typescript                        2.9.2
webpack                           4.9.2

Repro steps

Run ng test --code-coverage Notice that things like import statements are counted as if else branches. image

The log given by the failure

No log file

Desired functionality

I expect to see things like import statements and decorator metadata to not be marked as if else branches in the code coverage report.

Mention any other details that might be useful

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 38
  • Comments: 35 (1 by maintainers)

Commits related to this issue

Most upvoted comments

Maybe it’s the way that the sourceMaps are handled. If I add “sourceMap”: true, in the test section, with @angular-devkit/build-angular 0.7.0 it seems to work.

See: https://stackoverflow.com/questions/51257459/if-else-path-not-taken-in-imports-in-angular-istanbul-code-coverage-report/51676221#51676221

I was able to fix the issue by adding "sourceMap": true, in angular.json test section.

DevDependency versions are,

    "@angular-devkit/build-angular": "~0.7.0",
    "@angular/cli": "~6.1.2",
    "@angular/compiler-cli": "^6.1.0",
    "@angular/language-service": "^6.1.0",
    "@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",
    "typescript": "~2.7.2"

we’re planning a bug bash sprint starting in 1.5 weeks; as a P1 this issue will be on it.

In my case passing the sourceMap flag via CLI did fix the issue (ng test --source-map) while setting it in tsconfig.json didn’t seem to have any effect.

I am still having the same issue with all dependencies completely up to date

I can confirm this issue. And I can confirm that running it using ng test --code-coverage --source-map solves the issue.

Here’s my ng -v output:

Angular CLI: 6.2.1 Node: 10.9.0 OS: linux x64 Angular: 6.1.7 … animations, common, compiler, compiler-cli, core, forms … http, language-service, platform-browser … platform-browser-dynamic, router

Package Version

@angular-devkit/architect 0.8.1 @angular-devkit/build-angular 0.7.5 @angular-devkit/build-optimizer 0.7.5 @angular-devkit/build-webpack 0.7.5 @angular-devkit/core 0.8.1 @angular-devkit/schematics 0.8.1 @angular/cli 6.2.1 @ngtools/webpack 6.1.5 @schematics/angular 0.8.1 @schematics/update 0.8.1 rxjs 6.3.2 typescript 2.9.2 webpack 4.9.2

Thought it might be worth noting that I’m still seeing this issue… I’m currently using Angular 6.2.7 I currently have “@angular-devkit/build-angular”: “~0.8.7”,

After adding "sourceMap": true to my angular.json test options, checking that it was included in tsconfig.json, and manually trying to run ng test --code-coverage=true --watch=false --source-map=true I still have had zero success getting code coverage working and detecting branches properly.

Just had the same problem start happening to me,

  • angular: 6.1.10
  • devkit 0.8.5

@lanegoolsby 's solution worked out for me, didn’t have to add anything into the CLI commands, just the angular.json file edit

For anyone here from Google, I may have found a more permanent fix than using the CLI flag. Set “sourceMap”: true in angular.json under projects -> [yourProject] -> architect -> test -> options.

Making the change in the tsconfig.spec.json or tsconfig.json had no effect for me.

My environment:

Angular CLI: 6.1.3 Node: 8.11.3 OS: win32 x64 Angular: 6.1.2 … animations, common, compiler, compiler-cli, core, forms … http, language-service, platform-browser … platform-browser-dynamic, router

Package Version

@angular-devkit/architect 0.7.3 @angular-devkit/build-angular 0.7.3 @angular-devkit/build-optimizer 0.7.3 @angular-devkit/build-webpack 0.7.3 @angular-devkit/core 0.7.3 @angular-devkit/schematics 0.7.3 @angular/cli 6.1.3 @ngtools/webpack 6.1.3 @schematics/angular 0.7.3 @schematics/update 0.7.3 rxjs 6.2.2 typescript 2.7.2 webpack 4.9.2

Also confirm running ng test --code-coverage --source-map solves this, despite all settings being set to sourceMap:true. Of course, this advice conflicts with the advice given in #11968. 😞

Same issue as @frankrue - setting sourcemap to true did not resolve the issue.

EDIT: A bit more clarification - it does not work for libraries generated with Angular CLI 6. sourceMap option was added to angular.json and all tsconfig files.

I can confirm that setting sourceMap: true fixed the issue for me I am on 0.7.2 @brian428 @jfpicard1 Guessing this shouldn’t need to be on since it worked without it before?

Had the same problem after upgrading to cli 6.1.1 + devkit 0.7.1. Fixed by downgrading @angular-devkit/build-angular to 0.6.8

Before (with coverage issues):

Angular CLI: 6.1.1
Node: 8.11.2
OS: win32 x64
Angular: 6.1.0
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.7.1
@angular-devkit/build-angular     0.7.1
@angular-devkit/build-optimizer   0.7.1
@angular-devkit/build-webpack     0.7.1
@angular-devkit/core              0.7.1
@angular-devkit/schematics        0.7.1
@angular/cli                      6.1.1
@ngtools/webpack                  6.1.1
@schematics/angular               0.7.1
@schematics/update                0.7.1
rxjs                              6.2.2
typescript                        2.9.2
webpack                           4.16.3

After (coverage working properly again):

Angular CLI: 6.1.1
Node: 8.11.2
OS: win32 x64
Angular: 6.1.0
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.7.1
@angular-devkit/build-angular     0.6.8
@angular-devkit/build-optimizer   0.6.8
@angular-devkit/core              0.7.1
@angular-devkit/schematics        0.7.1
@angular/cli                      6.1.1
@ngtools/webpack                  6.0.8
@schematics/angular               0.7.1
@schematics/update                0.7.1
rxjs                              6.2.2
typescript                        2.9.2
webpack                           4.16.3