简体   繁体   English

React / Jest 从单个组件而不是全部获得覆盖 - 当前方法无效

[英]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 "". collectCoverageFrom 将 glob 模式作为参数,因此您需要使用“”调整该路径字符串。

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.也可能是您从 package.json 运行的“测试”cmd 掩盖了额外的 arguments,尝试直接从模块调用 jest 以查看是否是这种情况。

This worked to me with create-react-app from windows cmd:这对我有用,来自 windows cmd 的 create-react-app:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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