'React / Jest get coverage from a single component not all - current approach not working

I have tried this answer to get coverage for a single component but it is not working. The test runs only for that component but no coverage?

Is there some other configuration that needs to be done anywhere?

I have tried:

npm test src/components/component1/my-component.test.tsx --coverage --collectCoverageFrom=src/components/component1/my-component.test.tsx

Anything I am doing wrong?



Solution 1:[1]

The collectCoverageFrom takes in glob patterns as argument, so you need adjust that path string with "".

I have successfully run this command with code coverage from one file with

node_modules/.bin/jest ComponentTest.test.ts --coverage --collectCoverageFrom="path/Component.tsx"

It also it may be that your "test" cmd you are running from package.json is obscuring the additional arguments, try directly calling jest from modules to see if thats the case.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Luke Celitan