question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Timeout when using jest "useFakeTimers" functionality

See original GitHub issue

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.

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:open
  • Created 2 years ago
  • Reactions:10
  • Comments:16 (2 by maintainers)

github_iconTop GitHub Comments

12reactions
NTagcommented, Oct 17, 2022

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:

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…

12reactions
josh-meredithcommented, Sep 8, 2022

If you’re still having issues, try adding both nextTick and setImmediate to the array, this is the only way it’d work for me.

jest.useFakeTimers({ doNotFake: ['nextTick', 'setImmediate'] }).setSystemTime(new Date('2022-01-01'))

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found