istanbul
istanbul copied to clipboard
calling method on super: branch not covered
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:
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](https://cloud.githubusercontent.com/assets/9574149/21007248/06d9841c-bd34-11e6-8d93-0b5570b9cff4.png)
But seems fine with:
![screen shot 2016-12-08 at 10 46 13](https://cloud.githubusercontent.com/assets/9574149/21007280/245b4836-bd34-11e6-9041-bb181a74e335.png)
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](https://cloud.githubusercontent.com/assets/9574149/21008172/682ed92a-bd38-11e6-95d9-d3a44b379650.png)
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](https://cloud.githubusercontent.com/assets/9574149/21008180/6f61aa7e-bd38-11e6-897a-038b15d755c7.png)
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 <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>
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?