react-apollo icon indicating copy to clipboard operation
react-apollo copied to clipboard

Could not find "client" in the context of ApolloConsumer when using react-apollo 2.5.3 with MockedProvider

Open bkuzma opened this issue 7 years ago • 45 comments

Intended outcome:

I have a React component that makes use of <ApolloConsumer> in order to access its client render prop and explicitly call client.query when a user action occurs. In order to test my component in my Jest/Enzyme test suite, I wrap it in <MockedProvider> and mount it with Enzyme. My tests should run with no issue.

Example component:

import React, { Component } from "react";
import { graphql, ApolloConsumer } from "react-apollo";
import gql from "graphql-tag";

export const query = gql`
  query ErrorTemplate {
    people {
      id
      name
    }
  }
`;

class App extends Component {
  constructor(props) {
    super(props);

    this.state = {
      people: []
    };
  }

  render() {
    return (
      <ApolloConsumer>
        {(client) => (
          <main>
            <button onClick={(event) => {
              client.query({
                query
              }).then(response => {
                this.setState({ people: response.data.people });
              });
            }}>Fetch People</button>
            <ul>
              {this.state.people.map(person => <li key={person.id}>{person.name}</li>)}
            </ul>
          </main>
        )}
      </ApolloConsumer>
    );
  }
}

export default App;

Example tests:

import React from "react";
import { MockedProvider } from "react-apollo/test-utils";
import Enzyme, { mount } from "enzyme";
import Adapter from "enzyme-adapter-react-16";

Enzyme.configure({ adapter: new Adapter() });

import App, { query } from "./App";

const mock = {
  request: {
    query
  },
  result: {
    people: [
      { id: 1, name: 'John Smith' },
      { id: 2, name: 'Sara Smith' },
      { id: 3, name: 'Budd Deey' },
    ]
  }
};

describe("App", () => {
  it("mounts", () => {
    const app = mount(
      <MockedProvider addTypename={false} mocks={[mock]}>
        <App />
      </MockedProvider>
    );
  });
});

Actual outcome:

My test fails with a message saying: Invariant Violation: Could not find "client" in the context of ApolloConsumer. Wrap the root component in an <ApolloProvider>.

Screenshot: image

How to reproduce the issue:

Reproduction: https://github.com/bkuzma/react-apollo-error-template/tree/context-error-example

If you look at the commit history of the context-error-example branch, the single test passes in the second to last commit (412d2d57f52d2b10c1620dd2259bd8269eab74e5), but when I update react-apollo to 2.5.3 in the last commit (26474d6aab4cbdca7d9c08c9af386f4be8f0a2de), the test fails with the same error described above.

Version

bkuzma avatar Mar 25 '19 21:03 bkuzma

Hi there! Same case importing { withApollo } from react-apollo. Just updated to 2.5.3 this morning with the exact same output as @bkuzma. " Invariant Violation: Could not find "client" in the context of ApolloConsumer. Wrap the root component in an ..."

allezallez99 avatar Mar 25 '19 21:03 allezallez99

Yup, I see it too - not during test. Trying to upgrade to solve the ignored "network-only" fetching issue...

davewthompson avatar Mar 26 '19 00:03 davewthompson

I have the same issue with "react-apollo": "2.5.3" and I use react-testing-library for my tests.

quentincrinon avatar Mar 26 '19 10:03 quentincrinon

I'm looking into this, and should have a fix ready shortly. We're going to move React Apollo's testing utilities to a new package when react-apollo 3 lands, to help avoid issues like this (that are caused by rollup bundle differences). More details shortly - thanks!

hwillson avatar Mar 26 '19 15:03 hwillson

We got this too, and pinning "react-apollo": "2.5.2" solved the issue

craftninja avatar Mar 26 '19 17:03 craftninja

Still running into this using v2.5.5. I'm not using MockedProvider. I'm running a pretty plain react-native expo project but using Offix instead of plain apollo-client. Pinning to 2.5.2 works for now.

evelant avatar Apr 23 '19 00:04 evelant

I just tried upgrading to v2.5.5 and immediately started getting this error. Reverted back to v2.5.2 as other suggested and it solved the problem.

jgroh9 avatar Apr 25 '19 15:04 jgroh9

Hmm... I wonder if the other issues here are because in certain situations, the react-apollo used is not the .cjs version, which is what was used in the fix in #2907. E.g. if you're using the MockedProvider in a styleguide via webpack, the react-apollo that's bundled will be the .esm version, meaning the same issue rears its head in a different way.

jure avatar May 10 '19 13:05 jure

@jure I recall receiving errors when using the MockedProvider in Storybook.

jgroh9 avatar May 10 '19 13:05 jgroh9

This exact error also occurs on 2.5.5 when building a production version with webpack, non-production version works as expected. Pinning to v2.5.2 works for production builds as suggested by @jgroh9 . The production code doesn't include MockedProvider anywhere in the code.

simeonpashley avatar Jun 11 '19 15:06 simeonpashley

Also happening on 2.5.6

mpgon avatar Jun 12 '19 10:06 mpgon

@hwillson should another issue be opened for the most recent version (2.5.6), since this one is closed?

mpgon avatar Jun 18 '19 12:06 mpgon

Same, "downgrade" to 2.5.2 fixed this issue for me as well

pencilcheck avatar Jun 20 '19 23:06 pencilcheck

also happen in 3.0.1

linchen2chris avatar Aug 20 '19 06:08 linchen2chris

I have same problem 3.0.1

kazu69 avatar Aug 21 '19 18:08 kazu69

I have same problem in 3.0.1, downgrading to 3.0.0 resolves my problem

1nd avatar Aug 22 '19 16:08 1nd

Had the same problem at 2.5.8, upgrading to 3.0.0 fixed it.

radhasatam avatar Aug 22 '19 20:08 radhasatam

Echoing the same issue. I ran npm i to update some packages and started having this issue, ended up locking to 3.0.0 for now.

mikestecker avatar Aug 29 '19 23:08 mikestecker

It appears this issue also exists in 3.1.0.

connorjacobsen avatar Sep 09 '19 23:09 connorjacobsen

Hi, for people stuck like me: if you use [email protected], you also need to import MockedProvider from react-apollo/test-utils, otherwise you still get this issue. Conversely, if you use v3.x.x, you need to import MockedProvider from the newer @apollo/react-testing instead.

eric-burel avatar Sep 10 '19 08:09 eric-burel