enzymejs / enzyme

JavaScript Testing utilities for React
https://enzymejs.github.io/enzyme/
MIT License
19.96k stars 2.01k forks source link

TypeError: Cannot read property 'contextTypes' of undefined when using shallow #2536

Closed mcastrigno closed 3 years ago

mcastrigno commented 3 years ago

When I make a shallow copy of a component that is exported as: export default connect(mapStateToProps)(CompensationPlansDashboard);

I get the error TypeError: Cannot read property 'contextTypes' of undefined

  13 |     };
  14 |       
> 15 |     const sut = shallow(<CompensationPlansDashboard {...props} />);
     |                 ^
  16 | 
  17 |     expect(sut).toMatchSnapshot();
  18 |   });

If this export does not work for shallow, how can I fix it and still use Redux connect function? TIA

ljharb commented 3 years ago

What's the rest of the stack trace from the error?

mcastrigno commented 3 years ago
TypeError: Cannot read property 'contextTypes' of undefined

  13 |     };
  14 |       
> 15 |     const sut = shallow(<CompensationPlansDashboard {...props} />);
     |                 ^
  16 | 
  17 |     expect(sut).toMatchSnapshot();
  18 |   });

```

at Object.render (node_modules/enzyme-adapter-react-16/src/ReactSixteenAdapter.js:626:27) at new ShallowWrapper (node_modules/enzyme/src/ShallowWrapper.js:397:7) at shallow (node_modules/enzyme/src/shallow.js:10:10) at Object. (compensationPlansDashboard/compensationPlansDashboard.test.tsx:15:17)``

ljharb commented 3 years ago

How are you importing CompensationPlansDashboard? Can you add a console.log(CompensationPlansDashboard) before the shallow call?

mcastrigno commented 3 years ago
import React from "react";
import { shallow } from "enzyme";
import CompensationPlansDashboard from "./CompensationPlansDashboard";

// Mock higher order components (HOCs)
jest.mock("./compensationPlans/CompensationPlans", () => "CompensationPlans");

describe("<CompensationPlansDashboard /> rendering", () => {
  it("matches snapshot", () => {
    const props = {
      compensationPlans: [],
      isFetching: true,
    };

    const sut = shallow(<CompensationPlansDashboard {...props} />);

    expect(sut).toMatchSnapshot();
  });
});
ljharb commented 3 years ago

Pretty sure your mock is broken. It needs to be this:

jest.mock("./compensationPlans/CompensationPlans", () => { default: "CompensationPlans" });

or something like that.

However, you shouldn't BE mocking HOCs. You should keep them, and do one .dive() per HOC.

mcastrigno commented 3 years ago

that is giving me an error "Declaration or statement expected.ts(1128)" under default

ljharb commented 3 years ago

What happens if you comment out/remove the mock?

mcastrigno commented 3 years ago

same error

ljharb commented 3 years ago

and the console.log(CompensationPlansDashboard)? does that log a component, or undefined?

mcastrigno commented 3 years ago

undefined

mcastrigno commented 3 years ago

some kind of import or export problem

mcastrigno commented 3 years ago

or both

ljharb commented 3 years ago

In that case, it’s not an enzyme problem - it’s something with your jest or transpilation setup.

ljharb commented 3 years ago

Happy to help you work through it here, I’m not just sure where else to look

mcastrigno commented 3 years ago

Yeah, not sure either. Don't have a lot experience with jest. I am modifying a existing test after refactoring a component that was much more complicated. The prior test mocked a long list of HOCs that I factored out of the component.