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.

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
Not providing any solution here, but the problem is likely to be in Devkit 0.7 instead of CLI 6.1.
same issue after upgrade to 6.1.1. coverage dropped from 100% Statements : 98.13% ( 3667/3737 ) Functions : 99.47% ( 752/756 )
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
I'm guessing something is wrong in the creation or handling source maps?
Hope this gets fixed soon need to be on latest version of the @angular-devkit/build-angular because of the issue in 0.6.8 of it not exiting with proper error code but now this coverage report issue is a problem. Willing to try to help fix it as well if anyone can point me where to start looking.
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'm on 0.7.1, and adding "sourceMap": true to the test section (or via the command line) doesn't seem to affect the messed up coverage report output.
Maybe you have a tsconfig.json with "sourceMap": false ?
Here my DevDependecies:
"devDependencies": {
"@angular-devkit/build-angular": "~0.7.0",
"@angular/cli": "^6.1.1",
"@angular/compiler-cli": "^6.1.0",
"@angular/language-service": "^6.1.1",
"@types/jasmine": "^2.8.8",
"@types/jasminewd2": "~2.0.2",
"@types/node": "^10.5.5",
"codelyzer": "^4.4.2",
"jasmine-core": "^2.99.1",
"jasmine-spec-reporter": "^4.2.1",
"karma": "^2.0.5",
"karma-chrome-launcher": "^2.2.0",
"karma-cli": "^1.0.1",
"karma-coverage": "^1.1.2",
"karma-coverage-istanbul-reporter": "^2.0.1",
"karma-html-reporter": "^0.2.7",
"karma-ie-launcher": "^1.0.0",
"karma-intl-shim": "^1.0.3",
"karma-jasmine": "^1.1.2",
"karma-jasmine-html-reporter": "^0.2.2",
"karma-phantomjs-launcher": "^1.0.4",
"karma-typescript": "^3.0.12",
"karma-typescript-angular2-transform": "^1.0.2",
"karma-typescript-plugin": "^0.1.2",
"lite-server": "^2.2.2",
"rxjs-tslint-rules": "^4.7.2",
"ts-node": "^7.0.0",
"tslint": "^5.11.0",
"tslint-angular": "^1.1.2",
"tslint-consistent-codestyle": "^1.13.3",
"tslint-microsoft-contrib": "^5.1.0",
"typescript": "^2.7.2"
}
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?
Hmm, updated to 0.7.2, but still no luck. tsconfig.spec.json doesn't set sourceMap: false. I even added sourceMap: true just to try it (even though it's already set to true in the base tsconfig.json), but had no effect. The test section of my angular.json also includes this, but no luck there either:
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"karmaConfig": "./karma.conf.js",
"sourceMap": true,
...
}
}
None of these tweaks make any difference. The coverage report is generated but the highlighting is all wrong.
I have tried various versions and combinations of "sourceMap" set to true/false with no luck. My particular use case is a library that will later be imported by another app, so downgrading anything is proving problematic, as the "library" generation in Angular CLI 6 is fresh and shiny and new.
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.
fixed by adding "sourceMap": true thanks to @jfpicard1
Adding sourceMap: true does appear to fix the code coverage problem for me, but it seems to make failure stack traces disappear:
Without sourceMap: true:
HeadlessChrome 70.0.3508 (Linux 0.0.0) CheckoutRegisterPageComponent should create FAILED
TypeError: this.companyService.getCompanyRefs is not a function
at CompanyDetailsFormComponent.ngOnInit (webpack:///../common-ui/src/app/shared/company-details-form/company-details-form.component.ts?:47:33)
at checkAndUpdateDirectiveInline (webpack:///./node_modules/@angular/core/fesm5/core.js?:9545:19)
at checkAndUpdateNodeInline (webpack:///./node_modules/@angular/core/fesm5/core.js?:10809:20)
at checkAndUpdateNode (webpack:///./node_modules/@angular/core/fesm5/core.js?:10771:16)
at debugCheckAndUpdateNode (webpack:///./node_modules/@angular/core/fesm5/core.js?:11404:38)
at debugCheckDirectivesFn (webpack:///./node_modules/@angular/core/fesm5/core.js?:11364:13)
at Object.eval [as updateDirectives] (ng:///DynamicTestModule/CheckoutRegisterPageComponent.ngfactory.js:70:5)
at Object.debugUpdateDirectives [as updateDirectives] (webpack:///./node_modules/@angular/core/fesm5/core.js?:11356:21)
at checkAndUpdateView (webpack:///./node_modules/@angular/core/fesm5/core.js?:10753:14)
at callViewAction (webpack:///./node_modules/@angular/core/fesm5/core.js?:10994:21)
HeadlessChrome 70.0.3508 (Linux 0.0.0): Executed 1 of 130 (1 FAILED) (0 secs / 0.791 secs)
With sourceMap: true:
HeadlessChrome 70.0.3508 (Linux 0.0.0) CheckoutRegisterPageComponent should create FAILED
[object ErrorEvent] thrown
HeadlessChrome 70.0.3508 (Linux 0.0.0): Executed 1 of 130 (1 FAILED) (0 secs / 0.883 secs)
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"
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 think https://github.com/angular/angular-cli/pull/11967 solves this issue. Looking at collaborators who might be familiar with the code, maybe @qiyigg can take a look?
@hansl will this be making it into the ng7 release?
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
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. 😞