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

Add expected/received variables to 'No more mocked responses' error message #8340

Merged

Conversation

markneub
Copy link
Contributor

@markneub markneub commented Jun 1, 2021

Updated version of abandoned PR #6963

Checklist:

  • If this PR is a new feature, please reference an issue where a consensus about the design was reached (not necessary for small changes)
  • Make sure all of the significant new logic is covered by tests

Motivation

When testing a request using the MockedProvider, sometimes a mocked response is not found because the variables provided do not match the mock. It can be hard to tell what the differences are — this PR prints out the expected variables and the found variables, including variables that are set to undefined (fixes #6771).

Sample Test Output

General use should error if the variables in the mock and component do not match

Error: No more mocked responses for the query: query GetUser($username: String!) {
  user(username: $username) {
    id
    __typename
  }
}

Expected variables:
  {"username":"mock_username","age":"undefined"}
Found 1 mock with variables:
  1: {"username":"mock_username"}

General use should error if the variables do not deep equal

Error: No more mocked responses for the query: query GetUser($username: String!) {
  user(username: $username) {
    id
    __typename
  }
}

Expected variables:
  {"username":"some_user","age":42}
Found 1 mock with variables:
  1: {"age":13,"username":"some_user"}

General use should error if the query in the mock and component do not match

Error: No more mocked responses for the query: query GetUser($username: String!) {
  user(username: $username) {
    id
    __typename
  }
}

Expected variables:
  {"username":"mock_username"}

@apollo-cla
Copy link

@markneub: Thank you for submitting a pull request! Before we can merge it, you'll need to sign the Apollo Contributor License Agreement here: https://contribute.apollographql.com/

@benjamn benjamn self-assigned this Jun 7, 2021
@benjamn benjamn requested review from benjamn, hwillson and brainkim June 7, 2021 17:03
@benjamn benjamn force-pushed the improve-mocked-responses-error branch from adf586c to 95f68ee Compare June 7, 2021 20:56
@markneub markneub requested a review from StephenBarlow as a code owner June 7, 2021 20:56
@benjamn benjamn changed the base branch from main to release-3.4 June 7, 2021 20:57
Copy link
Member

@benjamn benjamn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@markneub Looks good to me, with some minor error message formatting tweaks. You're going to make a lot of developers a lot less confused when their mock variables don't match. Thanks!

Comment on lines +45 to +48
Expected variables: {"username":"other_user","age":<undefined>}

Failed to match 1 mock for this query, which had the following variables:
{"username":"mock_username"}
Copy link
Member

@benjamn benjamn Jun 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With my changes, undefined values now print as <undefined> rather than "undefined". This makes sense because these values are not actually strings, and while the <undefined> syntax is not legal JSON, that's fine because these strings are just for display (not for JSON.parse to consume later).

@benjamn benjamn merged commit 9b522cf into apollographql:release-3.4 Jun 7, 2021
@benjamn
Copy link
Member

benjamn commented Jun 8, 2021

These changes should be available now in @apollo/client@3.4.0-rc.6. Thanks @markneub!

@hwillson hwillson removed this from the MM-2021-06 milestone Jul 29, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MockLink doesn't strip undefined values | error message doesn't show undefined variables
4 participants