How to test refetchQueries in React using MockProvider?

In my react code, I invoked a createUser query with refetchQueries like this:

 const result = await createUser({
            awaitRefetchQueries: true,
            refetchQueries: ['getUser'],
            variables: {
              input: {
                  ...
              },
            },
          });

My test set up looks like this:

    const mocks: ReadonlyArray<MockedResponse> = [
      {
        request: {
          query: CreateUserDocument,
          variables: {
            input: {
                ...
            },
          },
        },
        result: {
          data: {
                ...
          },
        },
      },
    ];

When I ran the test, I received an error in the console:

Unknown query named "getUser" requested in refetchQueries options.include array

I searched on the internet, and some people suggested using the newData attribute for refetchQueries, so I added this newData attribute to mocks:

    const mocks: ReadonlyArray<MockedResponse> = [
      {
        request: {
          query: CreateUserDocument,
          variables: {
            input: {
                ...
            },
          },
        },
        result: {
          data: {
                ...
          },
        },
        newData: () => { // should I add parameters here since getUser requires user's ID?
            return {
                data: {
                       ... // mock data from the getUser() query
                }
            }
        }
      },
    ];

And I am still receiving the same error in console.

My questions are:

  • What is wrong/missing with my setup?
  • If you have been testing query calls that have refetchQueries, can you provide a full working example of the mock setup to show how refetchQuery and data should be mocked in MockProvider’s mock?

Thank you very much!