npm test -- --coverage永远不会退出

33

我正在使用 create-react-app 创建一个 React 应用程序。当我执行 npm test -- --coverage 时,测试永远不会结束。实际上,npm test 运行的是 react-scripts test。有什么想法吗?

输入图像描述


你可以显示当前运行的实际命令。它应该在 package.json 的 scripts 对象中。如果命令中有 --,请尝试将其删除。例如:jest . --coverage - connormcwood
1
有很多关于react-tests未终止的线程。您可以尝试以下操作:将测试值设置为CI=true react-scripts test --env=jsdom,而不仅仅是react-scripts test。请参见https://github.com/facebook/create-react-app/issues/1137。 - connormcwood
3
"test": "CI=true react-scripts test"解决了这个问题。谢谢。 - Anup
没有什么特别的原因。在CI中,测试过程必须退出,这样CI服务器才能获取结果。当从命令行运行时,你应该让它保持打开状态,这样每次想验证某个东西时就可以进行一次测试。因此,它会以监视模式启动,并在每次保存代码时运行。 - jessehouwing
请您添加命令行输出,而不是其图片。请参阅为什么不鼓励使用文本、代码和数学表达式的图片? - Super Jade
显示剩余2条评论
7个回答

45

-- --coverage参数无法运行,并且应该使用下面的一个命令将CI设置为true

默认情况下,npm test使用交互式CLI运行监视器。但是,您可以通过设置名为CI的环境变量来强制运行测试一次并完成进程。

来源:React文档

Windows(cmd.exe)

  • set CI=true && npm test

  • set CI=true && npm run build

Windows(PowerShell)

  • ($env:CI = "true") -and (npm test)

  • ($env:CI = "true") -and (npm run build)

Linux、macOS(Bash)

  • CI=true npm test

  • CI=true npm run build


文档中不包含此部分内容

Docker(用于node和react):

docker run -e CI=true [myImage] npm run test


1
(针对React)上述答案的源代码:https://create-react-app.dev/docs/running-tests/#travis-ci - Akber Iqbal
1
非常感谢...它对我起作用了。 - Christian LSANGOLA

36

在Jest的观察模式下,覆盖率无法工作。

因为默认情况下,“react-scripts test --env=jsdom”在观察模式下运行,必须关闭观察模式才能生成覆盖输出。

以下摘自package.json的内容展示了一行“coverage”,说明如何在使用create-react-app引导的应用程序中实现代码覆盖率。

这只是修改后的“test”脚本,其中组合添加了选项--watchAll=false--coverage

 "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "coverage": "react-scripts test --env=jsdom --watchAll=false --coverage",
    "eject": "react-scripts eject"
  }
请注意,使用独立的双破折号--已经过时。

5

大多数情况下,出现此问题的原因如下。

  1. Not mentioning the required npm-script arguments in the package.json file. If you use create-react-app to create your react application, then it will not accept any command line arguments. To resolve this problem, add following line under the script tag in your package.json.

    "test": "react-scripts test --coverage --watchAll", //mark --watchAll=false if you want.
    
  2. Not mentioning the required jest configuration arguments in the package.json or jest.config.js files. You should mention the files which needed to include in your test coverage under the jest configurations. Add following configurations in your package.json.

package.json

  "jest": {
    "collectCoverageFrom": [
      "src/**/*.js",
      "!src/index.js", // files you need to avoid in test coverage
      "!src/hooks/*.js",
      "!src/context/*.js"
    ],
    "coverageThreshold": {
      "global": {
        "branches": 90,
        "functions": 90,
        "lines": 90,
        "statements": 90
      }
    },
    "coverageReporters": [
      "html",
      "text"
    ]
  },

2

在我的情况下,指定一个目录是有效的。

"test:cover": "react-scripts test --coverage src"

1
我尝试了上面所有的解决方法,但对于我来说,它仍然停在消息“Ran all test suites.”这里。但是这个小技巧有所帮助:
    "test:ci": "cross-env CI=true react-scripts test --forceExit --detectOpenHandles",

解释:问题出在Jest无法关闭所有进程上。以上是一个快速的解决方法。理想情况下,您应该跟踪阻止Jest退出的进程。

0
在我的情况下,只需添加一个新的脚本"test:coverage": "react-scripts test --coverage"
 "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "test:coverage": "react-scripts test --coverage",
    "eject": "react-scripts eject"
  },

0
你可以这样做:
"scripts": {
 //* other scripts like start, build

 "test": "react-scripts test",
 "test:coverage": "react-scripts test --coverage --watchAll=false",
},

奖励分: 由于运行覆盖测试需要时间来生成覆盖范围,因此我们可以添加两个脚本,一个用于普通测试以了解测试是否通过,另一个用于生成覆盖率。

现在您可以根据用例运行测试或带有覆盖范围的测试:

npm run test
npm run test:coverage

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接