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

nyc trying to cover babel plugin even though it's excluded via config #581

Closed
NiGhTTraX opened this issue May 26, 2017 · 2 comments
Closed

Comments

@NiGhTTraX
Copy link

In a project that uses babel-istanbul and babel-register AND a custom babel plugin, nyc instruments the custom plugin even though it's in a folder excluded from coverage via .nycrc:exclude.

Expected Behavior

100% coverage on src/foobar.js and nothing else because nyc is set to exclude the tests/ folder which includes the babel-plugin defined in .babelrc.

Observed Behavior

image

image

Bonus Points! Code (or Repository) that Reproduces Issue

https://github.com/NiGhTTraX/nyc-exclude-bug

{
  "include": [
    "src/**/*.js"
  ],
  "exclude": [
    "tests/**/*.js"
  ],
  "all": true,

  "sourceMap": false,
  "instrument": false,

  "reporter": ["html"],
  "report-dir": "tests/unit/results/coverage",

  "lines": 100,
  "branches": 100,
  "check-coverage": true
}

Forensic Information

Operating System: Ubuntu 16.04
Environment Information:

Node: v6.9.5
npm: 3.10.10

NiGhTTraX added a commit to NiGhTTraX/react-test-buffet that referenced this issue May 26, 2017
This enables the tests to be run with Mocha in Node without having to bundle
them with Webpack first. This is now consistent with the debug build which uses
Webpack and noop-loader.

The babelrc config for the `tests` env has been split in 2 to not generate
coverage reports when watching the files but still ignore static assets.

The Babel plugin includes an ignore pragma for Istanbul because of a bug causes
it to not be excluded via nycrc.

istanbuljs/nyc#581
@JaKXz JaKXz self-assigned this May 27, 2017
@bcoe
Copy link
Member

bcoe commented Jul 30, 2017

@NiGhTTraX thanks for the great reproduction, and sorry for the slow reply have been trying to gradually catch up on old GitHub issues.

I did a tiny bit of digging using your example repo, and the issue seems to be an interaction between the exclude rules and he --all functionality. My hunch is that the walk of files that takes place during the --all step isn't having the include and exclude rules applied.

If you feel like contributing to the project, this is probably a pretty straight forward fix; I think it will be in nyc/index.js.

NiGhTTraX added a commit to NiGhTTraX/react-test-buffet that referenced this issue Aug 31, 2017
nyc has a bug where it walks all the files when `all: true`.
istanbuljs/nyc#581
@NiGhTTraX
Copy link
Author

So I found the real problem: the plugin is instrumented by babel-plugin-istanbul when it's required by Babel while transpiling a source file with babel-register. Deep breath. Here's how it happens:

  1. if (config.all) nyc.addAllFiles() - entry point for when all: true
  2. glob.sync(pattern, {cwd: dir, nodir: true, ignore: this.exclude.exclude}) - this correctly excludes the plugin; let's assume that it emits todo.jsx for now
  3. NYC.prototype._readTranspiledSource requires the source file
  4. babel-register handler will pick the require and compile it
  5. Doing so it will apply the plugins in .babelrc over it
  6. my-custom-babel-plugin is part of .babelrc so Babel will fetch it by doing a require call
  7. babel-register sees that require and tries to compile the plugin
  8. It will again use the plugins in .babelrc
  9. At this point babel-plugin-istanbul will instrument my-custom-babel-plugin
  10. That instrumented plugin will report coverage to nyc

So yeah, not really a nyc bug. Maybe babel-plugin-istanbul could read the same excludes and bail out if it's attempting to instrument an excluded file?

#649 would work around the problem, but I'd like to have the code not emit coverage reports in case other tools will read it instead of nyc.

A foolproof workaround is to put the plugin in a separate folder and add a .babelrc there that removes babel-plugin-istanbul. This is the solution I've went with and it's working as expected. I had to duplicate the .babelrc file, but eh...

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

No branches or pull requests

3 participants