我试图收集 这个项目 的测试覆盖率
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.
我错过了什么?我应该在配置中添加什么以获得覆盖?
欢迎任何提示:)
尝试
- 将 glob 模式更改为
"src/**/*.{js,jsx,ts,tsx}"
。 - 删除
node_modules
然后运行yarn
以重新安装所有内容。 - 删除
node_modules
和yarn.lock
,然后重新安装所有内容,这导致了 另一个错误,我试图解决安装该特定依赖项的问题,但它不起作用。 - 从 GitHub 克隆存储库,然后在新版本上运行命令。
- 切换到不同的 Node 版本(v10.16.2 和 v11.7.0)。
原文由 Lual 发布,翻译遵循 CC BY-SA 4.0 许可协议
我在评论中说的快速修复,使用
--watchAll
代替,例如:react-scripts test --coverage --watchAll
。仅供将来参考,我认为理想情况下我们应该使用
--watch
,它只能在更改的文件上运行,但它给了我同样的麻烦。我认为这与这个问题有关 ’–coverage –watch’ 应该在第一次迭代时计算所有文件的覆盖率 以及这个问题 No coverage when running in watch mode 。我不确定为什么它适用于某些人而不是你,可能与 Git 和文件暂存有关。