Jest 查找测试但不收集覆盖率 [英] Jest finds tests but doesn't collect coverage

查看:32
本文介绍了Jest 查找测试但不收集覆盖率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用

收集这个项目的测试覆盖率

yarn test --coverage # 即react-scripts test --coverage"

我的笑话配置是这样的:

 "jest": {"collectCoverageFrom": ["src/**/*.ts*"],覆盖阈值":{全球的": {行":90,声明":90}}}

我的文件夹结构如下:

<预><代码>.├── package.json├── postcss.config.js├── 公共/├── README.md├── src/│ ├── App.css│ ├── App.test.tsx│ ├── App.tsx│ ├──资产/│ ├──组件/│ │ ├── Emoji/│ │ │ ├── Emoji.test.tsx│ │ │ ├── Emoji.tsx│ │ │ └── index.ts│ │ └── 首页/│ │ ├── 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通过 src/components/Emoji/Emoji.test.tsx通过 src/App.test.tsx----------|----------|---------|---------|----------|--------------------|档案 |% stmts |% 分支 |% 函数 |% 行 |未覆盖的行#s |----------|----------|---------|---------|----------|--------------------|所有文件 |0 |0 |0 |0 ||----------|----------|---------|---------|----------|--------------------|测试套件:3 次通过,总共 3 次测试:3 次通过,共 3 次快照:共 0 个时间:3.432s运行所有测试套件.

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

欢迎任何提示:)

尝试

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

解决方案

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

仅供将来参考,我认为理想情况下我们应该使用 --watch,它只会在更改的文件上运行,但它给了我同样的麻烦.我认为这与这个问题有关 '--coverage --watch' 应该首先计算所有文件的覆盖率迭代以及这个问题在观察模式下运行时没有覆盖.我不确定为什么它适用于某些人而不适用于您,大概与 Git 和文件暂存有关.

I trying to collect test coverage for this project using

yarn test --coverage # i.e. "react-scripts test --coverage"

My jest config is this:

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

and my folder structure is as follows:

.
├── 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 is being able to find all the tests but it fails to collect coverage:

 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.

What am I missing? What should I add to the configuration to get the coverage?

Any hint is welcome :)

Tries

  1. Changing the glob pattern to "src/**/*.{js,jsx,ts,tsx}".
  2. Removing node_modules and then running yarn to reinstall everything.
  3. Removing node_modules and yarn.lock, and then reinstall everything, which led to another bug, which I tried to solve installing that particular dependency, but it didn't work.
  4. Cloning the repository from GitHub and then running the command on the fresh version.
  5. Switching to a different Node version (v10.16.2, and v11.7.0).

解决方案

The quick fix I said in my comment, using --watchAll instead, eg: react-scripts test --coverage --watchAll.

Just for future reference, I think ideally we should be using --watch, which would only run on changed files, but it gave me the same trouble. I think it's related to this issue '--coverage --watch' should calculate coverage for all files at first iteration and also this issue No coverage when running in watch mode. I'm not sure why it worked for some people and not you, presumably something to do with Git and staging of files.

这篇关于Jest 查找测试但不收集覆盖率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