TypeError: Cannot read property 'contextTypes' of undefined

Juan Camilo Hernández Moreno picture Juan Camilo Hernández Moreno · Sep 30, 2016 · Viewed 21.1k times · Source

I'm trying to test a React-app with Jest. I use Enzyme's shallow to render my App.js component in App-test-js but I'm getting this error: TypeError: Cannot read property 'contextTypes' of undefined

This is my App.js:

and this is my App-test.js:

import React from 'react'
import { shallow } from  'enzyme'
import App from '../components/App.js'

describe( '<App />', () => {
  it('Button disable when input is empty', () => {
    const App = shallow(<App />);

    expect(App.find('.my-button').hasClass('disabled')).to.equal(true);

  });

});

And this the error when I run npm test:

Terminal screenshot

This is my first time with testing in jest, please could someone help me with any idea about this error?

Answer

sanrodari picture sanrodari · Oct 1, 2016

The problem here is that you are redefining the the app component with the result of the shallow call

//    Redefining
//    ↓
const App = shallow(<App />);

The solution would be to use a different name:

//    Use a different name
//    ↓
const app = shallow(<App />);