Timeout when using jest "useFakeTimers" functionality
See original GitHub issueWhat 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.
jest
.useFakeTimers('modern')
.setSystemTime(new Date('2020-01-01').getTime())
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
import nock from 'nock'
import axios from 'axios'
jest
.useFakeTimers('modern')
.setSystemTime(new Date('2020-01-01').getTime())
describe('Test', () => {
const url = 'http://www.google.com'
beforeEach(() => nock(url).get('/').reply(201))
afterEach(() => expect(nock.isDone()).toEqual(true))
it('should pass this test', async () => {
const res = await axios(url)
expect(res.status).toEqual(201)
})
})
Remove fake timers and the test case passes
Does the bug have a test case?
Yes, provided above
Versions
Software | Version(s) |
---|---|
Nock | v13.0.11 |
Node | v14.16.0 |
TypeScript | v3.9.7 |
Issue Analytics
- State:
- Created 2 years ago
- Reactions:10
- Comments:16 (2 by maintainers)
Top Results From Across the Web
Timer Mocks - Jest
Jest can swap out timers with functions that allow you to control the ... functions (i.e., setTimeout() , setInterval() , clearTimeout() ...
Read more >Even after using jest.useFakeTimers getting timeout error for ...
I have created below function for polling export const wait = (ms: number): Promise<void> => new Promise((resolve) => setTimeout(resolve, ...
Read more >jest.useFakeTimers JavaScript and Node.js code examples
How to use. useFakeTimers. function. in. jest ... toBeDefined(); done(); }); it('triggers snapshot retrieval after timeout', async (done) => { jest.
Read more >Mocking setTimeout with Jest. | Medium - Marek Rozmus
How to test setTimeout with Jest. ... misleading as we won't write any mocks but we need to use some functionality that Jest...
Read more >Using Fake Timers | Testing Library
jest.useFakeTimers() }). When using fake timers, ... cleanup functions), from being coupled to your fake timers and use real timers instead.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I followed @chornos13 comment, started with the whole list into
doNotFake
, and removed one by one elements. The setup which is working on my computer (my setup is jest and supertest) is:I don’t understand why Jest doesn’t offer a way to just change the date without mocking the universe…
If you’re still having issues, try adding both
nextTick
andsetImmediate
to the array, this is the only way it’d work for me.jest.useFakeTimers({ doNotFake: ['nextTick', 'setImmediate'] }).setSystemTime(new Date('2022-01-01'))