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

Created on 9 Mar 2017  ·  30Comments  ·  Source: facebook/jest

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.

The dev dependency in package.json is
_"devDependencies": {
"babel-core": "^6.17.0",
"babel-jest": "^18.0.0",
"babel-loader": "^6.2.5",
"babel-plugin-transform-decorators": "^6.13.0",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-preset-es2017": "^6.16.0",
"babel-preset-react": "^6.16.0",
"babel-preset-stage-0": "^6.16.0",
"enzyme": "^2.7.1",
"eslint": "^3.12.1",
"eslint-config-rallycoding": "^3.1.0",
"jest": " * ",
"jest-cli": "18.1.0",
"jest-react-native": "16.0.0",
"react-addons-test-utils": "^15.4.2",
"react-dom": "^15.4.2",
"react-test-renderer": "*"
}_

Most helpful comment

I just realized why this is happening... Because I'm manually including Jest inside a create-react-app project. Was hoping I could have my cake and eat it too ;-) Looks like I'll have to wait for CRA to update Jest. Thanks!

All 30 comments

I remember having similar issue when using different versions of Jest linked to local copy, which was somehow coupled with testEnvironment config option. Can you try to remove it if present (defaults to jsdom)? That worked for me back then.

Why is your Jest dependency empty? You only need one, either jest or jest-cli.

I just ran into this as well. It appears that when the testEnvironment is set to jest-environment-jsdom, the this._environment.moduleMocker does not have a fn property (either on itself or the prototype). At least that's what I uncovered while looking into the issue. This is not a problem with v18 of Jest.

Note that the jest-environment-node works just fine.

Are you sure you have the latest versions of all Jest packages of version 19? This might break if you have some Jest 18 and some Jest 19 packages.

I'm not sure why this is installing v18 of the jest-environment packages, but it is, and that's the problem. Workaround is to add those to my package.json, but... :-/

Oh wow, this is not great. Every single one of those packages should be 19.x. Would you mind inspecting the "dependencies" fields in some of those packages and see if they point to 18.x? That may point to an issue with lerna when publishing.

Weird. I just created a new project with _only_ babel-jest and jest and the versions are all 19.x. So there's probably something else up in my project. Looking.

I just realized why this is happening... Because I'm manually including Jest inside a create-react-app project. Was hoping I could have my cake and eat it too ;-) Looks like I'll have to wait for CRA to update Jest. Thanks!

Upgrading jest-cli from from 18 to 19 solved the issue.

When I upgrade node from 5.12.0 to 6.10.2, this error is disappeared.

I still get this with running a build on circle.

package.json:

{
    "name": "frontend",
    "version": "0.1.0",
    "private": true,
    "dependencies": {
        "auth0-lock": "^10.14.0",
        "babel-cli": "^6.24.1",
        "babel-jest": "^19.0.0",
        "babel-plugin-syntax-flow": "^6.18.0",
        "jest": "^19.0.2",
        "lodash": "^4.17.4",
        "moment": "^2.18.1",
        "moments": "^0.0.2",
        "proxyquire": "^1.7.11",
        "react": "^15.5.4",
        "react-breadcrumbs": "^1.6.6",
        "react-datetime": "^2.8.9",
        "react-dom": "^15.5.4",
        "react-dropzone": "^3.13.1",
        "react-redux": "^5.0.4",
        "react-router": "^4.1.1",
        "react-router-dom": "^4.1.1",
        "react-router-redux": "^5.0.0-alpha.6",
        "redux": "^3.6.0",
        "redux-devtools": "^3.3.2",
        "redux-form": "^6.6.3",
        "redux-localstorage": "^0.4.1",
        "redux-thunk": "^2.2.0",
        "semantic-ui-react": "^0.68.0",
        "sinon": "^2.2.0",
        "superagent": "^3.5.2"
    },
    "devDependencies": {
        "@kadira/storybook": "^2.21.0",
        "faker": "^4.1.0",
        "flow-bin": "^0.44.2",
        "react-scripts": "0.9.5"
    },
    "scripts": {
        "start": "REACT_APP_URL=http://localhost:3001 react-scripts start",
        "build": "REACT_APP_URL=<REDACTED> react-scripts build",
        "test": "react-scripts test --env=jsdom",
        "eject": "react-scripts eject",
        "flow": "flow",
        "storybook": "start-storybook -p 9009 -s public",
        "build-storybook": "build-storybook -s public"
    }
}

circle:

machine:
    node:
        version: 6.10.3

test:
    override:
        - CI=true npm test

dependencies:
    override:
        - yarn --ignore-engines

deployment:
    master:
        branch: master
        commands:
            - firebase deploy --token "$FIREBASE_DEPLOY_TOKEN" --non-interactive

