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].
There are two related issues both in Istanbul and TypeScript: https://github.com/gotwarlost/istanbul/issues/690 https://github.com/Microsoft/TypeScript/issues/13455
The latest suggestion from both threads is to run tests with coverage targeting ES6 instead of ES5. Unfortunately from what I can tell, the Angular CLI doesn't use tsconfig.json when running tests, and the suggestions of using --compilerOptions '{\"target\":\"es6\"}' won't work since Angular CLI doesn't recognize that.
At the moment, there's nothing to do, adding /* istanbul ignore next */ comments everywhere around the constructor won't help. We're deadlocked while waiting for Angular, Istanbul, or TypeScript to change something.
If there's something we can do to help, we'd like to.
@hotforfeature you can actually change the "target" to es6 in the src/tsconfig.spec.json with the version of angular cli that you are using. However, if you are running tests on phantomjs it will fail due to the "const" keyword/ES6/ES2015 not being supported yet. There is a beta version of phantomjs that they have released that you could try which should have support for it, but may have other issues.
tsconfig.spec file has es6 as a target, running on CLI-1.0.0 still get all the missing branches from all the constructors, maybe I'm missing something?
I have the same problem as @sandman21dan. Changed target from es5 to es6 in tsconfig.spec.json and constructor branches are still not covered.
Any idea ?
Any solutions? It is really frustrating :/
Since it is being 2 months, just want to know the progress. Is there any ETA for this issue resolution?
Changing target to es6 in the main tsconfig.json doesn't work either
Same problem here.
Anyone got anything on this issue resolution?
I found an ugly workaround. Set the constructor params to @Inject(TYPE) private variable: any resolves it but then you loose intellisence and type-checking.
Changing to es6 doesn't work here as well. Is there a feasible solution in the works?
Reproduced this issue by simply adding http: HTTP to the constructor of a newly created project created with angular-cli: https://github.com/rg1220/angular-cli-coverage-issue-5526

I have the same issue :( ... Why is it blocked? Can we expect a fix in the near future?
Workaround
As a workaround I found the special comment /* istanbul ignore next */
If you place after your class, the constructor gets ignored. Has to be exactly after the brace, do not break lines.
Example
export class InternalComponent {
constructor(private authService: any) {
}
} /* istanbul ignore next */
Generates
define(["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var InternalComponent = (function () {
function InternalComponent(authService) {
this.authService = authService;
}
return InternalComponent;
}()); /* istanbul ignore next */
exports.InternalComponent = InternalComponent;
});
Which ignores this line exports.InternalComponent = InternalComponent; that causes the issue.
Note: If you have static members in your class add the comment after the last one, example:
static readonly authKey = 'access_token'; /* istanbul ignore next */
To ignore super
super() /* istanbul ignore next */;
If you have generics and/or @Inject in your constructor params, try to break lines.
With those hacks I got 100% coverage.
UPDATE: Well, yes, this is pretty ugly and might not work in all cases.
@brunolm This workaround works if you don't use decorator on attribute in your code.

If I remove the @Effect I have 100% coverage... do you have an idea ?
Heya, just wanted to mention that I added the blocked tag because I spent some time trying to fix it and as far as I can tell this is a limitation of the code coverage tools right now. @hotforfeature's comment (https://github.com/angular/angular-cli/issues/5526#issuecomment-288095948) explains the current situation well, I couldn't find any further developments.
Using an es6 target is not a real solution per se either, overall the CLI doesn't really support outputting es2015 (see https://github.com/angular/angular-cli/issues/2907),
Is there any other update? Has this been raised with the code coverage tools support? (I'm a bit new to this) I'm getting the issues @davinkevin mentioned, which breaks the only known workaround (other than lowering the branch requirement, which I'm being told is unsavory).
@filipesilva does #7610 now resolve this in the sense that we can provide es2015 for code coverage to run and it should work?
@brunolm , it works but /* istanbul ignore next */ what does this hack do exactly?
@webcat12345 /* istanbul ignore next */ ignores the line bellow in the coverage. https://github.com/gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md
Following my instructions it will place the ignore statement exactly at a part of the generated code that is not being accessed.
It is a really ugly workaround and might not work in all cases.