Cannot read property 'errors' of undefined in mutations

Hey, community! I updated packages(apollo/client, react-scripts, typescript, etc) to the latest versions in my project and got errors in mutations and it happens only in the testing. On the browser all ok and works as should.
Error:

Cannot read property 'errors' of undefined
      at new ApolloError (node_modules/@apollo/client/errors/index.js:31:28)
      at Object.error (node_modules/@apollo/client/core/QueryManager.js:135:83)
      at notifySubscription (node_modules/zen-observable/lib/Observable.js:140:18)
      at onNotify (node_modules/zen-observable/lib/Observable.js:179:3)
      at SubscriptionObserver.error (node_modules/zen-observable/lib/Observable.js:240:7)
      at node_modules/@apollo/client/utilities/observables/asyncMap.js:27:40

Mutation mock:

const mock: MockedResponse = {
  request: {
    query: MyMutation,
    variables: {
      something: 22,
    },
  },
  result: jest.fn(() => ({
    data: {
      somethingData: {
        something: 22,
      },
    },
  })),
};

...
<MockedProvider
  mocks={[mock]}
  addTypename={false}
  defaultOptions={{ watchQuery: { fetchPolicy: 'no-cache' } }}
>
    <Component />
</MockedProvider>

Maybe anyone has ideas what can be wrong🤷🏻‍♂️

@artsmi what happens if you remove the jest.fn call so you’re storing the data object directly?

  result: {
    data: {
      somethingData: {
        something: 22,
      },
    },
  },

It works, but I need to test the success mutation state :frowning:
Previously I checked it like this:

await waitQueries();
expect(switchStoreMock.result).toBeCalled();

I think I can check success in other way, like this:

let isSuccess = false;
const mock: MockedResponse = {
  request: {
    query: MyMutation,
    variables: {
      something: 22,
    },
  },
  result: () => {
    isSuccess = true;

    return {
      data: {
        somethingData: {
          something: 22,
        },
      },
    }
  },
};

...
    await waitQueries();
    expect(isSuccess).toBe(true);

But it looks not cool :smiley: and also interesting what was changed

Resolved!
Added to the package.json:

"jest": {
  "resetMocks": false
},
1 Like

Solved