Cannot read property 'call' of undefined for configure adapter for Jest
See original GitHub issueThanks 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:
- Created 5 years ago
- Comments:11 (6 by maintainers)
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:
to this:
…but shallow-wrapping in my test (as below) kept failing with the mentioned error message
HOWEVER, then I realized that I had another, unnecessary export, before my class export in
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!@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.