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
jest.useFakeTimers()
stuck the test in MSW 2
#1830
Comments
Looks similar issue happens to me in #1829 My Code Sandbox stucks at await response.json() if using fakeTimers And the issue gone if I remove those fakeTimers code |
A little attempt at some initial debugging:
I modified like this, to see if we could run it once jest progressed timers, and we can but this failed with a json parsing error.
this shows a response that's wrong, which is interesting
which doubles the response in the body. (just leaving these breadcrumbs for debugging further) |
With
which I discovered as a setting from this: nock/nock#2200 this config option in jest is
which makes it seem like there's some issue related to |
Found another workaround you can see here jest.useFakeTimers({
now: hardCodeNow,
doNotFake: ["queueMicrotask"],
}); It looks like it's related to this issue - nodejs/undici#1251 in undici, which is really less of an undici issue and more of a "jest does some things in not great ways" issue. As long as you don't need to fake calls to So in summary - I don't believe
jest.useFakeTimers({
now: hardCodeNow,
legacyFakeTimers: true,
});
jest.useFakeTimers({
now: hardCodeNow,
doNotFake: ["queueMicrotask"],
}); Both of these configurations seem to work properly |
Since i'm not sure that msw can effectively fix this directly and there are at least 2 workarounds, I'm going to close this one - but please let me know if neither of those 2 help to resolve the issue which stems from how jest and undici (the node fetch library) interact. |
@kettanaito - do you think it might be wise add some documentation here: https://mswjs.io/docs/runbook for this? If so (or if you think there's a better place) I'm happy to write up a draft |
@mattcosta7, thanks for looking into this! Yes, I believe the Runbook is the best place for this. Please, would you have a minute to open a pull request with the |
Opened mswjs/mswjs.io#290 |
@mattcosta7 Thanks for the solution, but it seems that none of them are working in my reproduction repo in the first thread message. I didn't find any major differences in my testing approach compared to @jeffwong14's Codesandbox. |
@joel-daros I haven't used create-react-app in a long time. From the docs, it looks like As we discuss in the docs for msw
I'm not sure it can be configured in create-react-app without ejecting (but you can always just use jest directly instead of via react-scripts and pass a custom configuration for jest that mirrors cra but with additional configuration so you may need to avoid using |
I suppose |
Maybe! I think it might conflict on |
@mattcosta7 2nd fixed the issue, I have not checked 1st. |
Prerequisites
Environment check
msw
versionNode.js version
v18.17.1
Reproduction repository
https://github.com/joel-daros/msw2-text-encoder-issue
Reproduction steps
Clone the repo and run:
npm install --legacy-peer-deps npm run test
Current behavior
After adding this line, the test stuck, and the only way to finishing is aborting the jest execution:
I noticed this issue has been addressed here in MSW 1.x (and actually works well in 1.x version), but seems like it's back in MSW 2.x with the addition of the jest.pollyfills.js file.
Expected behavior
jest.useFakeTimers()
should be work in MSW 2.The text was updated successfully, but these errors were encountered: