jest
jest copied to clipboard
[Bug]: useFakerTimers and fetch/XHR do not seem to work
Version
28.1.2
Steps to reproduce
- Clone https://github.com/phil-tutti/react-testing-library
- Run
yarn install - Run
yarn test - See the tests fail
Expected behavior
I expected to see all the tests pass.
Actual behavior
Tests that use useFakeTimers fail
Additional context
I tried a while to come up with a minimal reproduction case. However, to rule out any libraries like node-fetch, whatwg-fetch and so on, I also created a failing example with XMLHttpRequest.
Additionally, I tried to see if setTimeout works with useFakeTimers within a React component, and it seems to work.
Basically, what I observed, is that as soon as I start using useFakeTimers, requests (fetch/XHR) are not being sent, as if something is blocking them. Even running jest.runAllTimers() did not do the trick. Now I'm wondering if this is the expected behaviour, and if yes, maybe someone can shed some light here for me, as I'm really trying to understand that behaviour. When I digged a bit into node-fetch or whatwg-fetch, I saw that they worked with setTimeout and Promise, so in my eyes, whenever I run advanceTimersByTime, runAllTimer or runOnlyPendingTimers, it should run those requests, but it doesn't.
Environment
System:
OS: macOS 12.4
CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Binaries:
Node: 16.15.1 - /usr/local/bin/node
Yarn: 1.22.18 - ~/.yarn/bin/yarn
npm: 8.11.0 - /usr/local/bin/npm
npmPackages:
jest: 28.1.2 => 28.1.2
Did you try playing with useFakeTimers options? Depending on situation something like advanceTimers: true, or doNotFake: ['nextTick'] could make all work.
Hey @mrazauskas , thank you for your suggestions. I tried them out and played with the values a bit, but they don't seem to do the trick
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
Bumping it in order to not be auto-closed
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
This is still an issue
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.
This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.
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. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.