istanbul
istanbul copied to clipboard
Statements and Branches never get to 100% coverage
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.
How can I do to get 100% coverage of statements and branches?
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
Tks @Katu360 , is there a way to solve that?
Jest uses istanbul on transformed code though, so it should cover those babel helpers as well?
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?
This is happening to me as well - via Jest coverage testing ES6 modules. Anything I can do to help?
Same here.
https://github.com/facebook/jest/issues/817 may be of interest here.
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"
}
}
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);
});
});
});