istanbul icon indicating copy to clipboard operation
istanbul copied to clipboard

calling method on super: branch not covered

Open dcrockwell opened this issue 7 years ago • 40 comments

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

dcrockwell avatar Aug 28 '16 19:08 dcrockwell

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);
    });
});

dcrockwell avatar Aug 28 '16 19:08 dcrockwell

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

yanneves avatar Aug 30 '16 13:08 yanneves

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

dobryanskyy avatar Sep 07 '16 08:09 dobryanskyy

Just called super in a constructor and got an uncovered branch 😞

noamokman avatar Oct 20 '16 09:10 noamokman

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

brandonsturgeon avatar Oct 21 '16 19:10 brandonsturgeon

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 avatar Oct 25 '16 20:10 bitttttten

@bitttttten

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

brandonsturgeon avatar Oct 26 '16 00:10 brandonsturgeon

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

tconroy avatar Nov 07 '16 22:11 tconroy

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.

bitttttten avatar Nov 07 '16 22:11 bitttttten

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 avatar Nov 07 '16 23:11 dobryanskyy

@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

brandonsturgeon avatar Nov 08 '16 02:11 brandonsturgeon

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.

Drasky-Vanderhoff avatar Dec 06 '16 20:12 Drasky-Vanderhoff

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

owenDods avatar Dec 08 '16 10:12 owenDods

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

owenDods avatar Dec 08 '16 11:12 owenDods

Nice find @owenDods !

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

brandonsturgeon avatar Dec 08 '16 17:12 brandonsturgeon

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!

owenDods avatar Dec 08 '16 18:12 owenDods

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.

Izhaki avatar Dec 12 '16 10:12 Izhaki

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.

Izhaki avatar Dec 18 '16 00:12 Izhaki

I believe that this one is for typescript to fix.

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

Izhaki avatar Mar 16 '17 00:03 Izhaki

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

Izhaki avatar Mar 20 '17 21:03 Izhaki

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.

brunolm avatar Aug 23 '17 20:08 brunolm

I got the same issue as <View style={styles.searchBarDeleteIconSection}> {this.state.isDeleteVisible ? <ImageButton name="search-bar-icon-delete" source={{ uri: SEARCH_ICON_DELETE_IPHONE }} style={styles.searchBarDeleteIcon} pressedStyle={styles.closeIconPressed} action={alert}/> : null} </View>

rtskmr avatar Oct 23 '17 07:10 rtskmr

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.

ben8p avatar Dec 13 '17 09:12 ben8p

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

Dji75 avatar Jan 05 '18 07:01 Dji75

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

jfrej avatar Jan 25 '18 16:01 jfrej

By installing babel-plugin-istanbul solved for me

rpresb avatar Mar 02 '18 10:03 rpresb

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

srihari-sridharan avatar Jun 20 '18 05:06 srihari-sridharan

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

FrancescoBorzi avatar Nov 13 '18 10:11 FrancescoBorzi

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

Mischala avatar Nov 20 '18 21:11 Mischala

Any news about this?

FrancescoBorzi avatar Jul 07 '19 18:07 FrancescoBorzi