伊斯坦布尔分支机构覆盖范围是ES6,应该是100%,但只有75%

时间:2016-09-12 09:00:13

标签: istanbul es6-class

我写了一个非常简单的类和一些单元测试。覆盖率报告应该是100%,但我看到75%的分支机构。

enter image description here

我无法弄清楚如何达到100%以及我应该在哪里理解我所缺少的东西。

更新

单元测试:

/* global describe jest it expect */

import GenericDice from '../generic-dice-vanilla';

jest.unmock('../generic-dice-vanilla');

describe('GenericDice', () => {
  it('exists.', () => {
    expect(GenericDice).toBeDefined();
  });

  it('has a default face property set to 1', () => {
    const dice = new GenericDice();

    expect(dice.face).toBe(1);
  });

  it('has a default rolling property set to true', () => {
    const dice = new GenericDice();

    expect(dice.rolling).toBe(true);
  });

  it('has a default animation property set to an empty string', () => {
    const dice = new GenericDice();

    expect(dice.animation).toBe('');
  });

  it('outputs something when the render function is called', () => {
    const dice = new GenericDice();
    const result = dice.render();

    expect(result).toBeDefined();
  });
});

我使用Babel.js将此代码从ES6转换为ES5。

要运行单元测试,我使用以下命令:

jest ./src/ -u

所有代码都可以在Github上找到:https://github.com/gyroscopico/generic-dice/tree/feature/35-vanilla

2 个答案:

答案 0 :(得分:1)

它与您正在使用的Jest版本以及库用于收集覆盖率的方式相关,如果您按照以下步骤操作,您将找到一个练习示例:

  1. 克隆此回购https://github.com/a-c-m/react-jest-coverage-test.git
  2. 运行" npm test"
  3. 查看覆盖范围不是100%
  4. 使用此命令强制使用最新版本的jest和babel
  5.   

    rm -rf node_modules / jest; npm install jest @ test babel-jest @ test   multimatch istanbul-lib-instrument; npm测试

    1. 现在看到的覆盖范围是100%
    2. 希望这可以帮助您更新配置以获得完整的覆盖率。

答案 1 :(得分:0)

可能:

当您将ES6转换为ES5时,转换器有时会添加所谓的辅助代码,这可能会导致覆盖范围缩小。例如,在typescript中这段代码:

constructor( x: number, y: number, w: number, h: number ) {
    super(); // <- Throws 'Branch not covered'.
    this.rect = new Rect( x, y, w, h);
}

被描述为:

var _this = _super.call(this) || this;

导致'分支未被覆盖'。

使用babel,只需将auxiliaryCommentBefore: ' istanbul ignore next '添加到您的配置(docs)。

有关详情,请参阅此GitHub issue