How to run Jest tests with coverage for one file
Asked Answered
O

3

48

I want to have coverage report for only one file that I am working on at the moment.

It is a bit overwhelming to have the full table of coverage for the whole application files and then search for the one I need.

What I tried was to run test for one file and add --coverage. But it shows coverage for all files:

package.json

...
"test": "react-scripts test",
...

My command

npm test my-component.test --coverage

Is there an option that I can add to this command to show me only my-component.tsx coverage?

Offstage answered 5/3, 2021 at 12:13 Comment(0)
O
80

The solution is to add one small option --collectCoverageFrom to collect only for a certain file (i.e. component). This is based on this post

NPM version
npm test my-component.test -- --coverage --collectCoverageFrom=src/components/my-component/my-component.tsx
Notice an extra -- before --coverage.... This needs to be passed for npm as following options provided will not be taken into consideration without it.

YARN version
yarn test my-component.test --coverage --collectCoverageFrom=src/components/my-component/my-component.tsx

This will show coverage table only for my-component.tsx.

NOTE:

The path to my-component.tsx file needs to be relative to project root and exact. It cannot be relative as I did for my-component.test.

Offstage answered 5/3, 2021 at 12:13 Comment(8)
If someone wants to add multiple files, you can use this: --collectCoverageFrom='["packages/**/index.js"]' --coverage . Original Ref: github.com/facebook/jest/issues/1932Primordial
Tried using the command from the above comment, but wouldn't work through the CLI. According to the docs, looks like it no longer takes an array, just the glob. collectCoverageFrom="packages/**/index.js" --coverage worked for meMode
If you add path/spec_file after the --collectCoverageFrom..., only that file will have the tests run. For example, npm test my-component.test -- --coverage --collectCoverageFrom=src/components/my-component/my-component.tsx src/components/my-component/my-component.spec.tsx.Engels
That is correct. You can run also all tests and take only specific coverage info.Offstage
This worked for me: npm test -- [fileName] --coverageNeil
Whit this command you are mentioning, you will run all of the tests and have coverage for all files. This answer is showcasing how to run tests from one test file and take coverage from one component file.Offstage
My coverage is alway 0 by running your above yarn command.Tonus
Check if you are targeting the correct file. It might happen that you misspelled file name. That happens to me all the timeOffstage
A
-1

You can try this out, it works for me

npm run coverage -- <filename>.test.ts
Armory answered 19/4 at 8:10 Comment(0)
I
-4
npm run test:coverage -- fileName.component.spec.ts
Incomprehensible answered 23/10, 2022 at 14:35 Comment(1)
Your answer could be improved with additional supporting information. Please "edit" to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center: stackoverflow.com/help/how-to-answerWurth

© 2022 - 2024 — McMap. All rights reserved.