question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot read property 'call' of undefined for configure adapter for Jest

See original GitHub issue

Thanks for reporting an issue to us! We’re glad you are using and invested in Enzyme. Before submitting, please read over our commonly reported issues to prevent duplicates!

All common issues

  • common issues

Notoriously common issues

  • Webpack build issues
  • Cannot find module ‘react-dom/lib/ReactTestUtils’
  • Query Selector fails
  • Testing third party libraries

If you haven’t found any duplicated issues, please report it with your environment!

Current behavior

● Test suite failed to run

We’ve recently updated a lot of packages(including jest, react etc.) but not enzyme. However this bug start appearing in the test setup file while it has not been changed.

    TypeError: Cannot read property 'call' of undefined

      6 | configure({ adapter: new Adapter()});
      7 |

      at forEach (node_modules/foreach/index.js:7:27)
      at defineProperties (node_modules/define-properties/index.js:52:19)
      at Object.<anonymous> (node_modules/object.assign/index.js:11:27)
      at Object.<anonymous> (node_modules/enzyme/build/ReactWrapper.js:23:15)
      at Object.<anonymous> (node_modules/enzyme/build/index.js:3:21)
      at Object.<anonymous> (test/config/setup-test.js:8:15)

This is the setup-test file.

/* Setup Enzyme with appropriate react adapter for testing */
import {configure} from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';


configure({ adapter: new Adapter()});

I logged all the way down at /foreach/index.js where the call fails.

var hasOwn = Object.prototype.hasOwnProperty;
var toString = Object.prototype.toString;

module.exports = function forEach (obj, fn, ctx) {
    if (toString.call(fn) !== '[object Function]') {

Weird thing is that toString is undefined, but hasOwn is a defined function.

This is our jest file

{
  "rootDir": "../../",
  "verbose": true,
  "setupFiles": ["<rootDir>/test/config/setup-test.js", "<rootDir>/test/config/setup-jsdom.js"],
  "testURL": "http://localhost/",
  "reporters": ["default", "jest-junit"],
  "roots": [
    "client",
    "test/unit/client"
  ],
  "moduleFileExtensions": [
    "js",
    "jsx"
  ],
  "moduleDirectories": [
    "node_modules"
  ],
  "modulePaths": [
    "client",
    "test/utils"
  ],
  "moduleNameMapper": {
    "\\.(css|less|scss)$": "<rootDir>/test/utils/empty-style-mock.js",
    "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/test/utils/empty-svg-mock.js"
  },
  "transformIgnorePatterns": [],
  "unmockedModulePathPatterns" : [
    "./node_modules/jasmine-reporters"
  ]
}

Expected behavior

Your environment

MAC OSX 10.13.6

API

  • shallow
  • mount
  • render

Version

enzyme: “3.3.0”, enzyme-adapter-react-16: “1.1.1” react: “16.4.1” node: “v8.11.1” jest: “23.1.0” jsdom: “11.12.0”

Adapter

  • enzyme-adapter-react-16
  • enzyme-adapter-react-15
  • enzyme-adapter-react-15.4
  • enzyme-adapter-react-14
  • enzyme-adapter-react-13
  • enzyme-adapter-react-helper
  • others ( )

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
jonasfridcommented, Nov 1, 2018

Not sure if it’s related, but I received a similar error (TypeError: Cannot read property 'prototype' of undefined) when using Enzyme + Jest. In the end, it turned out to have to do with how / what I exported from the component I was testing.

Simply put, the component was wired up via redux, but I wanted to shallow test it, so changed this:


class MyComponent extends Component {
  …
}
…
…
export default withRouter(connect(
  mapStateToProps,
  mapDispatchToProps
)(MyComponent))

to this:

export class MyComponent extends Component {
  …
}
…
…
export default withRouter(connect(
  mapStateToProps,
  mapDispatchToProps
)(MyComponent))

…but shallow-wrapping in my test (as below) kept failing with the mentioned error message

import {MyComponent as UnConnectedMyComponent} from '../question';
...
const wrapper = shallow(
  <UnConnectedMyComponent />
);

HOWEVER, then I realized that I had another, unnecessary export, before my class export in MyComponent:

export const myConstant = 'FOO'
…
…
export class MyComponent extends Component {
  …
}
…
…
export default withRouter(connect(
  mapStateToProps,
  mapDispatchToProps
)(MyComponent))

So, in the end, what was failing was that I duplicate exports from the component I was testing.

Not sure if this helps, but removing the export const myConstant = 'FOO' sorted my issue at least!

1reaction
ljharbcommented, Aug 17, 2018

@nicoxxie I’m going to close this, but I’m very interested in understanding why this is breaking for you, so it’d be great if you could follow up as you discover more.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot read property 'call' of undefined for configure adapter ...
Weird thing is that toString is undefined, but hasOwn is a defined function. This is our jest file. { "rootDir": "../../", "verbose": true,...
Read more >
Jest error -- Cannot read property 'get' of undefined
In the mock object that Jest creates for the module, every mock function returns undefined . You need to mock a return value...
Read more >
error cannot read property 'configurations' of undefined. react ...
This error typically occurs when you are trying to access an undefined property of an object in React Native. To resolve this error,...
Read more >
Configuring Jest
Jest will run .mjs and .js files with nearest package.json 's type field set to module as ECMAScript Modules.
Read more >
Continuous integration for React applications using Jest and ...
Next, you need to configure enzyme to use the adapter. react-scripts supports configuring testing tools in a src/setupTests.js file.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found