11

I have written a very simple class and some unit tests. The coverage report should be 100% but I see 75% for branches.

enter image description here

I can't figure out how to get to 100% and where I should be looking to understand what I'm missing.

UPDATE

Unit tests:

/* 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();
  });
});

I'm using Babel.js to transpile this code from ES6 into ES5.

To run the unit tests, I use the following command:

jest ./src/ -u

All the code can be found on Github: https://github.com/gyroscopico/generic-dice/tree/feature/35-vanilla

Thomas Amar
  • 352
  • 10
  • 20
  • 2
    Knowing what unit tests you run is needed to answer your question. Could you add these to your question ? – Philippe Sep 21 '16 at 03:32
  • Are you using a converter like Babel to convert your code to es5? ? If you are, branches may be added in the generated code. – Frank Bessou Sep 21 '16 at 13:44
  • Can you give instructions on how to reproduce the problem? Steps on how you run babel.js (including any options you pass it) and run istanbul would be helpful. – Tim Nov 15 '16 at 18:57
  • Did it ever cover 100%? if so, @Frank Bessou is probably right. Compilers generate stuff under the hood that can only be seen if the generated/executed instructions are inspected – bichito Jan 14 '17 at 00:31
  • Just wondering whether you've already looked into this: http://blog.dmbcllc.com/es2015-code-coverage-and-jest-react-js-unit-testing/ I tried their solution and it worked, but it's ugly. – Nelson Yeung Jan 17 '17 at 00:57

2 Answers2

1

It's related with the version of Jest that you are using and also the way the libraries using to collect the coverage, you will find a practice example if you follow this steps:

  1. Clone this repo https://github.com/a-c-m/react-jest-coverage-test.git
  2. Run "npm test"
  3. See the coverage is not 100%
  4. force the latest version of jest and babel with this command

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

  1. See the coverage now is 100%

Hope this will help you to update your configuration to get a full coverage.

damianfabian
  • 1,681
  • 12
  • 16
0

Possibly:

When you transpile ES6 to ES5, the transpiler sometimes add so-called auxiliary code, which may result in reduced coverage. For example, in typescript this code:

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

is transpiled to this:

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

resulting in 'Branch not covered'.

With babel, just add auxiliaryCommentBefore: ' istanbul ignore next ' to your config (docs).

See this GitHub issue for more.

agilgur5
  • 667
  • 12
  • 30
Izhaki
  • 23,372
  • 9
  • 69
  • 107