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.

waitFor should work with fake timers

See original GitHub issue

Relevant code or config:

const {waitFor} = require('@testing-library/dom')

beforeEach(() => jest.useFakeTimers())
afterEach(() => jest.useRealTimers())

test('example', async () => {
  const doAsyncThing = () =>
    new Promise((r) => setTimeout(() => r('data'), 300))
  let result
  doAsyncThing().then((r) => (result = r))

  await waitFor(() => expect(result).toBe('data'))
})

What you did:

I expected waitFor to work even with jest fake timers enabled

What happened:

It times out because there’s nothing telling jest to advance timers

Reproduction:

https://github.com/kentcdodds/testing-library-fake-timers

Problem description:

When enabling fake timers with DOM Testing Library, the waitFor utility doesn’t work (so none of the other async utils will either). The reason for this is there’s nothing telling the clock to advance the timers.

Suggested solution:

I’m pretty sure we actually do want the async utils to work seamlessly when fake timers are enabled.

Here’s a waitFor function that will work only if fake timers are enabled. Maybe we can detect fake timers (we already have a function for this) and if they’re enabled, then we could do this instead?

async function waitForWithFakeTimers(cb) {
  let waiting = true
  while (waiting) {
    await act(() =>
      Promise.resolve()
        .then(() => jest.runAllTimers())
        .then(() => new Promise(resolve => setImmediate(resolve))),
    )
    try {
      cb()
      waiting = false
    } catch {}
  }
}

It’s pretty basic, it doesn’t support timeout etc. But it’s a good start.

Mostly, I’m looking for feedback. Should we do something like this if fake timers are enabled? Or is there another approach we can take?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ValentinHcommented, Feb 6, 2022

Really cool idea. I spent quite some time yesterday to help a colleague debug a test exactly because of this!

1reaction
tannerlinsleycommented, Jun 23, 2020

I think this is absolutely better than the test failing incorrectly. So I vote yes detect and use this. Improvements can be made incrementally.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Fake Timers | Testing Library
When using fake timers, you need to remember to restore the timers after your test runs. The main reason to do that is...
Read more >
React testing library - fakeTimers with waitFor ... - Stack Overflow
In my case all my tests are passing, but running slow because they are using real timers, and jest.useFakeTimers immediately causes every test ......
Read more >
Timer Mocks - Jest
In the following example we enable fake timers by calling jest.useFakeTimers() . This is replacing the original implementation of ...
Read more >
Fix the "not wrapped in act(...)" warning with Jest fake timers
[0:46] In here we're using jest fake timers because we don't want to have to wait for a full second for this tick...
Read more >
Unit Testing Beginners Guide - Part 2 - Spying and fake timers
You will learn what mocking and stubbing is, how to test functions ... Let's put the "spy" to work by changing the code...
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