我为伊斯坦布尔抱怨缺少分支机构覆盖范围的测试中到底缺少了什么?

时间:2019-04-24 13:19:08

标签: angular typescript istanbul

在我的一生中,我无法弄清伊斯坦布尔在抱怨什么。我在此文件中发现了一个分支。这是有问题的组件的相关部分(我去除了无关的位以避免产生噪声):

export class TextComponent {
    value: string;
    _field: FieldModel;
    private _record: any;

    @Input()
    set field(field: FieldModel) {
        this._field = field;
        this.cacheValue();
    }

    @Input()
    set record(record: any) {
        this._record = record;
        this.cacheValue();
    }

    cacheValue() {
        this.value = this.getValue();
    }

    getValue(): string {
        if (!this._record || !this._field) {
            return '';
        }

        return this._field.name in this._record ? this._record[this._field.name] : '';
    }
}

和样本测试:

    it('should store the record value', () => {
        component.record = {
            first: "Nancy",
            last: "Sue",
        };
        expect(component.value).toBe('');

        component.field = {name: 'first'};
        expect(component.value).toBe('Nancy');
    });

我的代码覆盖率是100%,但缺少一个分支。这是代码覆盖率报告显示的内容:

enter image description here

我肯定知道黄色的亮点是伊斯坦布尔认为没有覆盖的分支。但是,我不知道它实际上在抱怨什么,因此我不知道如何编写测试来涵盖它。我包括的样本测试是对此功能的主要测试。显然,它在某种程度上是有缺陷的,但是我不知道该以哪种方式,而且那一点亮点也不是很有用。

Angular 7.1.0,打字稿3.1.6

1 个答案:

答案 0 :(得分:1)

看起来像是open issue for istanbul。似乎以这种方式使用另一个类可能会引起一些问题。该线程中的一种潜在解决方法是为该方法指定一个类似OPTIONS_SELECTION的返回类型。可能是TS转换成JavaScript的问题所在。 javascript可能有一个分支,您的代码没有。