Jest TypeError: Cannot read property ‘bind’ of undefined in JestAdapter

Solution for Jest TypeError: Cannot read property ‘bind’ of undefined in JestAdapter
is Given Below:

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)

Steps to reproduce the behavior:

Dependencies

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

Jest configuration

"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 }]]
}

The jest looks broken with latest upgrades and there is no proper fix on a particular issue. Every fix leading to one more bug open.

So far these many errors have been identified.

  • Import Errors – Jest not able to import
  • Cannot read property ‘app’ of undefined
  • TypeError: Jest: a transform must export a process function.
  • TypeError: Cannot read property ‘bind’ of undefined

Environment:

Node version: v12.17.0
NPM version: 6.14.4

What is the proper configuration and what am i missing?

runtime.setGlobalsForRuntime is undefined hence bind is also undefined. Should we set setGlobalsForRuntime or is this because of some missing config?

I uninstalled jest-cli which was 27+ version globally and tried to run from the local dependencies only, it is working for me now.

Configured the moduleNameWrapper in jest settings for common components

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

Babel config modified as below:

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"]
};

Dependencies updated:

"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"]
}

What’s the react version? jest still not support react17.