Fix NYC Istanbul 'Else Path Not Taken' from node_modules Imports

Fix NYC Istanbul 'Else Path Not Taken' from node_modules Imports

There is a known bug when using Babel with Istanbul (NYC) that when you run coverage it displays coverage problems with else path not taken for imports.

For example, in my case, I had:

import sequelize from Sequelize

and running coverage result with:

else path not taken

It usually happens if you have in your babel.config.json the following:

  "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:

  1. To remove the sourceMaps and retainLines (which I didn’t wanted).
  2. To use /* istanbul ignore next */ or /* istanbul ignore else */ which didn’t worked
  3. To add auxiliaryCommentBefore: ' istanbul ignore next ' to Babel configuration which didn’t worked for me.

The solution I found to be the best is to use the babel plugin:

babel-plugin-istanbul

install it using npm i --save-dev babel-plugin-istanbul.

Then, add the following to your babel.config.json:

  "env": {
    "test": {
      "plugins": [
        "istanbul"
      ]
    }
  },

and last, update your package.json script to run coverage with BABEL_ENV variable set to test. I used cross-env for multi-OS support (Windows/Linux/Mac)

"test": "mocha",
"coverage": "cross-env BABEL_ENV=test nyc npm run test -s",

And now the coverage working perfectly with sourceMaps and retainLines!!!