Jestjs create react应用程序中所有提交文件的Jest覆盖率报告

Jestjs create react应用程序中所有提交文件的Jest覆盖率报告,jestjs,create-react-app,Jestjs,Create React App,在我的create react应用程序中为所有提交的文件生成Jest覆盖率报告时遇到困难 最初,代码覆盖率按预期生成,但是我的环境中发生了一些变化,现在只有自上次提交以来更改的文件显示覆盖率 我看到有很多关于这个问题的帖子,但是我自己无法解决 package.json: {... "devDependencies": { "@testing-library/jest-dom": "^5.11.9",

在我的create react应用程序中为所有提交的文件生成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.

您处于“监视”模式,该模式为。尝试使用-watchAll,以便运行所有测试并生成覆盖率

npm test -- --coverage --watchAll

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

您处于监视模式,这是一个非常重要的功能。尝试使用-watchAll,以便运行所有测试并生成覆盖率

npm test -- --coverage --watchAll

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

您在监视模式下运行它,它无法报告未运行的测试的覆盖率。请尝试使用例如CI=true npm test--coverage。@jonrsharpe CI环境变量控制除监视模式之外的其他函数,它们可能需要监视模式。@evelynhathaway没错,关键是要证明覆盖率是为运行的测试生成的。您在监视模式下运行它,它无法报告未运行的测试的覆盖率。请尝试例如CI=true npm test--coverage。@jonrsharpe CI环境变量控制除监视模式之外的其他函数,它们可能需要监视模式。@evelynhathaway没错,重点是要证明为运行的测试生成了覆盖率。