Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Istanbul branch coverage is ES6 class should be 100% but is only 75%

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

like image 943
Thomas Amar Avatar asked Sep 12 '16 09:09

Thomas Amar


People also ask

How do I get 100 branch coverage?

Therefore, to achieve 100% Statement coverage we have to make sure that every statement in the code is executed at least once. So we will need test case(s) executed in such a way that every statement of the code is executed at least once during the test execution.

How do I ignore branch coverage for missing else?

You can use /* istanbul ignore else*/ just before the if statement to ignore the missing else . Save this answer. Show activity on this post. You can use /* istanbul ignore else */ to tell istanbul not to include that in the coverage.


1 Answers

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.

like image 127
damianfabian Avatar answered Oct 31 '22 11:10

damianfabian