How to get coverage of a folder using jest
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 tonot
include those files or folder. This command will exclude all the Typescript files insidemodel
folder of thesomeFolder
folder
That’s it, Cheers! 🍻