Jest无限挂起,未运行任何测试。

54

每次我运行jest时,它都不会执行任何操作。我已经让计数器任意增高了。我已经使用--no-cache选项运行了jest。

jest --debug 输出如下:

{
  "configs": [
    {
      "automock": false,
      "browser": false,
      "cache": true,
      "cacheDirectory": "/var/folders/7v/64n1tsk11zs2pbwf5bm_c9kc0000gn/T/jest_dx",
      "clearMocks": false,
      "coveragePathIgnorePatterns": [
        "/node_modules/"
      ],
      "detectLeaks": false,
      "forceCoverageMatch": [],
      "globals": {},
      "haste": {
        "defaultPlatform": "ios",
        "platforms": [
          "android",
          "ios",
          "native"
        ],
        "providesModuleNodeModules": [
          "react-native"
        ]
      },
      "moduleDirectories": [
        "node_modules"
      ],
      "moduleFileExtensions": [
        "js",
        "json",
        "jsx",
        "node"
      ],
      "moduleNameMapper": [
        [
          "^React$",
          "/Users/skilurus/github/flock-react-app/node_modules/react"
        ]
      ],
      "modulePathIgnorePatterns": [
        "/Users/skilurus/github/flock-react-app/node_modules/react-native/Libraries/react-native/"
      ],
      "name": "b29a126b130a0be47202d3bc7b00f1b4",
      "resetMocks": false,
      "resetModules": false,
      "restoreMocks": false,
      "rootDir": "/Users/skilurus/github/flock-react-app",
      "roots": [
        "/Users/skilurus/github/flock-react-app"
      ],
      "runner": "jest-runner",
      "setupFiles": [
        "/Users/skilurus/github/flock-react-app/node_modules/regenerator-runtime/runtime.js",
        "/Users/skilurus/github/flock-react-app/node_modules/react-native/jest/setup.js",
        "/Users/skilurus/github/flock-react-app/test-setup.js"
      ],
      "snapshotSerializers": [
        "/Users/skilurus/github/flock-react-app/node_modules/enzyme-to-json/serializer.js"
      ],
      "testEnvironment": "/Users/skilurus/github/flock-react-app/node_modules/jest-environment-jsdom/build/index.js",
      "testEnvironmentOptions": {},
      "testLocationInResults": false,
      "testMatch": [
        "**/__tests__/**/*.js?(x)",
        "**/?(*.)(spec|test).js?(x)"
      ],
      "testPathIgnorePatterns": [
        "/node_modules/",
        "e2e"
      ],
      "testRegex": "",
      "testRunner": "/Users/skilurus/github/flock-react-app/node_modules/jest-jasmine2/build/index.js",
      "testURL": "about:blank",
      "timers": "real",
      "transform": [
        [
          "^.+\\.js$",
          "/Users/skilurus/github/flock-react-app/node_modules/babel-jest/build/index.js"
        ],
        [
          "^[./a-zA-Z0-9$_-]+\\.(bmp|gif|jpg|jpeg|mp4|png|psd|svg|webp)$",
          "/Users/skilurus/github/flock-react-app/node_modules/react-native/jest/assetFileTransformer.js"
        ]
      ],
      "transformIgnorePatterns": [
        "node_modules/(?!react-native|native-base|react-navigation|react-native-fabric|tipsi-stripe)"
      ],
      "watchPathIgnorePatterns": []
    }
  ],
  "globalConfig": {
    "bail": false,
    "changedFilesWithAncestor": false,
    "collectCoverageFrom": [
      "src/**/*.{js,jsx}"
    ],
    "coverageDirectory": "/Users/skilurus/github/flock-react-app/__coverage__",
    "coverageReporters": [
      "json",
      "lcov",
      "text"
    ],
    "coverageThreshold": {
      "global": {
        "branches": 70,
        "functions": 75,
        "lines": 85,
        "statements": 80
      }
    },
    "detectLeaks": false,
    "expand": false,
    "globalSetup": null,
    "globalTeardown": null,
    "listTests": false,
    "mapCoverage": false,
    "maxWorkers": 7,
    "noStackTrace": false,
    "nonFlagArgs": [],
    "notify": false,
    "notifyMode": "always",
    "passWithNoTests": false,
    "rootDir": "/Users/skilurus/github/flock-react-app",
    "runTestsByPath": false,
    "testFailureExitCode": 1,
    "testPathPattern": "",
    "testResultsProcessor": null,
    "updateSnapshot": "new",
    "useStderr": false,
    "verbose": true,
    "watch": false,
    "watchman": true
  },
  "version": "22.3.0"
}

