【问题标题】:Jest TypeError: Cannot read property 'bind' of undefined in JestAdapterJest TypeError:无法读取 JestAdapter 中未定义的属性“绑定”
【发布时间】:2021-10-08 08:24:12
【问题描述】:
Test suite failed to run

          TypeError: Cannot read property 'bind' of undefined
      at jestAdapter (node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:30:56)

重现行为的步骤:

依赖关系

"jest": "^27.0.6",
"ts-jest": "^27.0.4",

笑话配置

"jest": {
"runner": "@jest-runner/electron",
"testEnvironment": "@jest-runner/electron/environment",
"moduleNameMapper": {
"\.(jpg|jpeg|png|gif|svg|yaml)$": "/mocks/fileMock.js",
"\.(css|less|scss)$": "/mocks/styleMock.js"
},
"modulePaths": [
"/src/",
"node_modules"
],
"preset": "ts-jest",
"transform": {
"^.+\.(ts|tsx)?$": "ts-jest",
"^.+\.(js|jsx)$": "babel-jest"
}
},

Babelrc

{
"presets": ["@babel/preset-env", "@babel/preset-react", "@babel/preset-typescript"],
"plugins": [["@babel/plugin-proposal-decorators", { "legacy": true }],["@babel/plugin-proposal-class-properties", { "loose": true }]]
}

这个玩笑在最近的升级中看起来很糟糕,并且没有针对特定问题的适当修复。每一个修复都会导致一个新的错误打开。

到目前为止,已经发现了许多错误。

  • 导入错误 - Jest 无法导入
  • 无法读取未定义的属性“app”
  • TypeError: Jest: a transform must export a process function.
  • TypeError:无法读取未定义的属性“绑定”

环境:

节点版本:v12.17.0 NPM 版本:6.14.4

什么是正确的配置,我缺少什么?

runtime.setGlobalsForRuntime 未定义,因此bind 也是未定义。我们应该设置setGlobalsForRuntime 还是因为缺少一些配置?

【问题讨论】:

  • 您找到解决方案了吗?我有同样的问题。虽然使用 react 16.8.6 - 所以不要认为这是下面提到的问题。
  • @Martin - 如果您已全局安装 jest-cli,请删除它。尝试仅使用 npm run test 而不是直接从本地依赖项运行 jest。通过这个我摆脱了这个问题

标签: javascript node.js reactjs jestjs


【解决方案1】:

我卸载了全局 27+ 版本的 jest-cli,并尝试仅从本地依赖项运行,它现在对我有用。

在常用组件的开玩笑设置中配置了 moduleNameWrapper

"moduleNameMapper": {
  "\\.(jpg|jpeg|png|gif|svg|yaml)$": "<rootDir>/__mocks__/fileMock.js",
  "\\.(css|less|scss)$": "<rootDir>/__mocks__/styleMock.js",
  "^common/(.*)$": "<rootDir>/src/common/$1"
},

Babel 配置修改如下:

module.exports = {
  "presets": ["@babel/preset-env", "@babel/preset-react", "@babel/preset-typescript"],
  "plugins": ["const-enum", ["@babel/plugin-proposal-decorators", { "legacy": true }],
    ["@babel/plugin-proposal-class-properties", { "loose": true }],
    ["@babel/plugin-transform-runtime", { "regenerator": true }],
    ["@babel/plugin-transform-modules-commonjs"],
    "@babel/plugin-syntax-jsx"]
};

已更新依赖项:

"ts-jest": "^26.0.0",
"jest": "26.0.1",

tsconfig

{
  "compilerOptions": {
    "outDir": "dist",
    "module": "CommonJS",
    //"module": "commonjs",
    "target": "ES6",
    "lib": ["ESNext", "DOM", "DOM.Iterable"],
    "moduleResolution": "Node",
    "declaration": false,
    "strict": false,
    "noImplicitAny": false,
    "noUnusedLocals": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "experimentalDecorators": true,
    "jsx": "react",
    "downlevelIteration": true,
    "importHelpers": true,
    "preserveConstEnums": true,
    "removeComments": false,
    "sourceMap": true,
    "baseUrl": "src",
    "resolveJsonModule": true,
    "paths": {
      "common/*": ["common/*"]
    }
  },
  "include": ["./*.ts", "./*.tsx", "src"],
  "exclude": ["node_modules", "dist", "*.js"]
}

【讨论】:

  • 看起来您已将 jest/ts-jest 降级到版本 26 - 似乎问题仅在使用 jest 版本 27 时出现
  • YES @Martin,降级作品也仔细观察我的设置变化
  • 它也在 react-native 中工作?? @MithunShreevatsa
  • @TusharKotecha - 未验证
【解决方案2】:

什么是反应版本?开玩笑还是不支持react17。

【讨论】:

  • "react": "17.0.1",哦,我明白了。那么这里的工作是什么?
猜你喜欢
  • 2021-11-07
  • 1970-01-01
  • 1970-01-01
  • 2022-06-16
  • 2020-12-23
  • 1970-01-01
  • 2021-03-28
  • 2019-05-05
  • 2020-06-09
相关资源
最近更新 更多