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

Proposal for an Option to Enable Infinite Reuse of Mocks #11556

Closed
leekib opened this issue Feb 1, 2024 · 5 comments
Closed

Proposal for an Option to Enable Infinite Reuse of Mocks #11556

leekib opened this issue Feb 1, 2024 · 5 comments
Labels
🏓 awaiting-contributor-response requires input from a contributor

Comments

@leekib
Copy link

leekib commented Feb 1, 2024

I think it would be great to add a clear option for continuous reuse of mocks

I saw that the maxUsageCount option was added for mock reuse in #11178.
I've been using mockLink with MockedResponses defined with a reasonably large number for maxUsageCount for development mock APIs, but I would love to see an option added for infinite reuse.
I read the comment in #9738 about the introduction of a new testing method using schemaLink. However, I think it would be great to add a clear option for continuous reuse of mocks in the existing MockedProvider method before that.
For example, while maintaining the current behavior when no maxUsageCount is provided, we could change the type of maxUsageCount to number | 'infinite' to allow specifying infinite explicitly for infinite reuse of mocks.
Would this approach be acceptable?

import { MockedResponse } from "@apollo/client/testing";

const dogMock: MockedResponse<Data, Variables> = {
  request: {
    query: GET_DOG_QUERY
  },
  variableMatcher: (variables) => true,
  //maxUsageCount: 9999, 
  maxUsageCount: 'infinite',
  result: {
    data: { dog: { id: 1, name: 'Buck', breed: 'poodle' } },
  },
};
@jerelmiller
Copy link
Member

jerelmiller commented Feb 1, 2024

Hey @2012160085 👋

maxUsageCount already handles Number.POSITIVE_INFINITY as a valid value. Per our docs

Passing Number.POSITIVE_INFINITY will cause the mock to be reused indefinitely.

In fact, we have a test case for this to prove this works.

Does this address what you're looking for?

Edit: Adding that maxUsageCount was added in 3.9.0 which was released 2 days ago on Jan 30th 🙂

@jerelmiller jerelmiller added the 🏓 awaiting-contributor-response requires input from a contributor label Feb 1, 2024
@leekib
Copy link
Author

leekib commented Feb 1, 2024

Thanks for fast response. I didnt think of Number.POSITIVE_INFINITY.

@leekib leekib closed this as completed Feb 1, 2024
Copy link
Contributor

github-actions bot commented Feb 1, 2024

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better.

@jerelmiller
Copy link
Member

You're welcome! Glad that works for you!

Copy link
Contributor

github-actions bot commented Mar 3, 2024

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
For general questions, we recommend using StackOverflow or our discord server.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🏓 awaiting-contributor-response requires input from a contributor
Projects
None yet
Development

No branches or pull requests

2 participants