enzyme: Multiple HOC Shallow render error: Invariant Violation: Could not find "client" in the context or passed in as an option.

Shallow rendering on HOC Issue

When running tests on a component that is wrapped in multiple HOC’s with <MockedProvider/>, I am getting the following error once I dive down to what the tested component renders. Even with wrapping my component with <MockedProvider/> before diving, it seems to loose the MockedProvider context once I dive the necessary amount. <MyComponent/> uses the useQuery hook.

Example:

const mocks = {...};
describe('My component', () => {
  const props = {...};
  const elem = shallow(
    <MockedProvider mocks={mocks} addTypename={false}>
      <MyComponent {...props} />
    </MockedProvider>
  );

  test('it should match snapshot', () => {
    expect(
      // elem.dive().dive().dive() this gets me to <MyComponent/>
      elem.dive().dive().dive().dive() // this should match the snapshot of what <MyComponent/> renders, instead errors
    ).toMatchSnapshot();
  }); 
});

Three .dive() calls gets me to <MyComponent />, therefore a fourth call should get me to what <MyComponent/> renders – instead I get the following error:

‘Invariant Violation: Could not find “client” in the context or passed in as an option. Wrap the root component in an <ApolloProvider>, or pass an ApolloClient instance in via options.’

I have searched forums and read the existing issues including https://github.com/enzymejs/enzyme/issues/2369 but nothing seems to fix this error. I have tried to use the wrappingComponent option of shallow to pass in the <MockedProvider /> but the same issue occurs once I dive to the correct level.

Any help or potential solutions are appreciated! Thank you.

My Apollo/Enzyme/React environment

API

  • shallow
  • mount
  • render

Version

library version
enzyme 3.9
react 16.12
react-dom 16.12
react-test-renderer 16.8.6
adapter (below)

Adapter

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

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Comments: 15 (5 by maintainers)

Most upvoted comments

+1. I’m having the same issue.

I’ll get a repo going for you that replicates the issue. @ljharb