React-apollo: Providing a custom cache to MockedProvider fails

Created on 5 Dec 2019  ·  2Comments  ·  Source: apollographql/react-apollo

Intended outcome:

I want to test that the cache has actually been updated after running a mutation. To do this I need to pass in my own cache object to MockedProvider

Actual outcome:

I get the following error when I pass in a cache prop:

import { InMemoryCache } from "apollo-cache-inmemory";
import { render } from "@testing-library/react";
import { MemoryRouter } from "react-router-dom";

const cache = new InMemoryCache();

test("foo", () => {
  const mocks = [...]
  render(
    <MockedProvider mocks={mocks} addTypename={false} cache={cache}>
      <MemoryRouter initialEntries={["/"]}>
        <MyComponent />
      </MemoryRouter>
    </MockedProvider>
  )

  const res = cache.readQuery(...);
  expect(res).toEqual(...);
})
Error!
          Network error: No more mocked responses for the query

Note that this error doesn't occur when I don't pass in a cache prop.

How to reproduce the issue:

Pass in a cache prop to MockedProvider

Version

    "@apollo/react-hooks": "^3.1.3",
    "apollo-cache-inmemory": "^1.6.2",
    "@apollo/react-testing": "^3.1.3",
  System:
    OS: macOS 10.15.1
  Binaries:
    Node: 10.15.1 - /usr/local/bin/node
    Yarn: 1.15.2 - /usr/local/bin/yarn
    npm: 6.4.1 - /usr/local/bin/npm
  Browsers:
    Chrome: 78.0.3904.108
    Firefox: 70.0
    Safari: 13.0.3

Most helpful comment

Actually, I fixed it by

const cache = new InMemoryCache({ addTypename: false });
 <MockedProvider mocks={mocks} addTypename={false} cache={cache}>
  ...
</MockedProvider>

Note that addTypename must align between the InMemoryCache and MockedProvider.

All 2 comments

Actually, I fixed it by

const cache = new InMemoryCache({ addTypename: false });
 <MockedProvider mocks={mocks} addTypename={false} cache={cache}>
  ...
</MockedProvider>

Note that addTypename must align between the InMemoryCache and MockedProvider.

Is this in the docs? If not, it should be.

Was this page helpful?
0 / 5 - 0 ratings