reactjs - 如何使用 react-create-app 获取代码覆盖率报告?

标签 reactjs npm jestjs

如果我跑

npm test --coverage
测试通过,但不运行覆盖率。
当我改变package.json具有
react-scripts test --coverage
然后,当我做 npm testa它运行测试但不运行覆盖率
测试与覆盖率报告一起运行,但覆盖率显示覆盖率全为零
 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/

相关文章:

node.js - npm 的 'Skipping failed optional dependency' 是什么意思?

javascript - 使用 Jest 和 Enzyme 测试 react-router v4

javascript - 在 Jest 环境被拆除后导入文件

javascript - React Router 不会在登录和注销时重定向

javascript - 从父组件传递的 props 只能在渲染中使用吗?

node.js - 当我在控制台中构建项目时,阻止 VS Code 打开 Web 浏览器窗口

node.js - 如何在 Node.js 和 npm 安装后运行实时服务器

reactjs - React js Material-UI 响应式表格

node.js - 模拟 Typeorm QueryBuilder