CircleCI output:

$ CI=true npm test

> [email protected] test /home/ubuntu/<REDACTED>
> react-scripts test --env=jsdom

 FAIL  src/reducers/transactions/reducer.test.js
  ● Test suite failed to run

    TypeError: Cannot read property 'bind' of undefined

      at Runtime._createRuntimeFor (node_modules/jest-runtime/build/index.js:640:37)
      at handle (node_modules/worker-farm/lib/child/index.js:41:8)
      at process.<anonymous> (node_modules/worker-farm/lib/child/index.js:47:3)
      at emitTwo (events.js:106:13)
      at process.emit (events.js:191:7)

 FAIL  src/reducers/contacts/reducer.test.js
  ● Test suite failed to run

    TypeError: Cannot read property 'bind' of undefined

      at Runtime._createRuntimeFor (node_modules/jest-runtime/build/index.js:640:37)
      at handle (node_modules/worker-farm/lib/child/index.js:41:8)
      at process.<anonymous> (node_modules/worker-farm/lib/child/index.js:47:3)
      at emitTwo (events.js:106:13)
      at process.emit (events.js:191:7)

....

Test Suites: 9 failed, 9 total
Tests:       0 total
Snapshots:   0 total
Time:        11.745s
Ran all test suites.
npm ERR! Test failed.  See above for more details.

CI=true npm test returned exit code 1

@natdm not sure if this will help or why it worked for me, but I blew away node modules and re-installed and the error went away.

I had the same error with a repo where I'm using Lerna and found this thread, while looking for a solution. It turned out that one old app was still using Jest v18, the other v21 and tests were failing with this bind error in the app with Jest v21.

I think that Lerna is supposed to somehow magically solve such problems, but I managed to fix it only when I updated Jest in both apps to v21.

I just solved the same issue with downgrading Node version to 8.9.4

I've had this error even having default env(jdom), solved installing
npm install --save-dev babel-jest regenerator-runtime

My package.json has the same version of jest-cli as babel-jest at 22.4.3. When I run my page.test.jsx, I get a really long output:

 FAIL  __tests__/layout/page.test.jsx (23.231s)
  PageLayout Component
    ✕ should render home route (135ms)
    ✕ should render login route (20ms)
    ✕ should render logout route (7ms)
    ✕ should render register route (26ms)
    ✕ should render analysis route (13ms)

  ● PageLayout Component › should render home route

    TypeError: Cannot read property 'bind' of undefined

      17 | const mapDispatchToProps = (dispatch) => {
      18 |     return {
    > 19 |         dispatchLayout: dispatch.bind(setLayout)
      20 |     }
      21 | }
      22 |

      at Function.mapDispatchToProps [as mapToProps] (import/redux/container/home-page.jsx:19:34)
      at mapToPropsProxy (node_modules/react-redux/lib/connect/wrapMapToProps.js:54:92)
      at Function.detectFactoryAndVerify (node_modules/react-redux/lib/connect/wrapMapToProps.js:63:19)
      at mapToPropsProxy (node_modules/react-redux/lib/connect/wrapMapToProps.js:54:46)
      at handleFirstCall (node_modules/react-redux/lib/connect/selectorFactory.js:38:21)
      at pureFinalPropsSelector (node_modules/react-redux/lib/connect/selectorFactory.js:85:81)
      at Object.runComponentSelector [as run] (node_modules/react-redux/lib/components/connectAdvanced.js:43:25)
      at Connect.initSelector (node_modules/react-redux/lib/components/connectAdvanced.js:195:23)
      at new Connect (node_modules/react-redux/lib/components/connectAdvanced.js:136:15)
      at constructClassInstance (node_modules/react-dom/cjs/react-dom.development.js:6801:20)
      at updateClassComponent (node_modules/react-dom/cjs/react-dom.development.js:8336:9)
      at beginWork (node_modules/react-dom/cjs/react-dom.development.js:8982:16)
      at performUnitOfWork (node_modules/react-dom/cjs/react-dom.development.js:11814:16)
      at workLoop (node_modules/react-dom/cjs/react-dom.development.js:11843:26)
      at renderRoot (node_modules/react-dom/cjs/react-dom.development.js:11874:9)
      at performWorkOnRoot (node_modules/react-dom/cjs/react-dom.development.js:12449:24)
      at performWork (node_modules/react-dom/cjs/react-dom.development.js:12370:9)
      at performSyncWork (node_modules/react-dom/cjs/react-dom.development.js:12347:5)
      at requestWork (node_modules/react-dom/cjs/react-dom.development.js:12247:7)
      at scheduleWorkImpl (node_modules/react-dom/cjs/react-dom.development.js:12122:13)
      at scheduleWork (node_modules/react-dom/cjs/react-dom.development.js:12082:12)
      at scheduleRootUpdate (node_modules/react-dom/cjs/react-dom.development.js:12710:5)
      at updateContainerAtExpirationTime (node_modules/react-dom/cjs/react-dom.development.js:12738:12)
      at Object.updateContainer (node_modules/react-dom/cjs/react-dom.development.js:12765:14)
      at ReactRoot.Object.<anonymous>.ReactRoot.render (node_modules/react-dom/cjs/react-dom.development.js:16069:15)
      at node_modules/react-dom/cjs/react-dom.development.js:16488:14
      at Object.unbatchedUpdates (node_modules/react-dom/cjs/react-dom.development.js:12557:12)
      at legacyRenderSubtreeIntoContainer (node_modules/react-dom/cjs/react-dom.development.js:16484:17)
      at Object.render (node_modules/react-dom/cjs/react-dom.development.js:16543:12)
      at Object.render (node_modules/enzyme-adapter-react-16/build/ReactSixteenAdapter.js:218:50)
      at new ReactWrapper (node_modules/enzyme/build/ReactWrapper.js:98:16)
      at mount (node_modules/enzyme/build/mount.js:19:10)
      at Object.<anonymous> (__tests__/layout/page.test.jsx:24:25)

