Istanbul: calling method on super: branch not covered

Created on 28 Aug 2016  路  37Comments  路  Source: gotwarlost/istanbul

export default class MyComponent extends Component {
    link(name, Constructor) {
        super.link(name, Constructor);
    }
}
class AnotherComponent extends Component {}

describe("MyComponent", () => {
    it("should be covered", () => {
        const myComponent = new MyComponent();
        myComponent.link("anotherComponent", AnotherComponent);
    });
});

This results in:
screen shot 2016-08-28 at 1 09 32 pm

Most helpful comment

Any news about this?

All 37 comments

I can workaround to get 100% coverage by doing the following:

export default class MyComponent extends Component {
    constructor() {
        super();
        this.superLink = super.link();
    }
    link(name, Constructor) {
        this.superLink(name, Constructor);
    }
}
class AnotherComponent extends Component {}

describe("MyComponent", () => {
    it("should be covered", () => {
        const myComponent = new MyComponent();
        myComponent.link("anotherComponent", AnotherComponent);
    });
});

this is reproducible for me on any call to super or one of its properties with at least one argument

is reproducible for me as well v 1.1.0-alpha.1. Marks opening bracket of each constructor where i call super as not covered branch

Just called super in a constructor and got an uncovered branch 馃槥

Having this problem too. I have to use the /* istanbul ignore next */ hack way more than I'd like to.

I've just got

import React, { Component } from 'react'
export default class Main extends Component {
    constructor(props) {
        super(props)
        this.state = {
            counter: 0
        }
    }
    render() {
        {this.state.counter}
    }
}

And I get a notice that my constructor branch is not being covered and so then a less than 100% score on my branches.

Is this related?

@bitttttten

That is the issue we're all complaining about, I believe.

any news on a fix for this? I'm running into the same problem. Can't get my coverage to 100% as a result. :/

not as far as I know, just put down your expected coverage to what you get with this issue. that's what I am doing.

I also did not find workaround for this. Thus I moved to nyc(which uses istanbul under cover) with babel and now everything is calculated correctly.

@dobryanskyy @tconroy

Another option is to put /* istanbul ignore next */ right above your constructor and istanbul won't bug you about coverage for your constructor

Found the same issue, what is exactly generating this problem ? Some ES6 compatibility ? I'm working with React and that's a very common airbnb pattern.

Also came up against this and found that declaring static properties above the constructor causes the /* istanbul ignore next */ statement to not work:

screen shot 2016-12-08 at 10 45 44

But seems fine with:

screen shot 2016-12-08 at 10 46 13

Found a workaround for the above problem I posted that doesn't anger the AirBnB lint gods (i.e. the react/sort-comp rule):

screen shot 2016-12-08 at 11 18 44

It seems that adding the /* istanbul ignore next */ to before the statement that immediately follows the call to super fixes it:

screen shot 2016-12-08 at 11 17 32

Nice find @owenDods !

I remember also running into that issue and just scrapping the static method entirely.

Yeah @brandonsturgeon, I almost did the same. But I didn't want to alter my code because of a coverage bug! It was a matter of principle!

I've been using 1.1.0-alpha.1 for a while, and saw this for the first time this morning after fresh npm install.

On another machine, where 1.1.0-alpha.1 was installed quite some time ago, this issue doesn't occur.

OK, some more details:

This doesn't happen with [email protected], but it does with [email protected]

For this code:

    constructor( x: number, y: number, w: number, h: number ) {
        super(); // Branch not covered.
        this.rect = new Rect( arguments[ 0 ], arguments[ 1 ], arguments[ 2 ], arguments[ 3 ] );
    }

In 2.1.4 super() emits (clearly this will throw Branch not covered):

        var _this = _super.call(this) || this;

Whereas 2.0.3 emits:

    _super.call(this);

So it seems that this is caused by a change to how typescript emits.

I believe that this one is for typescript to fix.

Cast your vote and opinions: https://github.com/Microsoft/TypeScript/issues/13455

I'm now convinced that it is best to simply stick to es6 when producing the coverage report - the emitted auxiliary code only reduces coverage with an es5 target. Here is an example how.

Related:
https://github.com/Microsoft/TypeScript/issues/13455

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.

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.

I got the same issue as
{this.state.isDeleteVisible ?
: null}

For the one using webpack and TypeScript configured to output ES5,
I made a little webpack loader:
https://www.npmjs.com/package/ts-es5-istanbul-coverage
Using it make sure you won't get the branch marked as not covered.

Got the same issue. Workaround works.
@davinkevin, @neonox31

I ran into the same problem running React with babel, webpack and karma. What solved it was switching from istanbul-instrumenter-loader to babel-plugin-istanbul

By installing babel-plugin-istanbul solved for me

Using /* istanbul ignore next */ worked for me.

The bug is still there in version 0.4.5. Are there any news? @gotwarlost can you please have a look?

Ignoring it works, but that doesn't really _solve_ the problem.
Still getting this bug when using ts-node with mocha and targeting ES5

Any news about this?

Just ran into this and found that removing the semicolon after
super(); // <-- this one

raised coverage back to 100% ... at least until prettier put it back :/

You can replace this: super();
With this: super()/* istanbul ignore next */;

I can confirm this is still an issue

This is still an issue..!

I need a workaround

image

I can workaround to get 100% coverage by doing the following:

export default class MyComponent extends Component {
    constructor() {
        super();
        this.superLink = super.link();
    }
    link(name, Constructor) {
        this.superLink(name, Constructor);
    }
}
class AnotherComponent extends Component {}

describe("MyComponent", () => {
    it("should be covered", () => {
        const myComponent = new MyComponent();
        myComponent.link("anotherComponent", AnotherComponent);
    });
});

you are moving problem from that line to constructor

I can workaround to get 100% coverage by doing the following:

export default class MyComponent extends Component {
    constructor() {
        super();
        this.superLink = super.link();
    }
    link(name, Constructor) {
        this.superLink(name, Constructor);
    }
}
class AnotherComponent extends Component {}

describe("MyComponent", () => {
    it("should be covered", () => {
        const myComponent = new MyComponent();
        myComponent.link("anotherComponent", AnotherComponent);
    });
});

you are moving problem from that line to constructor

thats good but what if I don`t want to write superlink, or just imagine if I have 50 components

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ouhouhsami picture ouhouhsami  路  16Comments

schulzch picture schulzch  路  151Comments

luisonthekeyboard picture luisonthekeyboard  路  16Comments

gbahamondezc picture gbahamondezc  路  24Comments

NiGhTTraX picture NiGhTTraX  路  36Comments