-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
MockLink doesn't strip undefined values | error message doesn't show undefined variables #6771
Comments
One solution i can think of is using a replacer function on eg: function replacer(key, value) {
return typeof value === 'undefined' ? "SOME GOOD MESSAGE OR SOMETHING" : value;
} if (!response || typeof responseIndex === 'undefined') {
this.onError(new Error(
`No more mocked responses for the query: ${print(
operation.query
)}, variables: ${JSON.stringify(operation.variables), replacer}`
));
return null;
} Alternatively, have the mock request behave the same as a real world request and strip the undefined values prior to finding the mock. |
Ran into this just now as well when migrating from 2 to 3, a large part of our apollo tests suddenly started failing and I just couldn't figure out what was going wrong. I think a rollback to the previous behavior (stripping undefined) would probably be desirable as right now it is a massive pain to debug and fix. |
The problem is because MockLink is not using fast-json-stable-stringify to stringify variables and compare it. Here is v2 MockLink equality check Here is v3 MockLink equality check
I'm interested is this a bug in v3 or intentional change? |
it looks like I also have this issue ☝️ |
@benjamn looks like |
See benjamn/wryware#21 for explanation and motivation. Should help with #6771, #6803, #6688, #6378, and #7081, and possibly other regressions that began in @apollo/[email protected].
See benjamn/wryware#21 for explanation and motivation. Should help with #6771, #6803, #6688, #6378, and #7081, and possibly other regressions that began in @apollo/[email protected].
This behavior should be fixed/improved in |
Thanks! I'll update and try it out. |
Tell me is I'm wrong but this regression seems to remain in @apollo/[email protected] ? |
Let us know if this is still a concern with |
Intended outcome:
When making a request against a live GraphQL endpoint any
undefined
values in the variables object are removed from the payload.When testing using the MockedProvider, the variables that were
undefined
remain in the request. This leads to the equality check failing (which it should).The problem comes when you see the error message that Apollo throws.
The error message
No more mocked responses were found etc
usesJSON.stringify
which excludes any values that were undefined. This behavior makes it tricky to debug because you're essentially seeing the exact mock you provided to the test.Actual outcome:
How to reproduce the issue:
Codesandbox
To reproduce:
In
App.js
note that the mutation has a value with undefined in the variables payload. Note that the mock in the test has the payload without the undefined value. This is the same request you would see get sent in the network tab.Note that the error message excludes the undefined variable (see image) and that the query it's showing matches our mock exactly.
Versions
@apollo/[email protected]
Chrome: 84.0.4147.105
Jest: 25.2.3
The text was updated successfully, but these errors were encountered: