Requests is Node don't resolve immediately
See original GitHub issueEnvironment
Name | Version |
---|---|
msw | 0.24.2 |
node | 10.21.0 |
OS | Mac OSX 10.15.7 |
Question
Pardon me if this is the wrong place to post. I am working on transferring all of our mocks to a wrapper library around your wonderful library. This is working great, but now that I have moved on to updating our UI unit tests I’ve run into a bit of a quandary.
Most of our tests do something like:
jest.spyOn(global, 'fetch').mockImplementation(url => Promise.resolve({
json: () => Promise.resolve(data),
status: 200
}));
I believe this is a fairly common pattern that I’ve seen referenced many places. I’ve removed this functionality to accommodate setupServer
, but now have multiple issues with act
warnings and unexpected tree states. I understand that the Node requests will be intercepted in the middleware, and thus asynchronous requests are still being made with some latency. I’ve also read the “How is this library different?” section of node-request-interceptor
, so understand that this particular project takes a stance.
It seems my only option at this point is to install and utilize react-testing-library
’s waitFor
utility. It just seems like a lot of overhead to upgrade react-scripts
/Jest to get all this working. I was hoping listen
would be a little more accommodating to customizations, but maybe that’s not in the cards.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (4 by maintainers)
Top GitHub Comments
@kettanaito Here’s an example that demonstrates the problem with the
LoginForm
component from the React REST examples. I know this maybe isn’t the most “ideal” testing setup.Wouldn’t you have to
await
the form submission being that it is an async handler? I think mocking fetch was just masking the promise-related issues here, but I could be wrong!await container.root.findByProps({ id: 'login-form' }).props.onSubmit({ preventDefault: () => {} });
Edit: I was curious about this, so I made a small repro where it works as expected based on the MSW template.
https://codesandbox.io/s/msw-test-react-test-renderer-qpd48?file=/test/App.test.js