node --version:8.9.4

npm --version:5.6.0

yarn --version:1.3.2

有人遇到过类似的问题吗?有人知道如何修复吗?


你解决了这个问题吗? - Amrendra
可能是的,但我不知道或记得它是什么。我没有在此处撰写答案的事实表明,我从未真正弄清楚如何修复它,而某些随机咒语为我解决了特定实例的问题。 - Abraham P
我通过更改Node版本来修复了它。 - Amrendra
你的测试启动了7个工作进程,因此很难确定哪一个进程出现了无限循环。使用 --runInBand 命令行选项可以让测试按顺序运行,这对于开始测试是非常有帮助的。由于在经过了163秒后仍然没有任何反应,你需要找出导致无限循环的测试用例。 - Șerban Ghiță
19个回答

2

对我来说,这个帖子中的答案并不足够(watchmen、运行时标志等),尽管运行时标志确实帮助我调试了这个问题。

对我来说,从Jest 26升级到27+时出现了完全相同的问题。

问题实际上源于在我的jest setupFile中使用jest.useFakeTimers()。这是我以前为react-navigate设置的一个解决方法。

需要删除jest.useFakeTimers()配置,并使测试异步化,在每个测试结束时休眠一段时间以允许其拆除。

const sleep = (ms: number) => new Promise((resolve) => setTimeout(() => resolve(true), ms));

afterAll(async () => {
  await sleep(10); // avoid jest open handle error
});

it('...', async () => {

非常感谢,我在尝试进行测试时还添加了jest.useFakeTimers()。 - sprut

1
在我的情况下,将 nodejs 从旧版本(例如 12.16.1)更新到更近的版本(例如 14.17.3),然后重新安装 jest (npm install jest -g) 完全解决了这个问题。

切换 Node.js 版本对我很有帮助。我使用的是 nvm,之前用的是 v16,但切换到 v14 就解决了问题。 - ahong

1

使用 --maxWorkers 选项在我的端上起作用了。Jest 将会适应您的最大资源。无论您拥有多少资源,Jest 都将会占用它们。您可以通过使用以下命令来限制 CPU 的使用:

--maxWorkers=<num>|<string>


谢谢,它有效了! 之前,当Node占用了我的20个CPU核心:Intel® Core™ i9-12900H处理器(总核心数14 / 总线程数20)时,我的笔记本电脑就会卡住。 操作系统:Ubuntu 22.04 - Oleksandr Bratashov

0

我在NestJS项目中遇到了类似的问题。结果发现其中一个模块包含了一个导入的模块,但是它没有被安装。而且出乎意料的是,在测试启动时并没有提供任何关于这个问题的信息。


0
在我的情况下,我尝试了互联网上的所有方法,唯一的解决方案是将 isolatedModules: true 设置为 true(我明确将其设置为 false)。

0
我遇到了同样的问题,测试从未开始,计时器无限地计数。没有任何建议的解决方案起作用。
事实证明,我需要从命令行中删除--experimental-vm-modules节点标志,该标志在使用Node v16时有效,但在更新到Node v20后导致了这个问题。
希望能对某人有所帮助。

0

@rohit 的回答给了我找到解决问题的线索,我的代码本来是正常工作的,但是 jest 在使用 useEffect 时陷入了无限循环。

所以我通过制作一个模拟来解决了这个问题。

import React from 'react'

const mockUseEffect = jest.fn()

jest.spyOn(React, 'useEffect').mockImplementation(mockUseEffect)

你可以根据自己的需求进行调整


0

检查你的测试,在我的情况下,我犯了一个错误,在测试中多放了一个 "a"。

  test('test expected that ..', async (a) => {
...

这会让我的测试永远挂起(使用node 14.15和jest 29.1.2)。


0

我通过修改我的 .babelrc 文件来解决了这个问题。

{
 "presets": ["@babel/preset-env", "@babel/preset-react"],
 "plugins": [
   [
     "styled-jsx/babel",
     {
       // "plugins": ["styled-jsx-plugin-postcss"]
     }
   ]
 ]
}

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