SOLVED | NYC Istanbul ‘else path not taken’ from import node_modules

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 imports.

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:

  1. To remove the sourceMaps and retainLines (which I didn’t wanted).
  2. To use
    1
    /* istanbul ignore next */
    or
    1
    /* istanbul ignore else */
    which didn’t worked
  3. To add
    1
    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

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
. I used cross-env for multi-OS support (Windows/Linux/Mac)


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!!!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.