apollo-client icon indicating copy to clipboard operation
apollo-client copied to clipboard

Ability to dynamically match mocks

Open prowe opened this issue 3 years ago • 5 comments

This PR provides the functionality outlined in https://github.com/apollographql/apollo-feature-requests/issues/245

It adds support for a new property MockedResponse.variableMatcher that is a function that takes variables and returns if they should match for this mock. A user cannot specify both this property as well as response.variables in the same mock.

It also passes the variables into the ResultFunction so that a function is able to use them to dynamically build a response. Passing these parameters also allows for the use of a mock for the result function and then asserting that specific variables were passed. This testing strategy results in a cleaner error message for mutations if the correct data is not passed and allows for partial matches using jests "objectContaining" and others.

My team found testing using the MockProvider to be difficult in several situations and these small changes would have greatly improved our ability to test with the ApolloClient.

prowe avatar Jul 26 '20 02:07 prowe

@prowe: 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/

apollo-cla avatar Jul 26 '20 02:07 apollo-cla

Are there any additional steps I should take beyond reporting a feature request and opening a PR to have this looked at/discussed. This is my first Pull Request to this project so I may not be following the right process and just want to make sure i'm doing the best I can.

Thanks

prowe avatar Aug 24 '20 20:08 prowe

Is there any progress on reviewing/merging this? This would greatly help with my needs for mocking in my application.

brycefranzen avatar Sep 27 '21 05:09 brycefranzen

Thanks for this @prowe - would you or anyone else in the community be interested in rebasing this against main? This is a great change. We're revisiting our entire testing story (tracked in https://github.com/apollographql/apollo-client/issues/9738) but until that happens, this is definitely an improvement.

hwillson avatar Aug 11 '22 00:08 hwillson

Thanks for this @prowe - would you or anyone else in the community be interested in rebasing this against main? This is a great change. We're revisiting our entire testing story (tracked in #9738) but until that happens, this is definitely an improvement.

Rebased and should be good. Let me know if there is anything else I can do to be of assistance.

prowe avatar Aug 11 '22 04:08 prowe

whoa this PR looks great! Can we already start using it ? Do you think it will land in master ?

Thank you !

eltonio450 avatar Oct 20 '22 12:10 eltonio450

I think this is a solid PR and would love to get this in our 3.8 release! I've updated the base branch to our release-3.8 branch. I'll take a crack at getting the conflicts resolved.

jerelmiller avatar Nov 28 '22 17:11 jerelmiller

Hey @prowe! Would you be willing to rebase this against our release-3.8 branch? I've attempted to do this myself, but there are enough changes that I'm afraid I'd butcher your PR in doing so. I'd be happy to get this approved/merged once we are able to get this PR up-to-date and passing tests!

jerelmiller avatar Nov 28 '22 18:11 jerelmiller

@prowe Think this PR needs another round of rebasing, much appreciated!

lhmzhou avatar Mar 21 '23 03:03 lhmzhou

It's been two years since I last commented on this and the MR is still open 😢

Is this ever going to be merged? It would be AMAZING!

brycefranzen avatar Aug 11 '23 22:08 brycefranzen

Thanks for the tap on the shoulder @brycefranzen - we're going to prioritize getting this into our release-3.9 branch and then release it in 3.9.0-alpha.0 for the community to try out, expect that pretty soon. We're looking forward to seeing how folks use this feature and how it will complement the investments we will be making into Apollo Client's testing utilities 🚀

bignimbus avatar Aug 15 '23 18:08 bignimbus

🦋 Changeset detected

Latest commit: de539a099abbaf00e79b24bb1016e5e3b7f77563

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@apollo/client Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

changeset-bot[bot] avatar Aug 17 '23 04:08 changeset-bot[bot]

I attempted to resolve the merge conflicts here. I'll self-review this later in the week and add a changeset file as well.

bignimbus avatar Aug 17 '23 04:08 bignimbus

/release:pr

alessbell avatar Aug 24 '23 18:08 alessbell

A new release has been made for this PR. You can install it with npm i @apollo/[email protected].

github-actions[bot] avatar Aug 24 '23 18:08 github-actions[bot]

How are things looking here? I see we have a PR release we can use now 🙌🏼 ... Any progress on getting this reviewed/merged so it can be released with v3.9?

brycefranzen avatar Sep 13 '23 16:09 brycefranzen

How are things looking here? I see we have a PR release we can use now 🙌🏼 ... Any progress on getting this reviewed/merged so it can be released with v3.9?

This is very close to being merged :) Hopefully next week, though I'll be out for a few days at a conference. The final to do item is fixing a TS error these changes have caused that's not being reported on CI since it's occurring in test files (we have a separate to do item to fix that). Thanks for your patience!

alessbell avatar Sep 15 '23 19:09 alessbell

This has been released in v3.9.0-alpha.0 🎉

alessbell avatar Sep 19 '23 17:09 alessbell