The results repeat the above, for each it() test. Am I supposed to pass in my redux properties in the store for the <Provider>? The HomePageState is a connected redux container to the HomePage component. So, I'm curious if I have to pass the properties associated with the dispatchLayout from the <Provider>, or the <MainRoute> of the page.test.jsx?

Note: if anyone is interested, the full issue for my above problem can be further reviewed.

Hey @jeff1evesque, in your case I think that dispatch is really undefined

when running my tests I get an error Test suite failed to run, TypeError: cannot read property myProperty of undefined I have followed this to install jest https://medium.com/react-native-training/learning-to-test-react-native-with-jest-part-1-f782c4e30101 and I am on react-native. What is wrong with how my package.json is that it isn't working?

{
  "name": "CoffeeApp",
  "version": "0.1.0",
  "private": true,
  "devDependencies": {
    "babel-jest": "^23.0.0",
    "babel-preset-react-native-stage-0": "^1.0.1",
    "jest": "^23.0.0",
    "react-test-renderer": "^16.4.0"
  },
  "scripts": {
    "start": "react-native start",
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "test": "jest"
  },
  "jest": {
    "preset": "react-native"
  },
  "dependencies": {
    "prop-types": "^15.6.1",
    "query-string": "^6.0.0",
    "react": "16.2.0",
    "react-native": "0.52.0",
    "react-native-elements": "^1.0.0-beta4",
    "react-native-gesture-handler": "^1.0.0",
    "react-native-mixpanel": "0.0.16",
    "react-native-star-rating": "^1.0.9",
    "react-native-tab-view": "^0.0.78",
    "react-native-vector-icons": "^4.6.0",
    "react-navigation": "^1.5.11",
    "react-redux": "^5.0.7",
    "redux": "^3.7.2",
    "redux-persist": "^5.9.1",
    "tipsi-stripe": "^5.2.0"
  }
}

Thanks!

Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

I had this issue because my project did no explicitly depend on jest-cli, but because it was a dependency of jest, it was installed. When I installed another package (react-codemod) with a dependency on an older version of jest-cli, yarn chose the older one. I solved the problem by adding an explicity dependency in my project on the version of jest-cli matching the version of jest in my project.

wat, react-codemod depends on Jest? That has to be a bug

It's a bit strange, but it shouldn't matter. Maybe jest should not depend on jest-cli, so that users are forced to add an explicit dependency to their project (at the correct version) if they want to use it?

Including the appropriate peerDependencies should at least provide a warning of this kind of problem. (E.g. jest-cli should have a peer dependency on a version of jest that works correctly with it)

jest had a bug previously, fixed in newer versions. react-codemod` depends on a version of jest almost 2 years old. It shouldn't (it's a dev dependency though, so the version is really orthogonal)

I think this is a more specific case of https://github.com/facebook/jest/issues/5913. In my case, after installing react-codemod I had to add explicit dependencies to my project for jest-cli and jest-environment-jsdom for my jest setup to continue to work.

@Harishn28 @cpenarrieta @szimek @natdm @kentcdodds @cpojer I'm still getting this bind issue with jest version 22

still getting same error

Same error here...

图片

You have a different error. Your stack trace is almost entirely from ts-jest, I suggest to open up an issue there

Was this page helpful?
0 / 5 - 0 ratings