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
Timeout when using jest "useFakeTimers" functionality #2200
Comments
This happens to me right after upgrading Jest from v26 to v27, @sdomagala are you in the same condition as well? |
I've read that |
By specifying Quote from Jest blog post on v27 release [1]:
|
@imcotton for me it happened the moment I've tried to use those modern timers and function Using legacy timer indeed resolves the issue in this test case but it doesn't let you set global time so it doesn't really help me.
|
Recently I experienced this too. It seems like Try to advance at somewhere: jest.useFakeTimers();
const url = 'https://example';
nock(url).get('/').reply(201);
const axiosPromise = axios(url);
// Advance 1ms for nock delay.
jest.advanceTimersByTime(1);
// Got the response now.
const res = await axiosPromise;
expect(res.status).toBe(201); |
if you could digg into that and see if we could change the implementation to something more resilient that will work with jest's new fake timers that'd be great |
nock/lib/intercepted_request_router.js Lines 66 to 69 in 3efd738
Here, Jest's modern fake timer completely locks the time including the tick, so the connect is never reached, and a |
Also broken with sinon.useFakeTimers. The first mocked calls works, but any subsequent call times out. |
Add an eye here, bcs I just faced a same issue, with pretty much the same codes as above. But in my case, using Versions:"jest": "26.6.3", |
I've tried the recently added |
Using |
If you're still having issues, try adding both
|
yeah, that's true, before I found this answer, I tried one by one to not fake this functionality 'Date',
'hrtime',
'performance',
'queueMicrotask',
'requestAnimationFrame',
'cancelAnimationFrame',
'requestIdleCallback',
'cancelIdleCallback',
'clearImmediate',
'setInterval',
'clearInterval',
'setTimeout',
'clearTimeout',
'nextTick',
'setImmediate', and found out to making fakeTimers working is to not faking "'nextTick', 'setImmediate'" but I still don't know what effect If I do that on my test 😓 |
This worked for me, thanks! |
I followed @chornos13 comment, started with the whole list into import { jest } from "@jest/globals";
jest
.useFakeTimers({
doNotFake: [
"nextTick",
"setImmediate",
"clearImmediate",
"setInterval",
"clearInterval",
"setTimeout",
"clearTimeout",
],
})
.setSystemTime(new Date("2022-02-15")); I don't understand why Jest doesn't offer a way to just change the date without mocking the universe… |
Changing |
None of these worked for me |
Fantastic, this fixed it for me! I'm using When an error was thrown inside an async route handler in my Express app, and I used
|
I wrote a small function to fix this issue by extending /**
* Adds `fake` option to `jest.useFakeTimers` config api
*
* @param config Fake timers config options
*
* @return Jest instance
*/
function useFakeTimers(config?: FakeTimersConfig & { fake?: FakeableAPI[] }) {
if (config?.fake) {
if (config.doNotFake) {
throw new Error('Passing both `fake` and `doNotFake` options to `useFakeTimers()` is not supported.')
}
const { fake, ...options } = config
return jest.useFakeTimers({
...options,
doNotFake: Array<FakeableAPI>(
'Date',
'hrtime',
'nextTick',
'performance',
'queueMicrotask',
'requestAnimationFrame',
'cancelAnimationFrame',
'requestIdleCallback',
'cancelIdleCallback',
'setImmediate',
'clearImmediate',
'setInterval',
'clearInterval',
'setTimeout',
'clearTimeout',
).filter((api) => !fake.includes(api)),
})
}
return jest.useFakeTimers(config)
} Usage beforeAll(async () => {
useFakeTimers({ fake: ['Date'] })
}) cc @NTag |
The same issue also appears in vitest (as they use the same timer, but the doNotFake property doesnt exist there). vi.useFakeTimers({
shouldAdvanceTime: true,
toFake: ["Date"],
}); |
Instead of jest.useFakeTimers('legacy') it should be jest.useFakeTimers({
legacyFakeTimers: true
}) |
testing-library/user-event#833 might be your issue if you are using testing library user events. Took me forever to figure out but this eventually fixed it for me: jest.useFakeTimers({ advanceTimers: true }); |
i had to add { advanceTimers: true } to useFakeTimers like this: jest.useFakeTimers({ advanceTimers: true }).setSystemTime(dateAfterHour); also i had to delete this from jest config: fakeTimers: { adding advanceTimers;true to global config did not help |
In the context of fastify inject I found that it was essential to use the beforeAll(async () => {
jest
.useFakeTimers({ advanceTimers: true })
.setSystemTime(new Date('2022-04-24'));
});
afterAll(async () => {
jest.useRealTimers();
}); I'm not a |
What is the expected behavior?
When mocking timers in jest I would expect mocks to pass properly as those (to me) shouldn't be related to network communication.
What is the actual behavior?
Test case hangs until the whole suite times out
Possible solution
Make mocks not reliant on the passing time, or maybe provide a flag that would handle this behaviour differently?
How to reproduce the issue
Remove fake timers and the test case passes
Does the bug have a test case?
Yes, provided above
Versions
The text was updated successfully, but these errors were encountered: