There is a known bug when using Babel with Istanbul (NYC) that when you run coverage it displays coverage problems with
1 | else path not taken |
For example, in my case, I had:
1 import sequelize from Sequelize
and running coverage result with:
1 else path not taken
It usually happens if you have in your babel.config.json the following:
1
2 "sourceMaps": true,
"retainLines": true
and when you remove them the coverage jumps back with no coverage issues?
Solutions
I found few non helpful solutions for this case:
- To remove the sourceMaps and retainLines (which I didn’t wanted).
- To use
or1/* istanbul ignore next */which didn’t worked1/* istanbul ignore else */
- To add
to Babel configuration which didn’t worked for me.1auxiliaryCommentBefore: ' istanbul ignore next '
The solution I found to be the best is to use the babel plugin:
babel-plugin-istanbul
install it using
1 | npm i --save-dev babel-plugin-istanbul |
Then, add the following to your
1 | babel.config.json |
1
2
3
4
5
6
7 "env": {
"test": {
"plugins": [
"istanbul"
]
}
},
and last, update your package.json script to run coverage with BABEL_ENV variable set to
1 | test |
1
2 "test": "mocha",
"coverage": "cross-env BABEL_ENV=test nyc npm run test -s",
And now the coverage working perfectly with sourceMaps and retainLines!!!
Development Specialist, Artist and Activist
Personal Website