Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jci-aws opened this issue Jul 8, 2024 · 5 comments

Comments

@jci-aws
Copy link

jci-aws commented Jul 8, 2024

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
Copy link
Member

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
Copy link
Author

jci-aws commented Jul 8, 2024

Thought I was going crazy. Thanks!

@jci-aws jci-aws closed this as completed Jul 8, 2024
Copy link
Contributor

github-actions bot commented Jul 8, 2024

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
Copy link
Member

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

Copy link
Contributor

github-actions bot commented Aug 8, 2024

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 8, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants