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

Ability to dynamically match mocks #6701

Merged
merged 20 commits into from Sep 19, 2023

Conversation

prowe
Copy link
Contributor

@prowe prowe commented Jul 26, 2020

This PR provides the functionality outlined in apollographql/apollo-feature-requests#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.

@apollo-cla
Copy link

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

@prowe
Copy link
Contributor Author

prowe commented Aug 24, 2020

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

@brycefranzen
Copy link

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

Copy link

@brycefranzen brycefranzen left a comment

Choose a reason for hiding this comment

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

I have not ran the code, but the changes seem reasonable to me according to my knowledge of the codebase (which is minimal). Additionally, this feature is greatly needed for one of my projects, so I approve.

Copy link

@benjaminpearson benjaminpearson left a comment

Choose a reason for hiding this comment

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

Agree this PR would be very helpful. Would help on a project I'm on at the moment too.

docs/source/development-testing/testing.mdx Outdated Show resolved Hide resolved
@jpvajda jpvajda added 🏓 awaiting-contributor-response requires input from a contributor 🔬 testing-utilities 🏓 awaiting-team-response requires input from the apollo team and removed 🏓 awaiting-contributor-response requires input from a contributor labels May 3, 2022
@hwillson
Copy link
Member

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.

Co-authored-by: Benjamin Pearson <benjaminpearson@users.noreply.github.com>
@prowe
Copy link
Contributor Author

prowe commented Aug 11, 2022

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.

@eltonio450
Copy link

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

Thank you !

@jerelmiller jerelmiller changed the base branch from master to main November 22, 2022 20:39
@jerelmiller jerelmiller changed the base branch from main to release-3.8 November 28, 2022 17:35
@jerelmiller
Copy link
Member

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

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!

@changeset-bot
Copy link

changeset-bot bot commented Aug 17, 2023

🦋 Changeset detected

Latest commit: de539a0

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

@bignimbus
Copy link
Contributor

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 bignimbus removed the 🏓 awaiting-contributor-response requires input from a contributor label Aug 17, 2023
src/testing/react/__tests__/MockedProvider.test.tsx Outdated Show resolved Hide resolved
@@ -150,6 +150,42 @@ it("renders without error", async () => {

</ExpansionPanel>

### Dynamic variables
Copy link
Contributor

Choose a reason for hiding this comment

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

This file has been updated several times since this PR was first opened. Some updates were warranted, I tried not to take too many liberties. Critical feedback is welcome 🙏🏻

@@ -94,6 +97,9 @@ export class MockLink extends ApolloLink {
if (equal(requestVariables, mockedResponseVars)) {
return true;
}
if (res.variableMatcher && res.variableMatcher(operation.variables)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This was one of the conflict areas that was a bit murkier to resolve. I believe this logic is correct since we still want to return true on an exact match first. The tests pass, but a skeptical eye would be helpful here.

@bignimbus bignimbus requested review from jerelmiller and alessbell and removed request for StephenBarlow August 21, 2023 13:51
@alessbell
Copy link
Member

/release:pr

@github-actions
Copy link
Contributor

A new release has been made for this PR. You can install it with npm i @apollo/client@0.0.0-pr-6701-20230824185144.

@brycefranzen
Copy link

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?

@alessbell alessbell self-assigned this Sep 15, 2023
@alessbell
Copy link
Member

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!

Copy link
Member

@alessbell alessbell left a comment

Choose a reason for hiding this comment

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

Excited to get this released in our first 3.9 alpha, thanks @prowe! 🙌

@alessbell alessbell merged commit 8d2b4e1 into apollographql:release-3.9 Sep 19, 2023
17 checks passed
@alessbell alessbell mentioned this pull request Sep 19, 2023
@alessbell
Copy link
Member

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

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 20, 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.

None yet