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

8

13

we were using jest from unit testing in react-native. It was working well. We just deleted npm and installed it again and then when we tried to run unit test(npm test) we are getting following error and we are not able to run test cases.

enter image description here

What might have went wrong.

Purapurblind answered 9/3, 2017 at 10:16 Comment(0)
P
2

Upgrading jest-cli version from 18 to 19 fixed the problem.

Issue in github repo of jest

Purapurblind answered 13/3, 2017 at 9:25 Comment(0)
P
4

Try to re-install both jest and jest babel last versions

npm install --save-dev jest

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

And re-run the test, it works for me

Plica answered 3/4, 2017 at 13:14 Comment(0)
P
2

Upgrading jest-cli version from 18 to 19 fixed the problem.

Issue in github repo of jest

Purapurblind answered 13/3, 2017 at 9:25 Comment(0)
C
1

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

Error was on jest 27+ version but after setting to 26 it works well.

Crop answered 15/12, 2021 at 12:28 Comment(0)
H
0

I had the same problem. I started with a couple of tests using only the jest, then I initialized folder with create-react-app on my another machine and restored previous tests. After that, I switched back to my current machine, did npm install and ran tests. It gave me this error, so I deleted node_modules and did npm install again. All the tests are green now.

Hypothetical answered 12/3, 2017 at 9:14 Comment(0)
T
0

I had the same error:

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

Similar to @Harish_N, but I was using react-scripts.

In my case was totally about updating the test related dependencies to a newer version in the package.json.

Tipi answered 28/4, 2017 at 10:34 Comment(0)
R
0

If you added this line of code in

global.requestAnimationFrame = function(callback) {
    setTimeout(callback, 0);
};

File called "shim.js" or "setupshim.js" in your "package.json":

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

Remove this file. This will help to remove this error in "Jest" Unit testing.

This works for me, hope this gonna work for you also.

Rosel answered 1/11, 2018 at 7:6 Comment(0)
D
0

add this to your code.

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

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

Delaunay answered 3/1, 2022 at 13:50 Comment(1)
While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.Felly
S
0

I was on the latest version of Jest when I got the same error. I eventually realized there was a circular dependency in my source code. Resolving the circular dependency made my tests work again.

The output was cryptic, but in hindsight the stack trace gave me the clues I needed to see that there was a circular dependency issue. The problem was happening on the dist/index file, then my source files, then my src/index file. Here is what the output looked like, for an example:

FAIL  src/smoke.test.js
  ● Test suite failed to run

TypeError: Cannot read properties of undefined (reading 'default')

  259 |   enumerable: true,
  260 |   get: function get() {
> 261 |     return _button["default"];
      |                      ^
  262 |   }
  263 | });
  264 | Object.defineProperty(exports, "Input", {

  at Object.get [as Button] (dist/index.js:261:22)
  at Object.RealButton (src/skeleton/InputSkeletons.js:45:57)
  at Object.require (src/skeleton/index.js:4:1)
  at Object.require (src/index.ts:45:1)
  at Object.require (src/setupTests.js:4:1)
Sarraute answered 18/8, 2023 at 12:23 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.