angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Bug: Code Coverage Report

Open niklas-wortmann opened this issue 9 years ago • 74 comments

- [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. sample

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].

niklas-wortmann avatar Mar 20 '17 20:03 niklas-wortmann

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.

hotforfeature avatar Mar 21 '17 14:03 hotforfeature

If there's something we can do to help, we'd like to.

filipesilva avatar Mar 23 '17 21:03 filipesilva

@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.

stratus1011 avatar Mar 24 '17 20:03 stratus1011

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?

sandman21dan avatar Mar 28 '17 09:03 sandman21dan

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 ?

cpa-level-it avatar Apr 12 '17 07:04 cpa-level-it

Any solutions? It is really frustrating :/

raguilera82 avatar May 02 '17 00:05 raguilera82

Since it is being 2 months, just want to know the progress. Is there any ETA for this issue resolution?

SURENDARREDDY avatar May 19 '17 18:05 SURENDARREDDY

Changing target to es6 in the main tsconfig.json doesn't work either

cortopy avatar May 29 '17 15:05 cortopy

Same problem here.

rdsedmundo avatar Jun 14 '17 14:06 rdsedmundo

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.

phillipzada avatar Jun 23 '17 03:06 phillipzada

Changing to es6 doesn't work here as well. Is there a feasible solution in the works?

dinvlad avatar Jun 25 '17 01:06 dinvlad

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

screen shot 2017-07-02 at 9 20 11 am

rg1220 avatar Jul 02 '17 13:07 rg1220

I have the same issue :( ... Why is it blocked? Can we expect a fix in the near future?

brunolm avatar Aug 18 '17 17:08 brunolm

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 avatar Aug 23 '17 18:08 brunolm

@brunolm This workaround works if you don't use decorator on attribute in your code.

image

If I remove the @Effect I have 100% coverage... do you have an idea ?

davinkevin avatar Aug 31 '17 03:08 davinkevin

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),

filipesilva avatar Aug 31 '17 16:08 filipesilva

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).

UnkJester avatar Oct 04 '17 15:10 UnkJester

@filipesilva does #7610 now resolve this in the sense that we can provide es2015 for code coverage to run and it should work?

kenisteward avatar Oct 21 '17 18:10 kenisteward

@brunolm , it works but /* istanbul ignore next */ what does this hack do exactly?

webcat12345 avatar Oct 23 '17 18:10 webcat12345

@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.

brunolm avatar Oct 23 '17 19:10 brunolm