Repository navigation
How to exclude some files and folders from coverage report? #1455
Description
Activity
OK, I needed to ignore all
*.stories.jsfiles and you can do this by overriding the value for the JestcollectCoverageFromoption, which can be passed on the command-line via a customnpm testcommand. Note thatcollectCoverageFromexpects 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.
Reacted by Chafik, Nicholas Charbonneau, Carlos de la Orden, Andy Nguyen, abdennour, Vinh Le, Cal, Gabriel Théron, Pete Nykänen, Wang Guan and 39 moreReacted by Vincenzo Ferrari, Wagner Souza and Leo Borai@robcaldecott
Unfortunately I have an error every time I try to pass param with exclamation point.
bash: !src/**/*.stories.js: event not foundCan I override the value for the Jest config option in another way?
Reacted by Chance Smith, Leon Adler, Jonathan Palma and Sasha (Alex)How about adding the command to an npm script in your package.json? Does that work with bash?
It works. Thanks.
Is this feature documented?Could I add it to Readme?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",@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"Reacted by Ben Polinsky, Chen and Chance SmithThis is commonly requested & we should support this.
Reacted by Mykyta Liashenko, Guilherme Akio Sakae, Ryan Sullivan, Nurguly Ashyrov, Stef Dunlap, James Chao, Dan Lawson, Tomasz Cichocinski, Austin Wood, Cristen Jones and 67 moreWe could just add whole list of possible variants to documentation.
@NikitaLiashenko Sounds good, do you want to send a PR to User Guide?
Yes, let me do this
While adding an additional CLI argument seems like a simple enough fix, I agree with @mandysimon88 about putting excluded globs into
package.jsonbut 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 havecreateJestConfig.jsin react-scripts read Jest settings frompackage.jsonand override the original config? This way NPM scripts could be cleaner, and the config inpackage.jsonis set in a way that Jest already expects if you happen to eject your project later on.Example here:
ryansully@ec18454Then you can add Jest config to your
package.jsonlike so:"jest": { "collectCoverageFrom": [ "src/**/*.{js,jsx}", "!<rootDir>/node_modules/", "!<rootDir>/path/to/dir/" ] }
Reacted by Bob, Kamal Farsaoui, Yossi Jacob, Tony Goussot, Pradeep Senevirathne, Danilo Spinelli, andybflynn, Pramesh Bajracharya, Owen Buckley, EHM and 26 moreReacted by Tony Goussot, Alvaro Rago, EHM, Munala, Òscar Casajuana Alonso, Leo Borai, Timo Wang and Titilope MorolariReacted by Salvatore Tedde12 remaining items
Fixed in #1830.
Please help beta test the new version that includes this change!
#2172Reacted by dpikt, Filipe Pinheiro and Phivos StylianidesHow about using an annotation before the function name, like
@codeCoverageIgnoreinspired by PHPUnit ?Reacted by Kyle Roach, Salathiel Genese, Harry Gogonis, Terry Raimondo, Marco Afonso, Julio Betta, Petar Milutinovic, Gert Sønderby, Huy Nguyen, programstinator and 2 moreReacted by ReadyPlayerNaN, Gary White Jr., hawkeng and Leon AdlerIf you want to ignore any examples folder from coverage.
"jest": { "collectCoverageFrom": [ "src/**/*.{js,jsx}", "!**/examples/**" ] }Reacted by Paul Miu, Eric, Dan Boncuț, Ronald Rey, Souradeep Nanda, James Knight, Sam Tutton, Hom, Cheri Hung, David Clark and 20 moreReacted by Paul Miu, Joe Previte, Dani Esquinazi, Denys Feshchenko and Rafael LimaThis is what I was looking for (
node_modulesis 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
Reacted by Grant Carthew, Alexandr Zhidovlenko, JANG KI-YOUNG, Max Ciotti, Chris Dolphin and Mattia FranchettoCRA doesn't supports
coveragePathIgnorePatternsyet andcollectCoverageFromdoesn'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 ?
@3dos You missed
srcin the paths. This works for me:"jest": { "collectCoverageFrom": [ "src/**/*.{js,jsx}", "!/node_modules/", "!src/index.js", "!src/registerServiceWorker.js" ] }
Reacted by Filipe Dos Santos Mendes, Rohan Bagchi, finico, Michał Oleniec, Anton Burduzha, Nicolas Rouanne, kyampeire Hadijah, Gustavo Gard, Juriy Zaytsev, Harry Green and 2 moreThanks @kcjpop ! I feel so dumb now. Indeed this works
this ignores *.stories.js files for me:
collectCoverageFrom: [ 'components/**/*.{js,jsx}', '!components/**/*.stories.{js,jsx}', 'pages/**/*.{js,jsx}' ]or
coveragePathIgnorePatterns: ['.story.js']Reacted by Frantisek Skorunka, Ahmad K, Vojtěch Baránek, Chase DeAnda and Niels DequekerReacted by sashless, Johannes Bräunig and Dale FrenchI really only needed to exclude two files: index.js and registerServiceWorker.js ...
So, I added
/* istanbul ignore file */to each file (after anyimportstatements).Reacted by Quail@NikitaLiashenko @robcaldecott Wrapping the collectCoverageFroms, containing an exclamation, in single quotes works for me
npm test -- --coverage --collectCoverageFrom=src/**/*.js --collectCoverageFrom='!src/**/*.stories.js'- locked and limited conversation to collaborators
on Jan 18, 2019
I am trying to exclude some JS files from coverage report.
I have already added this to package.json
Unfortunately files from those dir still appear when I run
CI=true npm test -- --coverage