Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
bennycode opened this issue Feb 20, 2019 · 5 comments
Open

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

bennycode opened this issue Feb 20, 2019 · 5 comments

Comments

@bennycode
Copy link

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"],
});
@coreyfarrell
Copy link
Member

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.

@bennycode
Copy link
Author

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.

@coreyfarrell
Copy link
Member

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.

@bennycode
Copy link
Author

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?

@coreyfarrell
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants