简体   繁体   中英

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?

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.

This worked to me with create-react-app from windows cmd:

npx react-scripts test src/components/common/__test__/Dropdown.test.tsx --coverage --collectCoverageFrom=src/components/common/Dropdown.tsx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM