babel-plugin-istanbul icon indicating copy to clipboard operation
babel-plugin-istanbul copied to clipboard

How to use istanbul's --include-all-sources flag?

Open bennycode opened this issue 5 years ago • 5 comments

I have read that babel-plugin-istanbul supports exclude/include rules but how to make use of istanbul's --include-all-sources flag?

I have tried adding --all to my nyc reporter script but it does not seem to have any effect:

package.json

"scripts": {
  "coverage": "yarn test && nyc report --all",
  "test": "electron-mocha --require ./babel-register.js **/__tests__/*.test.ts"
}

babel-register.js

require("@babel/register")({
  cache: false,
  presets: [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": "current"
        }
      }
    ],
    "@babel/preset-typescript"
  ],
  plugins: [
    "@babel/proposal-class-properties",
    "istanbul"
  ],
  extensions: [".ts"],
});

bennycode avatar Feb 20 '19 17:02 bennycode

The nyc report doesn't (can't) perform instrumentation, it only reports. Try the following nyc --require ./babel-register.js --instrument=false --source-map=false --all=true true. What this does is cause nyc to run, generate coverage for all sources, then report. In this command the final true is a command which exits with a success code and no output (not sure if this is valid on Windows). That final true is needed because nyc needs to execute something, this could just as easily be any script which exits with success.

Probably be better to use .nycrc or put an "nyc" section into package.json instead of using all the arguments. See https://istanbul.js.org/docs/tutorials/es2015/ for general guide for using nyc with babel.

coreyfarrell avatar Feb 21 '19 13:02 coreyfarrell

Ah, is it possible to replace electron-mocha with nyc? My tests need to run in an Electron application context and I read that istanbul and electron-mocha don't play well together.

bennycode avatar Feb 21 '19 13:02 bennycode

You will need to run the nyc command separately. Even if nyc can work with an electron process we cannot support it. I suggest making your coverage script run yarn test && nyc --require ./babel-register.js --instrument=false --source-map=false --all=true -- node -e ''. This improves my previous suggestion as it doesn't depend on a true script existing.

coreyfarrell avatar Feb 21 '19 13:02 coreyfarrell

Ok, understood. Thanks for your support!

If I am going with the script you provided, do I then still need to run my electron-mocha --require ./babel-register.js **/__tests__/*.test.ts script? If yes, should I run it before or after nyc?

bennycode avatar Feb 21 '19 13:02 bennycode

Oh sorry one more thing, you need to add --clean=false to the nyc command. So I suggest: package.json

"scripts": {
  "coverage": "yarn test && nyc --clean=false --require ./babel-register.js --instrument=false --source-map=false --all=true -- node -e ''",
  "test": "electron-mocha --require ./babel-register.js **/__tests__/*.test.ts"
}

Don't change anything else. This way yarn test will run your tests, generating coverage for sources that you actually run. The nyc command will generate coverage data for all sources (with zero hits for all statements/branches/functions), merge with your existing test then report.

coreyfarrell avatar Feb 21 '19 14:02 coreyfarrell