Skip to content

How to exclude some files and folders from coverage report? #1455

Description

@MykytaLiashenko

I am trying to exclude some JS files from coverage report.
I have already added this to package.json

"jest": {
    "testPathIgnorePatterns" : ["<rootDir>/node_modules/", "<rootDir>/path/to/dir/" ],
    "testResultsProcessor": "./node_modules/jest-junit-reporter"
  }

Unfortunately files from those dir still appear when I run CI=true npm test -- --coverage

Activity

  1. robcaldecott commented on Jan 30, 2017

    @robcaldecott

    OK, I needed to ignore all *.stories.js files and you can do this by overriding the value for the Jest collectCoverageFrom option, which can be passed on the command-line via a custom npm test command. Note that collectCoverageFrom expects an array so to pass multiple values you specify the option multiple times.

    For example:

    npm test -- --coverage --collectCoverageFrom=src/**/*.js --collectCoverageFrom=!src/**/*.stories.js

    Notice the exclamation point (can I still call it a pling or does that betray my age?) to exclude a pattern.

  2. MykytaLiashenko commented on Jan 30, 2017

    @MykytaLiashenko
    Author

    @robcaldecott
    Unfortunately I have an error every time I try to pass param with exclamation point.
    bash: !src/**/*.stories.js: event not found

  3. MykytaLiashenko commented on Jan 30, 2017

    @MykytaLiashenko
    Author

    Can I override the value for the Jest config option in another way?

  4. robcaldecott commented on Feb 1, 2017

    @robcaldecott

    How about adding the command to an npm script in your package.json? Does that work with bash?

  5. MykytaLiashenko commented on Feb 1, 2017

    @MykytaLiashenko
    Author

    It works. Thanks.
    Is this feature documented?Could I add it to Readme?

  6. amandapouget commented on Feb 2, 2017

    @amandapouget

    This is not a great solution because you have to type out the options in the CLI every time. I'd rather put my 'exclude xyz folders' into the package.json and forget about it. Can someone give an example, with react scripts, of how to modify this line so that your solution works?

        "test": "CI=true react-scripts test --env=jsdom",
    
  7. MykytaLiashenko commented on Feb 2, 2017

    @MykytaLiashenko
    Author

    @mandysimon88
    Actually there are two solutions and the second fits to you.
    "test" : "CI=true react-scripts test --env=jsdom --coverage --collectCoverageFrom=src/**/*js --collectCoverageFrom=!src/path/to/file"

  8. added this to the milestone on Feb 9, 2017
  9. gaearon commented on Feb 9, 2017

    @gaearon
    Contributor

    This is commonly requested & we should support this.

  10. MykytaLiashenko commented on Feb 9, 2017

    @MykytaLiashenko
    Author

    We could just add whole list of possible variants to documentation.

  11. gaearon commented on Feb 11, 2017

    @gaearon
    Contributor

    @NikitaLiashenko Sounds good, do you want to send a PR to User Guide?

  12. MykytaLiashenko commented on Feb 12, 2017

    @MykytaLiashenko
    Author

    Yes, let me do this

  13. ryansully commented on Feb 26, 2017

    @ryansully
    Contributor

    While adding an additional CLI argument seems like a simple enough fix, I agree with @mandysimon88 about putting excluded globs into package.json but more like the original comment. If you have multiple globs to exclude, the CLI arguments can result in a very long command. Might it be better to instead have createJestConfig.js in react-scripts read Jest settings from package.json and override the original config? This way NPM scripts could be cleaner, and the config in package.json is set in a way that Jest already expects if you happen to eject your project later on.

    Example here:
    ryansully@ec18454

    Then you can add Jest config to your package.json like so:

    "jest": {
      "collectCoverageFrom": [
        "src/**/*.{js,jsx}",
        "!<rootDir>/node_modules/",
        "!<rootDir>/path/to/dir/"
      ]
    }
  14. 12 remaining items

  15. gaearon commented on May 16, 2017

    @gaearon
    Contributor

    Fixed in #1830.

  16. gaearon commented on May 16, 2017

    @gaearon
    Contributor

    Please help beta test the new version that includes this change!
    #2172

  17. ancyrweb commented on Sep 13, 2017

    @ancyrweb
    Contributor

    How about using an annotation before the function name, like @codeCoverageIgnore inspired by PHPUnit ?

  18. viraths commented on Feb 20, 2018

    @viraths

    If you want to ignore any examples folder from coverage.

    "jest": {
      "collectCoverageFrom": [
        "src/**/*.{js,jsx}",
        "!**/examples/**"
      ]
    }
    
  19. beauallison commented on May 7, 2018

    @beauallison

    This is what I was looking for (node_modules is the default so you need to re-add it)

    "jest": {
        "coveragePathIgnorePatterns": [
          "/node_modules/",
          "/test/"
        ]
      }
    

    Reference: https://facebook.github.io/jest/docs/en/configuration.html#coveragepathignorepatterns-array-string

  20. 3dos commented on Jun 26, 2018

    @3dos

    CRA doesn't supports coveragePathIgnorePatterns yet and collectCoverageFrom doesn't seem to work either for me

    "jest": {
        "collectCoverageFrom": [
          "src/**/*.{js,jsx}",
          "!/node_modules/",
          "!/index.js",
          "!/registerServiceWorker.js"
        ]
      }
    

    Still gets me index and rsw in the coverage. I know running after a 100% coverage is a misleading goal but still, writing tests for these is something I will not do and would be glad not to see these files in the coverage results.

    Is there an easy way to ignore them ?

  21. kcjpop commented on Jun 26, 2018

    @kcjpop

    @3dos You missed src in the paths. This works for me:

      "jest": {
        "collectCoverageFrom": [
          "src/**/*.{js,jsx}",
          "!/node_modules/",
          "!src/index.js",
          "!src/registerServiceWorker.js"
        ]
      }
  22. 3dos commented on Jun 27, 2018

    @3dos

    Thanks @kcjpop ! I feel so dumb now. Indeed this works

  23. jstein-ftdi commented on Jul 12, 2018

    @jstein-ftdi

    this ignores *.stories.js files for me:

    collectCoverageFrom: [ 
        'components/**/*.{js,jsx}', 
        '!components/**/*.stories.{js,jsx}', 
        'pages/**/*.{js,jsx}'
    ]
    
  24. omelniz commented on Aug 7, 2018

    @omelniz

    or
    coveragePathIgnorePatterns: ['.story.js']

  25. groundh0g commented on Jan 7, 2019

    @groundh0g

    I really only needed to exclude two files: index.js and registerServiceWorker.js ...

    So, I added /* istanbul ignore file */ to each file (after any import statements).

  26. gewbr commented on Jan 9, 2019

    @gewbr

    @NikitaLiashenko @robcaldecott Wrapping the collectCoverageFroms, containing an exclamation, in single quotes works for me npm test -- --coverage --collectCoverageFrom=src/**/*.js --collectCoverageFrom='!src/**/*.stories.js'

  27. locked and limited conversation to collaborators on Jan 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions