Jest 找到测试但不收集覆盖率

Jest finds tests but doesn't collect coverage

我正在尝试使用

收集 this project 的测试覆盖率
yarn test --coverage # i.e. "react-scripts test --coverage"

我的笑话配置是这样的:

  "jest": {
    "collectCoverageFrom": [
      "src/**/*.ts*"
    ],
    "coverageThreshold": {
      "global": {
        "lines": 90,
        "statements": 90
      }
    }
  }

我的文件夹结构如下:

.
├── package.json
├── postcss.config.js
├── public/
├── README.md
├── src/
│   ├── App.css
│   ├── App.test.tsx
│   ├── App.tsx
│   ├── assets/
│   ├── components/
│   │   ├── Emoji/
│   │   │   ├── Emoji.test.tsx
│   │   │   ├── Emoji.tsx
│   │   │   └── index.ts
│   │   └── Home/
│   │       ├── Home.css
│   │       ├── Home.test.tsx
│   │       ├── Home.tsx
│   │       └── index.ts
│   ├── index.css
│   ├── index.tsx
│   ├── react-app-env.d.ts
│   └── serviceWorker.ts
├── tsconfig.json
├── yarn-error.log
└── yarn.lock

Jest 能够找到所有测试,但无法收集覆盖率:

 PASS  src/components/Home/Home.test.tsx
 PASS  src/components/Emoji/Emoji.test.tsx
 PASS  src/App.test.tsx
----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |        0 |        0 |        0 |        0 |                   |
----------|----------|----------|----------|----------|-------------------|

Test Suites: 3 passed, 3 total
Tests:       3 passed, 3 total
Snapshots:   0 total
Time:        3.432s
Ran all test suites.

我错过了什么?我应该在配置中添加什么以获得覆盖率?

欢迎任何提示:)

尝试

  1. 正在将 glob 模式更改为 "src/**/*.{js,jsx,ts,tsx}"
  2. 删除 node_modules 然后 运行 yarn 重新安装所有内容。
  3. 删除 node_modulesyarn.lock,然后重新安装所有内容,这导致 another bug,我试图解决安装该特定依赖项的问题,但没有成功。
  4. 从 GitHub 克隆存储库,然后 运行 新版本上的命令。
  5. 切换到不同的节点版本(v10.16.2 和 v11.7.0)。

似乎在 Linux Mint 19.2 上运行良好。我建议将您的 jest 配置更改为更灵活的配置:

"jest": {
    "collectCoverageFrom": [
      "src/**/*.{js,jsx,ts,tsx}",
      "!<rootDir>/node_modules/"
    ],
    "coverageThreshold": {
      "global": {
        "lines": 90,
        "statements": 90
      }
    }
  }

然后更改 package.json 测试脚本,如果您使用的是 npm(对于 yarn,您可以只附加 --coverage,例如:yarn test --coverage;但是,对于 npm,它只会忽略它)。所以我建议要么这样做:

 "test": "react-scripts test --coverage",

或者,我建议使用 yarn 而不是 npm。要安装 yarn,请使用以下选项之一 methods.

工作:

我在评论中说的快速修复,使用 --watchAll 代替,例如:react-scripts test --coverage --watchAll.

仅供将来参考,我认为理想情况下我们应该使用 --watch,它只会 运行 更改文件,但它给我带来了同样的麻烦。我认为这与这个问题有关'--coverage --watch' should calculate coverage for all files at first iteration and also this issue No coverage when running in watch mode。我不确定为什么它适用于某些人而不是你,大概与 Git 和文件暂存有关。

不一定是原始提问者案例中的解决方案,但我 运行 遇到了完全相同的问题,这是我的解决方案:

我发现在升级 jest(从 23 到 26)时遇到了这个问题,解决方案是使用 --no-cache 选项 运行。据推测,他们在内部更改了这些覆盖率报告的某些内容,使得缓存数据不兼容。

就我而言,我正在使用选项 cacheDirectory: '.jest/cache' 测试 React Native/Expo 应用程序。删除 .jest 目录已解决我的问题。

将此添加到您的 package.json

"jest": {
    "testPathIgnorePatterns": [
      "<rootDir>/path/to/ignore/"
    ]
  }

这将解决您的问题,而不是指定很多这样的命令

去过那里... 您在 Jest 配置中还缺少一个参数:

collectCoverage: true

在我的例子中,我能够通过删除缓存并执行 npm install 来开始工作。在 windows 缓存位置 (~\AppData\Roaming\npm-cache)

对我有帮助的是

npm run test

npm run test --watchall

我这样做了:

npm run test a

npm run test a -- --coverage

在我们的案例中,问题出在开玩笑的 rootDir 设置(在我们的 package.json 中),我们将其设置为 tests。 Jest 因此忽略了我们实际源代码所在的 src/ 文件夹。解决方案是开玩笑地讲述这两个文件夹:

package.json旧

"jest": {
  "rootDir: "tests"
}

package.json 新

"jest": {
  "roots": [
    "src",
    "tests"
  ]
}

似乎问题出在标志 --watchAll 如果不将其设置为 false,它不会生成覆盖率

react-scripts test --reporters=jest-junit --reporters=default --coverage --coverageDirectory='testresults/coverage' --coverageReporters=cobertura --coverageReporters=lcov --watchAll=false 

对我来说,我更改了文件夹的名称并忘记更新 jest.config.ts 文件下的 collectCoverageFrom 键。