reactjs - 将 Jest 与 react-scripts 一起使用(通过 yarn 运行),如何在不必传递 watchAll 标志的情况下获得完整的覆盖率报告?

标签 reactjs jestjs babeljs

我有一个名为 jest-playground-1 的基本 create-react-app 设置,它的 package.json 文件为

{
  "name": "jest-playground-1",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/dom": "^6.10.1",
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.3",
    "@testing-library/user-event": "^7.1.2",
    "babel-jest": "^24.9.0",
    "react": "^16.12.0",
    "react-dom": "^16.12.0",
    "react-scripts": "3.3.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "jest": {
    "coverageReporters": [
      "html",
      "text"
    ]
  }
}

当我运行 yarn test --coverage(注意 --coverage 标志根据文档传递给 Jest)时,我希望看到所有我全新项目中的文件。

相反,我看不到任何文件,Jest runner 看起来像

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 运行它,我会看到我期望的输出:

 PASS  src/App.test.js
  ✓ renders learn react link (49ms)

------------------|----------|----------|----------|----------|-------------------|
File              |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
------------------|----------|----------|----------|----------|-------------------|
All files         |     2.44 |        0 |     6.25 |     2.44 |                   |
 App.js           |      100 |      100 |      100 |      100 |                   |
 index.js         |        0 |      100 |      100 |        0 |              7,12 |
 serviceWorker.js |        0 |        0 |        0 |        0 |... 25,132,133,134 |
------------------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        6.591s
Ran all test suites.

Watch Usage
 › Press f to run only failed tests.
 › Press o to only run tests related to changed files.
 › 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 标志运行时,我在项目 App.js、index.js 和 serviceWorker.js 中看到了三个文件

我如何告诉 Jest 默认运行这些而不必传递 --watchAll 标志?

最佳答案

你可以通过 --watch "你的文件扩展名"

"scripts": {
.
.
.
  "test": "react-scripts test --coverage --watch .test.js"
.
.
.

关于reactjs - 将 Jest 与 react-scripts 一起使用(通过 yarn 运行),如何在不必传递 watchAll 标志的情况下获得完整的覆盖率报告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59289886/

相关文章:

arrays - 使用 useState Hook 将项目添加到存储在 React 状态的数组中的正确方法?

reactjs - 链接 Gatsby JS 的音译

jestjs - 使用 Jest 测试一个命令行工具

reactjs - 引用错误 : $RefreshReg$ is not defined

reactjs - Socket.io 连接属性始终为 false

reactjs - 基于 react Material 表中的行数据的行操作

javascript - Jest 测试中的 Preact Compat 不兼容性

unit-testing - 如何在 Jest 中模拟 i18next 模块

gruntjs - 使用 babel 和 grunt 递归地将 ES6 转换为 ES5

node.js - 无法解析 'babel-loader'