如果我跑
npm test --coverage
测试通过,但不运行覆盖率。当我改变
package.json
具有react-scripts test --coverage
然后,当我做 npm test
和 a
它运行测试但不运行覆盖率测试与覆盖率报告一起运行,但覆盖率显示覆盖率全为零
PASS src/components/Header/SubHeader.test.js
The <SubHeader /> component
✓ should render (4ms)
✓ should render with a goback button (1ms)
✓ should render
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 0 | 0 | 0 | 0 | |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 3 passed, 3 total
Snapshots: 3 passed, 3 total
Time: 1.077s
Ran all test suites.
终于,我意识到我可以做到npm run test .
问题:为什么其他方法不起作用?
最佳答案
重新审视这个问题后,我发现如果你运行它,你将触发覆盖命令并获得所有结果。
解决方案 1
npm run test -- --coverage .
当您将参数传递给 npm
时脚本,需要添加--
在添加类似 --coverage
的参数之前.就是这样npm
作品。 See this answer for passing arguments to npm解决方案 2
yarn test --coverage .
编辑:我问了一个关于
.
的问题传递命令和答案非常简单。 .
被传递是因为它是一个位置参数,与 --coverage
相反这是 npm test
的选项命令:Why is . passed as argument to npm without -- delimiter?---- 问题分析----
问题 1
npm test --coverage
你没有通过 --coverage
论据 test
脚本。在另一种情况下,当您将脚本编辑为:
react-scripts test --coverage
并运行 npm test
在这里你实际上添加了参数 --coverage
,但在脚本本身中,不是来自 npm
命令问题 2
当您这样做时
npm run test .
你路过.
这意味着您要在此命令中包含所有文件。出于某种原因
.
传递给 npm run test
脚本但不是 --coverage
.另一边
yarn
不带 --
传递所有参数.
关于reactjs - 如何使用 react-create-app 获取代码覆盖率报告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63696265/