jest: Test suite failed to run, TypeError: Cannot read property 'bind' of undefined

jest: Test suite failed to run, TypeError: Cannot read property 'bind' of undefined

我们在 react-native 中使用来自单元测试的笑话。它运作良好。 我们刚刚删除了 npm 并重新安装了它,然后当我们尝试 运行 单元测试(npm 测试)时出现以下错误,我们无法 运行 测试用例。

可能出了什么问题。

我遇到了同样的问题。我开始只使用笑话进行几个测试,然后我在我的另一台机器上用 create-react-app 初始化文件夹并恢复以前的测试。之后,我切换回我当前的机器,进行了 npm 安装和 运行 测试。它给了我这个错误,所以我删除了 node_modules 并重新安装了 npm。现在所有的测试都是绿色的。

将 jest-cli 版本从 18 升级到 19 解决了这个问题。

Issue in github repo of jest

尝试重新安装 jest 和 jest babel 最新版本

npm install --save-dev jest

npm install --save-dev babel-jest regenerator-runtime

然后重新运行测试,对我有用

我有同样的错误:

Test suite failed to run, TypeError: Cannot read property 'bind' of undefined

类似于@Harish_N,但我使用的是react-scripts

在我的案例中,完全是关于将测试相关依赖项更新到package.json.

如果你在

中添加了这行代码
global.requestAnimationFrame = function(callback) {
    setTimeout(callback, 0);
};

在您的 "package.json" 中名为 "shim.js" 或 "setupshim.js" 的文件:

"setupFiles": [
     <rootDir>/__test__/setup/shim.js 
],

删除此文件。这将有助于消除 "Jest" 单元测试中的这个错误。

这对我有用,希望对你也有用。

反应 17+

  "devDependencies": {
    "@testing-library/dom": "^8.11.1",
    "@testing-library/jest-dom": "^5.16.1",
    "@testing-library/react": "^11.2.7",
    "@testing-library/user-event": "^12.8.3",
    "@types/jest": "^26.0.24",
    "@types/react": "^17.0.27",
    "@types/react-table": "^7.7.7",
    "jest": "26.0.1",
    "ts-jest": "26.0.0",
    "typescript": "^4.5.4"
  }

jest 27+ 版本出现错误,但设置为 26 后效果很好。

将此添加到您的代码中。

global.fetch = jest.fn().mockImplementation(() => mockFetchPromise);

字体:https://rishabhsrao.medium.com/mocking-and-testing-fetch-with-jest-c4d670e2e167