istanbuljs / babel-plugin-istanbul

A babel plugin that adds istanbul instrumentation to ES6 code
BSD 3-Clause "New" or "Revised" License
624 stars 73 forks source link

Getting zero coverage report after adding babel-plugin-istanbul #169

Closed jeff-bothe closed 6 years ago

jeff-bothe commented 6 years ago

I decided to install this plugin because nyc was falsely reporting uncovered branches on async arrow functions. But now, I am just getting a completely empty coverage report. Any help would be greatly appreciated! thanks!

environment specs: node 10.3.0 mocha 5.2.0 nyc 12.0.2 babel-plugin-istanbul 4.1.6 babel-plugin-module-resolver 3.1.1

I mentioned babel-plugin-module-resolver, cuz I was wondering if anyone knew of any known conflicts with that plugin?

My repo is pretty light on test configuration, so here is all of it:

package.json

"scripts": {
  "test": "nyc mocha --exit"
},
"nyc": {
  "sourceMap": false,
  "instrument": false
},

.babelrc

{
  "env": {
    "test": {
      "plugins": [ "istanbul" ]
    }
  },
  "presets": ["env", "es2015", "stage-0"], 
  "plugins": [
    ["module-resolver", {
      "root": ["./"]
    }]
  ]
}

mocha.opts

--recursive
--require babel-register
--require ./test/helpers
--timeout=3000

helpers.js

var supertest = require('supertest');
var chai = require('chai');

global.expect = chai.expect;
global.request = supertest;
coreyfarrell commented 6 years ago

Your .babelrc has the istanbul plugin under the test env. Please run npm i --save-dev cross-env then update your script to run cross-env NODE_ENV=test nyc mocha --exit. TBH I'm not familiar with the --exit argument of mocha / if that can prevent nyc from collecting/reporting coverage at the end of testing. I doubt the other babel plugin is preventing coverage, normally conflicts between plugins will cause incorrect coverage (extra branches for example), I've never seen other babel plugins cause no coverage to be collected.

jeff-bothe commented 6 years ago

@coreyfarrell thanks so much for the reply. The issue is fixed now but I am a bit confused as to why. Please read on, if you're interested in explaining.

At first, I didn't install cross-env because everyone on my team is either on a mac or running linux. I just prefaced my test script with NODE_ENV=test, but I was still having the same problem. So then I thought "maybe there is some other reason @coreyfarrell suggested cross-env", so I went ahead and installed it and of course now all is good.

So, can you tell me, is cross-env doing something other than making my test script work on Windows?

Thanks again!

coreyfarrell commented 6 years ago

@jeff-bothe That's interesting, I assumed by what you posted that you weren't setting the env at all, so I suggested cross-env simply because it's the correct cross-platform way to do it. You could try using your old method to run a script with console.log(process.env.NODE_ENV); to see if it displays what you expect. It's possible that the way you were running the tests (without cross-env) would require export NODE_ENV=test - IE if you set a variable from within a bash script then it's local to that specific process ID unless export'ed.