create-react-app icon indicating copy to clipboard operation
create-react-app copied to clipboard

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

Open dbartholomae opened this issue 6 years ago • 19 comments

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"

dbartholomae avatar Dec 31 '18 00:12 dbartholomae

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"
    ],
  }

mrmckeb avatar Jan 04 '19 15:01 mrmckeb

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.

dbartholomae avatar Jan 04 '19 15:01 dbartholomae

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?

mrmckeb avatar Jan 06 '19 10:01 mrmckeb

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

dbartholomae avatar Jan 06 '19 19:01 dbartholomae

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)

FezVrasta avatar Jan 14 '19 16:01 FezVrasta

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[bot] avatar Feb 13 '19 16:02 stale[bot]

@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.

FezVrasta avatar Feb 13 '19 17:02 FezVrasta

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.

mrmckeb avatar Feb 18 '19 07:02 mrmckeb

+1

dragonghy avatar Jun 24 '19 19:06 dragonghy

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!

FezVrasta avatar Aug 08 '19 12:08 FezVrasta

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';

jensbodal avatar Nov 05 '19 23:11 jensbodal

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

nickserv avatar Oct 25 '20 13:10 nickserv

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.

bratanon avatar Jun 06 '22 00:06 bratanon

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

bratanon avatar Nov 22 '23 12:11 bratanon

same issue

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

anonymous1654 avatar Dec 20 '23 06:12 anonymous1654

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 */

raafaelgomees10 avatar Apr 04 '24 21:04 raafaelgomees10

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.

AndreyTheWeb avatar May 08 '24 07:05 AndreyTheWeb