Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Code coverage comments like /* istanbul ignore file */ are ignored for jsx files #6106

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

Open
dbartholomae opened this issue Dec 31, 2018 · 19 comments

Comments

@dbartholomae
Copy link

Is this a bug report?

Yes

Did you try recovering your dependencies?

Yes - I have a minimum repo to reproduce.

Which terms did you search for in User Guide?

istanbul comment
coverage comment

Environment

System:
OS: Windows 10
CPU: x64 Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
Binaries:
Yarn: 1.12.3 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
npm: 6.4.1 - C:\Program Files\nodejs\npm.CMD
Browsers:
Edge: 42.17134.1.0
Internet Explorer: 11.0.17134.1
npmPackages:
react: ^16.7.0 => 16.7.0
react-dom: ^16.7.0 => 16.7.0
react-scripts: 2.1.2 => 2.1.2
npmGlobalPackages:
create-react-app: Not Found

Steps to Reproduce

Code coverage comments like /* istanbul ignore file */ are ignored for jsx files. When you set up a new app, adding this line to index.js doesn't work. This seems to be related to the Babel config as pure JS files work fine.

Here's an example repo:

  1. Clone https://github.com/dbartholomae/coverage-bug
  2. Run npm test (note that the command is modified in package.json and includes the coverage flag)

This could be related to #5756

Expected Behavior

Neither index.js nor index-no-jsx.js should show up in the coverage-report.

Actual Behavior

index.js shows up in the coverage-report.

Reproducible Demo

See "Steps to Reproduce"

@mrmckeb mrmckeb self-assigned this Jan 4, 2019
@mrmckeb
Copy link
Contributor

mrmckeb commented Jan 4, 2019

Hi @dbartholomae, you can actually use Jest here to ignore files. I know Istanbul comments work with Jest, but I'm not sure that specific comment does.

As an example, here I'm ignoring all tsx files (for no good reason):

  "jest": {
    "collectCoverageFrom": [
      "src/**/*.{js,jsx,ts}", 
      "!src/**/*.tsx"
    ],
  }

@dbartholomae
Copy link
Author

Thanks! Unfortunately that solution isn't ideal as I will have to add it to multiple files, and adding it in a central file introduces unnecessary coupling that makes it harder to refactor later on.
Furthermore the comment is respected in the .js file, only in the .jsx it is ignored - as are other comments like ignoring the next line. I assume it has to do with Babel config, maybe Babel throws out comments?
Btw I'm happy to help if there is a good way of doing so.

@mrmckeb
Copy link
Contributor

mrmckeb commented Jan 6, 2019

Hi @dbartholomae, I understand - I had assumed this was a jest issue, and that would explain why they have the coverage ignore patterns.

So, it sounds like the JSX transform is removing the comments... but that's also quite confusing, as it would be affecting a lot of people. Can you see the comments in your HTML report?

@dbartholomae
Copy link
Author

Yes, the comments are shown. I've also set up a repo which reproduces the bug with basically 2 lines changed compared to the vanilla verion created by create-react-app:
https://github.com/dbartholomae/coverage-bug

@FezVrasta
Copy link
Contributor

In my case, the file I add the comment to drops to 0% coverage even if it was higher without the comment (rather than ignoring it)

@stale
Copy link

stale bot commented Feb 13, 2019

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs.

@stale stale bot added the stale label Feb 13, 2019
@FezVrasta
Copy link
Contributor

@mrmckeb may you guys add some label to prevent this from getting auto-closed by the stale bot? This is a verified issue, there's no reason to close it automatically just because you guys are busy working on other things.

@mrmckeb
Copy link
Contributor

mrmckeb commented Feb 18, 2019

Hi @FezVrasta, sorry about that. We need to investigate this more. Would anyone be interested in picking this up and submitting a PR? If not, I can take a look this week.

@dragonghy
Copy link

+1

@FezVrasta
Copy link
Contributor

I'm trying to upgrade to 3.x one of my projects and now I'm reproducing this issue even on .js files. Is there any progress on this issue?

Thanks!

@jensbodal
Copy link

FWIW, for me the file was was only ignored if I placed this comment at the top of the file and had a newline between it and the next block:

WORKS

/* istanbul ignore file */

import { Foo } from 'bar';

DOES NOT WORK

/* istanbul ignore file */
import { Foo } from 'bar';

@nickserv
Copy link
Contributor

I don't seem to have this issue on src/index.tsx

@bratanon
Copy link

bratanon commented Jun 6, 2022

What @jensbodal said is true also for .ts files.

However I'm experience some confusing results my code

This works

/* istanbul ignore file */
import classnames from 'classnames';
import React from 'react';

but this doesn't work (swapping imports order)

/* istanbul ignore file */
import React from 'react';
import classnames from 'classnames';

Adding a newline results in both working.

sir-gon pushed a commit to sir-gon/algorithm-exercises-js that referenced this issue Jan 31, 2023
* Babel for ESLint import assertion:
  * https://stackoverflow.com/a/73225592
  * https://stackoverflow.com/a/72167806

* DISABLE instanbul for inspecting JSON imports
  * facebook/create-react-app#6106 (comment)
sir-gon pushed a commit to sir-gon/algorithm-exercises-js that referenced this issue Feb 1, 2023
* Babel for ESLint import assertion:
  * https://stackoverflow.com/a/73225592
  * https://stackoverflow.com/a/72167806

* DISABLE instanbul for inspecting JSON imports
  * facebook/create-react-app#6106 (comment)
@bratanon
Copy link

When using coverageProvider: "v8", the /* istanbul ignore file */ is totally ignored.

@anonymous1654
Copy link

anonymous1654 commented Dec 20, 2023

same issue

//istanbul ignore next works for individual functions, but /* istanbul ignore file */ doesn't work at all

@raafaelgomees10
Copy link

raafaelgomees10 commented Apr 4, 2024

When using coverageProvider: "v8", the /* istanbul ignore file */ is totally ignored.

Any istanbul ignore works when using `coverageProvider: "v8"...

  • /* istanbul ignore if */
  • /* istanbul ignore else */
  • /* istanbul ignore next */
  • /* istanbul ignore file */

@AndreyTheWeb
Copy link

FWIW, for me the file was was only ignored if I placed this comment at the top of the file and had a newline between it and the next block:

WORKS

/* istanbul ignore file */

import { Foo } from 'bar';

DOES NOT WORK

/* istanbul ignore file */
import { Foo } from 'bar';

If I use coverageProvider: “v8” then the whole file becomes covered, even if there is only one test.
I have a similar problem, /* istanbul ignore next */ does not work.

@olegafx
Copy link

olegafx commented Apr 30, 2025

If you're using it with vite/vitest, try to add @preserve to a comment:

/* istanbul ignore file @preserve */

@Miqlo
Copy link

Miqlo commented Apr 30, 2025

/* istanbul ignore file @preserve */Carlos Benavides

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