-
Notifications
You must be signed in to change notification settings - Fork 7
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
Provide a way to specify a custom matcher in MockedProvider #245
Comments
We are also facing the same difficulty here. As we have a component that generates some variables, but two objects with the same fields and nested fields are not equal to each other in javascript. This makes it impossible for me to mock the data, even if I know the exact variables. So I would like to just give it true always or use something like the lodash |
If I'm not mistaken, we have a use case that could benefit from this feature too. Sometimes not all the queries actually matter for a test case, and the count is too long. In those cases a code like this:
could be changed to this:
Or something even better |
For people who have the same problem, you might want to use https://mswjs.io/. It is highly recommended from several pioneers, and supports this use case. Instead of mocking via MockedProvider, you can mock on network level. Don't have to wait on this feature. |
@promontis It is fine for restful APIs. For Apollo client users, we, need to care about equality and deep equality and variables and also caching etc.... Which MokcedProvider of Apollo Client does them all automatically for us |
@aghArdeshir Maybe I'm missing something, but the mocking is on the network level. So basically all of the features of Apollo client can and are used. Instead of actually doing a network call over the wire, it is intercepted on the level of the browser (service worker) and a mock is returned. So, basically it uses the Apollo client all the way. Like this mocking library is agonistic for whatever graphql (or any other protocol) client is used, that's what makes it highly recommended from the pioneers. |
But it makes it hard to check for equality and deep equality of variables. And also in cases where user is using |
Hey all 👋 We have merged apollographql/apollo-client#6701 that provides the functionality outlined in this request and will be released in the next minor As such, I'm going to close this as completed. Thanks! |
Support for dynamic matching of MockedResponses and dynamic return data
Problem
The MockedProvider has a
mocks
prop that allows users to specify the variables and query to match what mock response to return. However, this requires the developer know the exact variables that are being passed to the query. In a lot testing situations, the goal of the test is not to test specific variables but rather, return a mock response to test other behavior.It is also difficult to assert that a particular query/mutation was executed and what variables it was executed with. The mock allows a developer to specify a Fetch function to return the response, but this method does not get any parameters that can be asserted to ensure it can return the correct response or generate a response (for example, responding with an object that has the id requested).
Proposed Solution
I propose two modifications to the API:
(variables: any) => boolean
that if provided is used to match variables rather than strict equality on the structure. Clients can provide this function instead of variables on the request to dynamically determine if the variables are a match for a specific query/mutation.The text was updated successfully, but these errors were encountered: