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

feat(mocking): make response error and result not required when delay is infinite #11562

Merged
merged 1 commit into from Feb 9, 2024

Conversation

mspiess
Copy link
Contributor

@mspiess mspiess commented Feb 1, 2024

Without the changes in this PR mocks always require a result or an error for a response. However, when an infinite delay is specified, that response is never returned, so it might as well be omitted.
With the changes in this PR omission of result and error no longer causes an error, when a delay of Infinity is specified.

const mocks = [
  {
    delay: Infinity
    request: {
      query: GET_DOG_QUERY,
      variables: {
        name: "Buck"
      }
    },
    result: {                                             // all of this
      data: {                                             // makes no
        dog: { id: "1", name: "Buck", breed: "bulldog" }  // sense with
      }                                                   // an infinite
    }                                                     // delay
  }
];


it("renders without error", async () => {
  render(
    <MockedProvider mocks={mocks} addTypename={false}>
      <Dog name="Buck" />
    </MockedProvider>
  );
  expect(await screen.findByText("Loading...")).toBeInTheDocument();
});

@apollo-cla
Copy link

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

Copy link

netlify bot commented Feb 1, 2024

👷 Deploy request for apollo-client-docs pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 7bebece

Copy link

changeset-bot bot commented Feb 1, 2024

🦋 Changeset detected

Latest commit: 7bebece

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 Patch

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

@jerelmiller
Copy link
Member

Hey @mspiess 👋

Appreciate the contribution! Would you be willing to provide some more information on the use case you're solving for and provide some more information about this change? Thanks!

@mspiess mspiess marked this pull request as ready for review February 1, 2024 23:24
@mspiess mspiess requested a review from a team as a code owner February 1, 2024 23:24
@mspiess
Copy link
Contributor Author

mspiess commented Feb 1, 2024

Hey @jerelmiller 👋

I updated the PR description.

Copy link
Member

@jerelmiller jerelmiller left a comment

Choose a reason for hiding this comment

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

Hey @mspiess 👋

I think this change makes sense and provides a nice improvement in your tests. Appreciate the additional context on why this change makes sense!

I've got a few minor things for you to look at. I'd love to get this in the next patch or two. Thanks again!

src/testing/core/mocking/__tests__/mockLink.ts Outdated Show resolved Hide resolved
src/testing/core/mocking/__tests__/mockLink.ts Outdated Show resolved Hide resolved
src/testing/core/mocking/mockLink.ts Outdated Show resolved Hide resolved
@jerelmiller
Copy link
Member

One more thing! Looks like we've got a couple snapshot tests that check the output of that error in MockedProvider.test.tsx. After we get the error output settled, can you update those snapshot tests to the new error message? That should get your tests passing in this PR. Thanks!

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.

Thanks for working on this and pushing those updates! Just took one more look - this is almost ready to merge 👍

src/testing/core/mocking/mockLink.ts Outdated Show resolved Hide resolved
request: {
query,
},
delay: Number.MAX_VALUE,
Copy link
Member

@alessbell alessbell Feb 5, 2024

Choose a reason for hiding this comment

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

As it turns out, both Number.MAX_VALUE and Number.MAX_SAFE_INTEGER will cause the timeout to fire immediately because neither fit into a 32-bit signed integer. (We can tell that it's not doing anything here since the test finishes in less than the default Jest 5000ms timeout without timer mocks.)

The timing itself isn't material to the behavior of this test, but it would be nice to make it accurate to the behavior we'd see in a real test.

With that in mind, would you mind adding a comment about this here and using a random very high number? We can use Jest's timer mock feature and e.g. jest.advanceTimersByTime(2_000_000_000) here to get the test passing again.

Copy link
Member

Choose a reason for hiding this comment

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

Ah shoot, I forgot about that. Thanks for the reminder @alessbell!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I used a constant named MAXIMUM_DELAY. Does that provide enough context, or would you prefer a comment?

Copy link
Member

Choose a reason for hiding this comment

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

A short comment above MAXIMUM_DELAY would be helpful, something like "We've chosen this value as the MAXIMUM_DELAY since values that don't fit into a 32-bit signed int cause setTimeout to fire immediately"

import { MockLink } from "../mockLink";
import { execute } from "../../../../link/core/execute";

jest.useFakeTimers();
Copy link
Member

@alessbell alessbell Feb 6, 2024

Choose a reason for hiding this comment

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

We're just missing a useRealTimers:

Suggested change
jest.useFakeTimers();
beforeAll(() => jest.useFakeTimers());
afterAll(() => jest.useRealTimers());

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.

Left two more minor comments, after those changes are pushed up this is good to go 🚀 Appreciate the contribution @mspiess!

Copy link
Member

@jerelmiller jerelmiller left a comment

Choose a reason for hiding this comment

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

Thanks a bunch @mspiess! We'll get this in the next patch which should release sometime next week.

@jerelmiller jerelmiller merged commit 65ab695 into apollographql:main Feb 9, 2024
24 of 25 checks passed
@github-actions github-actions bot mentioned this pull request Feb 9, 2024
@mspiess
Copy link
Contributor Author

mspiess commented Feb 9, 2024

Great! 🎉 Thanks for the reviews and detailed comments @jerelmiller and @alessbell!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants