Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code coverage for directive parameters is wrong #72

Closed
bashirsouid opened this issue Jul 7, 2017 · 6 comments
Closed

Code coverage for directive parameters is wrong #72

bashirsouid opened this issue Jul 7, 2017 · 6 comments

Comments

@bashirsouid
Copy link

bashirsouid commented Jul 7, 2017

We are also having a similar issue as #70 when using istanbul for code coverage in our Angular2 application that uses the standard @input decorator.

import { Component, Input } from '@angular/core';
import { PluginOptions } from '../../../plugin-contracts';
import { Template } from '../../../plugin-contracts';
 
@Component({
    selector: 'angular-component',
    templateUrl: './angular-component.component.html',
    styleUrls: ['./angular-component.component.scss'],
})
export class AngularComponent {
    /* istanbul ignore next */
    @Input()
    pluginOptions: PluginOptions;
 
    /* istanbul ignore next */
    @Input()
    template: Template;
}

Here is the typedef for the @input decorator:

/**
 * Type of the Input metadata.
 *
 * @stable
 */
export interface Input {
    /**
     * Name used when instantiating a component in the template.
     */
    bindingPropertyName?: string;
}

The code coverage shows up as missing branches in the @input decorator which is just an interface. Is it because it is an optional property? There is no way to directly cover this.

Any ideas?

@bcoe
Copy link
Member

bcoe commented Jul 22, 2017

@bashirsouid we would need to dig into the transpiled code that gets generated by:

    /* istanbul ignore next */
    @Input()
    pluginOptions: PluginOptions;

depending on what the expanded code looks like, we might be able to tweak the instrumenter slightly to give coverage; does the ignore directive work as expected?

@Enderer
Copy link

Enderer commented Jul 31, 2017

@bcoe I'm getting the same issue on @Input() properties. Adding the ignore directive doesn't have an effect

image

@gernsdorfer
Copy link

gernsdorfer commented Sep 4, 2017

For this problem I created a simple demo repository:
https://github.com/gernsdorfer/coverage-bug

External Interface's for Input's reduce the code coverage.

A workaround is to embed the interface:

@Input() test: ExternalBar; // coverage not working
@Input() testWorks: { externalBar: ExternalBar }; // // coverage work's

@troythacker
Copy link

I'm running into the same issue with @ViewChild

screenshot 2017-09-20 06 51 44

In the transpiled javascript, there seems to be two parts that are created for the decorators:

At the very top of the file:

"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};

and after the component definition:

var SearchComponent = (function () {
    function SearchComponent(renderer) {
        this.renderer = renderer;
    }
   // Rest of prototype declarations here
   return SearchComponent;
}());
__decorate([
    core_1.ViewChild('searchContainer'),
    __metadata("design:type", core_1.ElementRef)
], SearchComponent.prototype, "searchContainer", void 0);
__decorate([
    core_1.ViewChild('searchInput'),
    __metadata("design:type", core_1.ElementRef)
], SearchComponent.prototype, "searchBox", void 0);

@bcoe
Copy link
Member

bcoe commented Nov 23, 2017

@troythacker @bashirsouid @Enderer give babel-plugin-istanbul a shot, which can be used to instrument code as part of the transpilation process, which can provide more accurate coverage results for ES-next features.

Unfortunately, until some of these language features have landed in v8 itself, I can't promise we'll be able to find a workaround to get accurate coverage.

@maddy619
Copy link

maddy619 commented May 5, 2020

Please try setting sourceMap to true in test configuration of angular.json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants