jeffrifwald / babel-istanbul

Yet another JS code coverage tool that computes statement, line, function and branch coverage with module loader hooks to transparently add coverage when running tests. Supports all JS coverage use cases including unit tests, server side functional tests and browser tests. Built for scale.
Other
144 stars 23 forks source link

Question: hard time figuring out why abstract method with empty function body yields no coverage #59

Closed silkentrance closed 8 years ago

silkentrance commented 8 years ago

This happens with babel-istanbul@0.6.0

I have a class such as

class AbstractBase {
    /**
     * @abstract
     */
    meth() {}
}

When generating coverage data for the classes under test that derive from that abstract base, none of them will actually call upon super.meth() because it actually does nothing.

In the generated coverage report, the method is reported and decreases overall coverage.

And even using /* istanbul ignore next */ will not instruct istanbul to simply ignore the statement.

Am I doing something wrong here?

silkentrance commented 8 years ago

Ah, I think I found out why this happens. I am using babel 6.x here and there is some weird stuff going on with the comments when transpiling the sources.

silkentrance commented 8 years ago

I had to move the ignore comment to the top of the class declaration, sigh. This makes abstract classes with methods that actually do something very hard to generate proper coverage for or fail the build if the coverage for these methods is not being accounted for...

/* istanbul ignore next */
class AbstractBase {
    /**
     * @abstract
     */
    meth() {}
}
silkentrance commented 8 years ago

Hey, found a yet different solution, perhaps this one is worth noting down in the FAQ or documentation?

It also applies to normal methods that should not be considered when calculating the coverage

class AbstractBase {
    /**
     * @abstract
     */
    /* istanbul ignore next */ meth() {}
}

The comment must stand right next to the method's name. Otherwise the association will be lost in the transpiled code.

But alas, this only works for the first method so commented. The comment of a second method will be moved and can no longer be associated with the method.

  (0, _createClass3.default)(AbstractInjector, [{
    key: 'canInject',

    /**
     * Returns true whether this is able to handle the injection request.
     *
     * @abstract
     * @param {TargetType} target - the target object or function
     * @param {String} attr - the target's attribute
     * @param {DescriptorType} descriptor - the descriptor
     * @returns {Boolean} - true whether this can handle the request,
     * false otherwise
     */
    /*eslint no-unused-vars:0*/
    /* istanbul ignore next */value: function canInject(target, attr, descriptor) {}

    /**
     * Instructs this to inject according to the specified injection descriptor.
     *
     * @abstract
     * @param {InjectionDescriptor} injectionDescriptor - the injection descriptor
     * @throws {InjectionError}
     * @returns {void}
     */
    /*eslint no-unused-vars:0*/
    /* istanbul ignore next */
  }, {
    key: 'inject',
    value: function inject(injectionDescriptor) {}
  }]);

I suppose that this is a bug in the transpiler.

silkentrance commented 8 years ago

See https://phabricator.babeljs.io/T7189 for the babel issue generated from this.

silkentrance commented 8 years ago

While having to move the comment right in front the method identifier seems to be a bug in istanbul, sigh...

And here is the issue reported against istanbul https://github.com/gotwarlost/istanbul/issues/561

jeffrifwald commented 8 years ago

This ignore issue has been around for a long time:

https://github.com/gotwarlost/istanbul/issues/377

jeffrifwald commented 8 years ago

I can try updating the Istanbul version to see if that solves the ignore issue though. I'll ping you here when that is done.

silkentrance commented 8 years ago

@jmcriffey this would be great, thank you!

jeffrifwald commented 8 years ago

@silkentrance babel-istanbul version 0.7.0 is now using the latest version of istanbul. If that doesn't fix the ignore issues, unfortunately you'll have to put your comments in a spot that won't be missed by istanbul.

silkentrance commented 8 years ago

Thanks again for updating istanbul. It did not fix the issue as it is babel transpiler related but it might fix other issues :+1: