[Bug]: Refresh does not work when using fake timers
See original GitHub issueVersion
^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:
- Created 2 years ago
- Reactions:3
- Comments:7
Top 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 >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 FreeTop 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
Top GitHub Comments
I opened up https://github.com/sinonjs/fake-timers/pull/425
Tomorrow! 🙂 CET. It’ll be in semver range of v28 alpha, but not v27 (as that version of
fake-timers
has dropped node 10)