How to get coverage of a folder using jest

Khushal Sahni
Apr 23, 2022

Simple task yet I couldn’t find it well enough on Google so here it goes

  • To find the coverage of any particular folder in your repository by the tests you have written, all you have to do is use the collectCoverageFrom flag
  • Example : jest --config=path/to/config.js --collectCoverageFrom="src/service/someFolder/**/*.ts"
  • The above command finds coverage of all the Typescript files (ts) inside someFolder
  • If you want to remove some folder or files from coverage, such as if you have models but you don’t want them to be part of the coverage report. You would do something like
  • jest --config=path/to/config.js --collectCoverageFrom="src/service/someFolder/**/*.ts" --collectCoverageFrom="!src/service/someFolder/**/model/*.ts"
  • Notice that there is a ! sign which tells jest to not include those files or folder. This command will exclude all the Typescript files inside model folder of the someFolder folder

That’s it, Cheers! 🍻

--

--

Khushal Sahni

SDE-1 @ Urban Company, Let’s help each other out :)