【问题标题】:Jest coverage reporting on all committed files in create-react-app关于 create-react-app 中所有已提交文件的 Jest 覆盖率报告
【发布时间】:2021-05-09 09:24:53
【问题描述】:

在我的 create-react-app 中为所有提交的文件生成 Jest 覆盖率报告时遇到困难。

最初,代码覆盖率按预期生成,但我的环境发生了一些变化,现在只有自上次提交以来更改的文件显示覆盖率。

我看到有很多关于这个问题的其他帖子,但是我自己无法解决。

package.json:

{...
    "devDependencies": {
        "@testing-library/jest-dom": "^5.11.9",
        "@testing-library/react": "^11.2.5",
        "@testing-library/user-event": "^12.6.3",
        "react-test-renderer": "^17.0.1"
    },
    "jest": {
        "testMatch": [ "**/tests/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)" ],
        "coverageReporters": ["json","html","lcov", "text"]
    }
...}

项目结构:

|
+--node_modules
+--src
    |
    + __tests__
    + App.js
- package.json

运行命令npm test -- --coverage 产生以下输出:

No tests found related to files changed since last commit.
Press `a` to run all tests, or run Jest with `--watchAll`.
----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |       0 |        0 |       0 |       0 |                   
----------|---------|----------|---------|---------|-------------------

Watch Usage
 › Press a to run all tests.
 › Press f to run only failed tests.
 › Press q to quit watch mode.
 › Press p to filter by a filename regex pattern.
 › Press t to filter by a test name regex pattern.
 › Press Enter to trigger a test run.

【问题讨论】:

  • 你在监视模式下运行它,它不能报告它没有运行的测试的覆盖率。尝试例如CI=true npm test -- --coverage.
  • @jonrsharpe CI 环境变量控制着监视模式以外的其他功能,他们可能需要监视模式。
  • @evelynhathaway 没错,关键是要证明为运行的测试生成了覆盖率

标签: jestjs create-react-app


【解决方案1】:

您处于观看模式,has a known issue when running coverage on a subset of files。尝试使用--watchAll,以便运行所有测试并生成覆盖率。

npm test -- --coverage --watchAll

为此,我喜欢在我的package.json 中创建一个名为coverage 的特殊npm 运行脚本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-21
    • 2020-05-05
    • 1970-01-01
    • 2021-05-26
    • 1970-01-01
    • 2019-04-17
    • 2020-02-26
    • 2020-07-10
    相关资源
    最近更新 更多