apollographql / apollo-client

:rocket:  A fully-featured, production ready caching GraphQL client for every UI framework and GraphQL server.
https://apollographql.com/client
MIT License
19.38k stars 2.66k forks source link

MockedResponse variableMatcher shows warning about no more mocked responses for the query #11935

Closed jci-aws closed 4 months ago

jci-aws commented 4 months ago

Issue Description

When testing with MockedProvider, I have provided a variableMatcher to one of my MockedResponses. When running the test, the tests still pass but there is a console warning about the queries not matching:

No more mocked responses for the query: query GetDog($name: String) {
  dog(name: $name) {
    id
    name
    breed
  }
}
Expected variables: {"name":"Buck"}

Failed to match 1 mock for this query. The mocked response had the following variables:
  {}

This typically indicates a configuration error in your mocks setup, usually due to a typo or mismatched variable. 
    in Dog 

Since this test query is being matched through variableMatcher, I would not expect this warning to appear.

Link to Reproduction

https://codesandbox.io/s/sharp-butterfly-fkxn55?file=/src/dog.test.js:258-292

Reproduction Steps

Run tests in CodeSandbox, see warning message in console tab.

@apollo/client version

3.10.8

jerelmiller commented 4 months ago

Hey @jci-aws 👋

It looks like you're configuring variableMatcher on the request key, but this is not correct. This should be placed as a top-level key in the mock as specified in the documentation. If I move the variableMatcher there, I see that the warning goes away:

const mocks = [
  {
    request: {
      query: GET_DOG_QUERY,
    },
    variableMatcher: () => true,
    result: {
      data: {
        dog: { id: "1", name: "Buck", breed: "bulldog" },
      },
    },
  },
];

It looks like your test passes in the sandbox because you're only checking the loading state of the component, but don't check against data or error. If I log the loading, data and error values of your Dog component used for the test, I see that error is also set to the same message as the warning you're seeing in the console once its finished loading.

Hope this helps!

jci-aws commented 4 months ago

Thought I was going crazy. Thanks!

github-actions[bot] commented 4 months ago

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better.

jerelmiller commented 4 months ago

No problem! Sometimes an outside perspective is all thats needed 😆. Glad its working for you! Have a great day.

github-actions[bot] commented 3 months ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. For general questions, we recommend using StackOverflow or our discord server.