enzyme icon indicating copy to clipboard operation
enzyme copied to clipboard

TypeError: Cannot read property 'contextTypes' of undefined

Open emptyOne322 opened this issue 9 years ago • 14 comments

Hi, I added enzyme into my test and tried to do some simple test describe('Component: SliderField', () =>{

const minProps = {
    input: {
        name:"name",
        value:0,
        onChange: ()=>{},
        meta: {

        },
        min: 0,
        max: 100
    },
};

it('renders', () => {
    const wrapper = shallow(<SliderField {...minProps}/>)
    expect(wrapper.length).toEqual(1);
});

}) but it returned TypeError: Cannot read property "contextTypes" of undefined. I'm using mocha, webpack to build tests and webpack-dev-server to runing them in browser . So can someone help me or give some advices what can be wrong here?

emptyOne322 avatar Dec 15 '16 15:12 emptyOne322

I'm guessing your SliderField is expecting some context to be defined on it. You can pass context into a renderer via the second argument. See the documentation here: https://github.com/airbnb/enzyme/blob/master/docs/api/shallow.md#shallownode-options--shallowwrapper

shallow(<SliderField {...minProps} />, { context: {...} });

blainekasten avatar Dec 15 '16 16:12 blainekasten

I'm going to close this out as it is not an issue with Enzyme itself. Feel free to keep discussing though if you need more support.

blainekasten avatar Dec 15 '16 16:12 blainekasten

So can u give me qiuck explanation what context should be like?

emptyOne322 avatar Dec 16 '16 09:12 emptyOne322

Going to leave a note here in case others come across this issue in a search. This error seems to happen if you import {Component} from '<path>';
when you should have
import Component from '<path>';

jskeate avatar Mar 06 '17 16:03 jskeate

If you use eslint-plugin-import (which the airbnb eslint config uses) then you'd get a linting error if you imported the wrong thing ;-)

ljharb avatar Mar 06 '17 17:03 ljharb

import/named and import/default are off in the airbnb eslint config, it seems.

jskeate avatar Mar 06 '17 18:03 jskeate

Touché :-) they'll be turned on eventually.

ljharb avatar Mar 06 '17 19:03 ljharb

Thank you so much @jskeate that saved me so much time

ruthmpardee avatar Jun 26 '17 19:06 ruthmpardee

Ja! Thank you @jskeate - much appreciated.

ghost avatar Aug 18 '17 00:08 ghost

@jskeate Thanks.

AKNiazi avatar Jan 25 '18 12:01 AKNiazi

@jskeate some heroes don't use cape!

lucasdeassis avatar Apr 05 '18 13:04 lucasdeassis

In my case was similar but the error was on trying to access the wrapped component when it wasn't wrapped at all.

To make the point even clearer, test for components using the connect() HOC looks like this:

import _MyComponent from './MyComponent';
const MyComponent = _MyComponent.WrappedComponent;
// then used such as <MyComponent ...props />

This time the tested component was not using the HOC and not wrapped. So I changed the usage to be:

import MyComponent from './MyComponent';
// then used such as <MyComponent ...props />

luciomartinez avatar Aug 20 '18 20:08 luciomartinez

@jskeate danke! <3

mwlalata avatar Apr 08 '20 08:04 mwlalata

Going to leave a note here in case others come across this issue in a search. This error seems to happen if you import {Component} from '<path>'; when you should have import Component from '<path>';

If this doesn't solve the issue, make sure that you're actually exporting the component that you are trying to import.

gecersiznick avatar Jun 14 '20 07:06 gecersiznick