Bug: Code Coverage Report
- [x] bug report -> please search issues before submitting
- [ ] feature request
Versions.
$ ng --version @angular/cli: 1.0.0-rc.2 node: 6.9.5 os: win32 x64 @angular/common: 4.0.0-rc.5 @angular/compiler: 4.0.0-rc.5 @angular/core: 4.0.0-rc.5 @angular/forms: 4.0.0-rc.5 @angular/http: 4.0.0-rc.5 @angular/platform-browser: 4.0.0-rc.5 @angular/platform-browser-dynamic: 4.0.0-rc.5 @angular/router: 4.0.0-rc.5 @angular/cli: 1.0.0-rc.2 @angular/compiler-cli: 4.0.0-rc.5
Repro steps.
I have made a sample project, to reproduce this weird behaviour.
Please take a look at: https://github.com/Loster102/angular-coverage-sample
When I run 'ng test -cc true', there is a successful testrun.
But when I look at the coverage report, the branch coverage is really strange.
It says, that the constructor is not fully covered.
Desired functionality.
I would have expected that even the constructor is fully covered.
Mention any other details that might be useful.
In my real world application, there are some further strange behaviours, exactly like the one above. I try to reproduce these in the sample project. In this application, I'm using [email protected] and @angular/[email protected].
Same issues with Angular 5.2.11, Jest 23.1.0 and jest-preset-angular 5.2.2. All the istanbul packages pulled in (inspected via package-lock.json) also appear to be the latest.
"istanbul-api": "^1.3.1",
"istanbul-lib-coverage": "^1.2.0",
"istanbul-lib-instrument": "^1.10.1",
"istanbul-lib-source-maps": "^1.2.4",
I even tried upgrading to TypeScript 2.9.2 but no luck.
Why is this issue closed?!
The problem still remains, here is an example of an empty component:

