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.

[Bug]: Refresh does not work when using fake timers

See original GitHub issue

Version

^27.0.3

Steps to reproduce

Run the following test:

    test('Test refresh', async () => {
        jest.clearAllTimers();
        jest.useFakeTimers();
        const callback = jest.fn();
        const timer: NodeJS.Timer = global.setTimeout(callback, 1000);
        jest.advanceTimersByTime(500);
        expect(callback).toHaveBeenCalledTimes(0);
        timer.refresh();
        jest.advanceTimersByTime(500);
        expect(callback).toHaveBeenCalledTimes(0);
    });

Expected behavior

The timer is refreshed correctly and the callback is not called.

Actual behavior

Refreshing does not work and the callback is called.

Additional context

NodeJS refresh: https://nodejs.org/api/timers.html#timeoutrefresh

Environment

System:
    OS: Windows 10 10.0.22000
    CPU: (8) x64 Intel(R) Core(TM) i7-1065G7 CPU @ 1.30GHz
  Binaries:
    Node: 16.13.2 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.5 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
    npm: 7.21.1 - C:\Program Files\nodejs\npm.CMD

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:7

github_iconTop GitHub Comments

2reactions
SimenBcommented, Mar 2, 2022
1reaction
SimenBcommented, Mar 3, 2022

Tomorrow! 🙂 CET. It’ll be in semver range of v28 alpha, but not v27 (as that version of fake-timers has dropped node 10)

Read more comments on GitHub >

github_iconTop Results From Across the Web

API | React Native Testing Library - Open Source
Since we're using fake timers, the test won't depend on real time passing and thus be much faster and more reliable. Also we...
Read more >
useFakeTimers not working in jest/testing-library
You should advance timers after rendering the component. Besides, you should call jest.advanceTimersByTime() inside act function.
Read more >
Async Methods - Testing Library
Several utilities are provided for dealing with asynchronous code. These can be. ... Wait until the callback does not throw an error.
Read more >
The Jest Object
The jest object is automatically in scope within every test file. ... This function is not available when using legacy fake timers ...
Read more >
Testing Asynchronous Functionality in a React Component
To ensure the tests run in an acceptable time, we can use jest fake timers which will allow the test to make the...
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