我只想获得我目前正在处理的一个文件的覆盖率报告。
拥有整个应用程序文件的完整覆盖表,然后搜索我需要的文件,这有点让人不知所措。
我尝试的是对一个文件运行测试并添加
--coverage
。但它显示了所有文件的覆盖范围:
package.json
...
"test": "react-scripts test",
...
我的命令
npm test my-component.test --coverage
是否有一个选项可以添加到此命令以仅显示
my-component.tsx
覆盖范围?
解决方案是添加一个小选项
--collectCoverageFrom
来仅收集某个文件(即组件)。这是基于这篇文章
NPM版本
npm test my-component.test -- --coverage --collectCoverageFrom=src/components/my-component/my-component.tsx
--
之前多了一个 --coverage...
。这需要通过 npm
,因为如果没有它,将不会考虑提供的以下选项。
纱线版本
yarn test my-component.test --coverage --collectCoverageFrom=src/components/my-component/my-component.tsx
这将仅显示
my-component.tsx
的覆盖范围表。
注意:
my-component.tsx
文件的路径需要相对于项目根目录且准确。它不能像我对my-component.test
所做的那样是相对的。
你可以试试这个,对我有用
npm run coverage -- <filename>.test.ts
npm run test:coverage -- fileName.component.spec.ts