How can I fix this? Thanks.
We fixed this by modifying angular.json to add the sourceMap flag:
The flag should be added under the following path: projects.my-app.architect.test.sourceMap
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"karmaConfig": "./karma.conf.js",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.spec.json",
"scripts": [],
"styles": [
"src/styles.css"
],
"assets": [
"src/assets",
"src/favicon.ico"
],
"sourceMap": true // <-- Add this flag!!
}
},
Angular CLI team reported that newer CLI versions will correct this automatically: https://github.com/angular/angular-cli/issues/13093
Angular CLI team lies. I'm running the latest angular/jest and still seeing this problem. NONE of the above "solutions" work. I've tried /* istanbul ignore next */, sourcemaps (both on and off) setting the target to es6, even adding the deprecated "mapcoverage" setting. Nothing works. Problem still exists without so much as a workaround. This is not a "closed" bug.
Angular CLI team lies. I'm running the latest angular/jest and still seeing this problem. NONE of the above "solutions" work. I've tried /* istanbul ignore next */, sourcemaps (both on and off) setting the target to es6, even adding the deprecated "mapcoverage" setting. Nothing works. Problem still exists without so much as a workaround. This is not a "closed" bug.
Before calling a team as liars consider for a moment that you might be missing something or maybe you have encountered an edge case unknown to the community. If you provide a minimum reproducible example (created in stackblitz for instance) it would be more helpful to get your problem sorted out.
Also this issue was closed ages ago - a lot has happend to the CLI since. A regression might have happened in the meantime which, if confirmed, is a reason to reopen it.
@Javin007 bro... come on. These are some of the most capable people working on software today, so the chances that they are lying to cover up a bug that, at its core, leads to an incorrect coverage report number are almost 0.
I'm using the latest CLI and Jest and ignore next works just fine. I would guess there is something else wrong with your setup.
Please consider the tone you take when commenting on projects which are free for you to use. No one owes you a fix, we're all in this together, and people are doing their best.
@mcblum @ruimarques
I'm sorry if I come off as a bit irritated, but this is a very easily duplicated bug:
1.) Create new "hello world" project. 2.) Install Jest. 3.) Inject HttpClient into the app.component.ts 4.) Run tests with code coverage.
I JUST did it again to verify. It literally happens every time. On multiple versions of angular, and on every environment. And I've spent the last 48 hours searching for a solution, and have tried every single "fix" found on google to no avail. And this has been a problem for quite some time.
I'm not even saying it's specifically the Angular team's fault. It could very likely be a problem with Jest. However, claiming that "it's fixed" when it's not, and it's so easily duplicated is not helpful. Especially when I see numerous posts on numerous different sites (and even in this thread) claiming so without doing the very basic test outlined above. This original post was from March of 2017. It's now February of 2019, and this thread is a history of people continuing to have the exact same problem without a solution at any time. So this isn't just some problem that "cropped up again."
And yes, I'm irritated at spending two days of seeing people saying "it's fixed" when it simply isn't, and none of the MANY, MANY proffered solutions (everything from creating an entirely new project, to reinstalling angular) are anything but a waste of time. This kind of hand-waving away of the problem doesn't get it solved (obviously, since we're now on year two of the same issue).
If you actually have a solution that fixes the problem, then I and the many other people suffering from this problem would very much love to hear it. If you're not having this issue, then what is it that you're doing differently from the above steps? We would very much like to know.
@Javin007 since even the steps you list above could result in a different version being installed on my machine than yours, could you please put the exact configuration in a StackBlitz so one of us can grab the code and see what's up? When you say "install jest" there are 1,000 different options, stable vs. alpha version of jest-preset-angular and a bunch of other things that can change, so put your exact configuration in a SB and we can see how you're getting the issue you're getting.
Once again, I'm currently, as I type, working on a large project using Angular CLI + Jest + Istanbul and all of my ignore statements work correctly.
I will drop one cent here:
- first of all, the issue with coverage constructor when using jest with Angular CLI isn't in Angular CLI fix because Angular CLI only fixed for karma + Jasmine.
- jest-preset-angular 7.0.0-alpha.1 fixed the issue with coverage when using jest. However we are trying to make jest-preset-angular to be compatible with jest 24 at the same time. Being compatible with jest 24 + alpha version work are 2 big breaking changes and require some times to release an official version.
FWIW: Here's a project that has the problem: https://yqnigwvz.github.stackblitz.io
@Javin007 could you try with the following:
"jest": "23.6.0",
"jest-preset-angular": "7.0.0-alpha.2",
You will need to change your jest config when you update to the alpha of jest-preset-angular. It looks something like this:
"jest": {
"preset": "jest-preset-angular",
"setupFiles": [
"jest-canvas-mock"
],
"setupTestFrameworkScriptFile": "<rootDir>/config/jest/config.ts",
"transformIgnorePatterns": [
"<rootDir>/node_modules/(?!@ngrx|@agm)"
],
"transform": {
"^.+\\.js": "<rootDir>/node_modules/babel-jest",
"^.+\\.(ts|html)$": "ts-jest"
},
"globals": {
"ts-jest": {
"tsConfig": "<rootDir>/tsconfig.spec.json",
"diagnostics": false,
"stringifyContentPathRegex": "\\.html$",
"astTransformers": [
"jest-preset-angular/InlineHtmlStripStylesTransformer.js"
]
},
"window": true
},
"modulePathIgnorePatterns": [
"<rootDir>/coverage/",
"<rootDir>/.storybook/",
"<rootDir>/config/",
"<rootDir>/dist/",
"<rootDir>/e2e/",
"<rootDir>/node_modules/"
],
"collectCoverageFrom": [
"{apps|libs}/**/src/**/*.ts"
],
"coverageThreshold": {
"global": {
"statements": 90,
"branches": 90,
"functions": 90,
"lines": 90
}
},
"testMatch": [
"**/__tests__/**/*.ts?(x)",
"**/?(*.)+(spec).ts?(x)"
],
"snapshotSerializers": [
"jest-preset-angular/AngularSnapshotSerializer.js",
"jest-preset-angular/HTMLCommentSerializer.js"
]
}
Obviously you'll need to change everything that's different, this is just from my project. Upgrading to the alpha version and sticking with Jest 23 for now should fix it.
It does indeed seem to be working now. So it would seem it's the fix is indeed in the Jest alpha. Thanks for your help!
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.