-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Using msw with jest has unexpected behaviors with apollo client. #9619
Comments
+1 |
Thank you for reporting this! Have you tried posting this in our Apollo Community for guidance from other Apollo users, as other's may have input into your question. I'm curious if anyone has a work around for using MSW with Jest. I'll leave this open as a question for now, and see if the team can provide some direction. |
@jpvajda I haven't. If I revisit this I'll follow up there. Thank you. I suspect there are multiple async operations that happen between returning and having the data in the component. I don't think this happens with MockedProvider. If I remember correctly, a tick can be used with MockedProvider. I wonder if after checking if it's been returned, if a single tick would work. 🤔 |
Hi all, I don't think we have enough to say that there's definitely something actionable for the maintainers here. I don't think we can expect an instantaneous render after a network request resolves. So even though the data is available the React render cycle still needs to work. I wonder if this would produce a green test? const handler = jest.fn((req, res, ctx) => res(ctx.data({...}));
server.use(
graphql.query('foo', handler)
)
render(<Component />)
const hasDataNode = await screen.findByText('Has data'));
expect(hasDataNode).not.toBeNull(); |
what works for me is using a custom async function that waits for the next event loop.
i have it built into my test render method like this:
but you can also use it stand-alone. using your example:
|
Hi all, does the above workaround work for you? (Thanks @szamanr!) |
I haven't tried recently but when I wrote this, ticking next was arbitrary because it would take a random number of ticks. If I can tick once and be good then I would be happy with that. |
(This has already been stated by @bignimbus above, but I thought I'd make it a bit more obvious) Generally, with anything involving a "network", please use the Async Methods const handler = jest.fn((req, res, ctx) => res(ctx.data({...}));
server.use(
graphql.query('foo', handler)
)
render(<Component />)
- await waitFor(() => expect(handler).toHaveReturned())
- expect(screen.getByText('Has data')).toBeVisible()
+ expect(await screen.findByText('Has data')).toBeVisible() |
True. The one situation I can think of is checking that some element doesn't show up. Where we need to be confident that the data loaded and the components render but the one component we're querying doesn't show up. |
Seconding what @bignimbus and @phryneas wrote above. I'll go ahead and close out this issue, but I'd also like to mention we're currently working on improving the testing story in Apollo Client. You can follow issue #9738 if you're interested. Thanks! |
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. |
Intended outcome:
The test should return true.
Actual outcome:
The test returns false because the data has not returned yet.
How to reproduce the issue:
See the first section.
Versions
System:
OS: macOS 11.6.4
Binaries:
Node: 12.22.12 - ~/.nvm/versions/node/v12.22.12/bin/node
npm: 6.14.16 - ~/.nvm/versions/node/v12.22.12/bin/npm
Browsers:
Chrome: 100.0.4896.127
Edge: 100.0.1185.50
Firefox: 84.0.2
Safari: 15.3
npmPackages:
@apollo/client: ^3.3.3 => 3.3.13
apollo-upload-client: ^14.1.3 => 14.1.3
The text was updated successfully, but these errors were encountered: