Jest覆盖报告所有提交的文件在react-app

amrnrhlw  于 8个月前  发布在  Jest
关注(0)|答案(2)|浏览(61)

在我的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.

vsikbqxv

vsikbqxv1#

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

npm test -- --coverage --watchAll

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

ppcbkaq5

ppcbkaq52#

您需要运行包含a的脚本react-scripts test --coverage以运行所有测试。
例如:react-scripts test --coverage a

相关问题