istanbul icon indicating copy to clipboard operation
istanbul copied to clipboard

Statements and Branches never get to 100% coverage

Open PedroPinheiro opened this issue 8 years ago • 9 comments

This issue is open here, as pointed by @cpojer at facebook/jest#676

I'm trying to get real % coverage, but statements and branches can't reach 100% even with all the file tested. Moreover, the first line always presents yellow and black squares.

image

How can I do to get 100% coverage of statements and branches?

PedroPinheiro avatar Feb 18 '16 14:02 PedroPinheiro

Jest used Istanbul to collect coverage info. Istanbul includes coverage by statements, lines, functions and branches in its report. For transformed code, there can be branches in auto generated code. For exapmle, if you use Babel to tranform your code from es6 syntax, there will be branches for import statement in transformed code. In this case, the coverage by branches will never go up to 100%.

https://gist.github.com/cyjia/7a6291a416182fb18988

Katu360 avatar Feb 20 '16 02:02 Katu360

Tks @Katu360 , is there a way to solve that?

PedroPinheiro avatar Feb 20 '16 22:02 PedroPinheiro

Jest uses istanbul on transformed code though, so it should cover those babel helpers as well?

cpojer avatar Feb 21 '16 00:02 cpojer

It seems like if a source map is provided to istanbul, it should only count coverage based on the lines in the original file, and completely ignore any transpiled / changed code. Is that a valid assumption?

quantizor avatar Feb 22 '16 22:02 quantizor

This is happening to me as well - via Jest coverage testing ES6 modules. Anything I can do to help?

hharnisc avatar Mar 08 '16 20:03 hharnisc

Same here.

massick avatar Apr 18 '16 21:04 massick

https://github.com/facebook/jest/issues/817 may be of interest here.

a-c-m avatar Aug 17 '16 13:08 a-c-m

Any news on this? I can't manage to get 100% coverage with ES6 code. Example here => https://github.com/damianobarbati/react-redux-test Config here =>

{
	"name": "react-redux-form",
	"version": "1.0.0",
	"author": "Damiano Barbati <[email protected]> (http://github.com/damianobarbati)",
	"scripts": {
		"jest": "node ./node_modules/jest/bin/jest.js",
		"test": "yarn jest",
		"test:watch": "yarn jest -- --watch --no-cache"
	},
	"babel": {
		"presets": [
			"babel-preset-latest",
			"babel-preset-stage-0",
			"babel-preset-react"
		],
		"plugins": [
			"transform-decorators-legacy"
		]
	},
	"jest": {
		"bail": true,
		"verbose": true,
		"collectCoverage": true
	},
	"dependencies": {
		"axios": "^0.16.1",
		"babel-jest": "^20.0.0",
		"babel-plugin-transform-decorators-legacy": "^1.3.4",
		"babel-preset-latest": "^6.24.1",
		"babel-preset-react": "^6.24.1",
		"babel-preset-stage-0": "^6.24.1",
		"enzyme": "^2.8.2",
		"enzyme-to-json": "^1.5.1",
		"jest": "^20.0.0",
		"lodash": "^4.17.4",
		"moxios": "^0.4.0",
		"react-dom": "^15.5.4",
		"react-redux": "^5.0.4",
		"redux": "^3.6.0",
		"redux-mock-store": "^1.2.3",
		"redux-promise": "^0.5.3",
		"redux-thunk": "^2.2.0"
	}
}

damianobarbati avatar May 08 '17 00:05 damianobarbati

I achieved 100% branch coverage once I checked the cases with default arguments. E.g. for the following function

import { fromJS } from 'immutable';

import { LOCATION_CHANGE } from 'react-router-redux';

const initialState = fromJS({
  location: null,
});

function routeReducer(state = initialState, action = {}) {
  switch (action.type) {
    case LOCATION_CHANGE:
      return state.merge({
        location: action.payload,
      });
    default:
      return state;
  }
}

export default routeReducer

It was enough to write 2 test cases (see below). In the first one the function is called without any arguments (the default ones are used). In the second test case custom arguments are passed.

import { fromJS } from 'immutable';
import { LOCATION_CHANGE } from 'react-router-redux';

import routeReducer from './reducer';

describe('route Reducer', () => {
  let state;
  const initialState = {
    location: null,
  };

  beforeEach(() => {
    state = fromJS(initialState);
  });

  describe('default', () => {
    it('returns the initial state', () => expect(routeReducer()).toEqual(state));
  });

  describe('on LOCATION_CHANGE', () => {
    it('sets the loading flag', () => {
      const location = 'new location';
      const expectedResult = state.set('location', location);
      expect(routeReducer(state, { type: LOCATION_CHANGE, payload: location })).toEqual(expectedResult);
    });
  });
});

alexander-elgin avatar Jul 17 '18 12:07 alexander-elgin