Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
alyssajotice opened this issue Mar 1, 2022 · 7 comments · Fixed by #12549
Closed

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

alyssajotice opened this issue Mar 1, 2022 · 7 comments · Fixed by #12549

Comments

@alyssajotice
Copy link

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
@SimenB
Copy link
Member

SimenB commented Mar 2, 2022

Huh, I thought this worked (I filed sinonjs/fake-timers#187 years ago). But this looks like a bug in sinon to me - it for some reason uses the global setTimeout and clearTimeout.

https://github.com/sinonjs/fake-timers/blob/fc56a4d55ed1fcb9680adf7a7a09eee308b8e945/src/fake-timers-src.js#L601-L603

@SimenB
Copy link
Member

SimenB commented Mar 2, 2022

I opened up sinonjs/fake-timers#425

@alyssajotice
Copy link
Author

Thanks for the quick response on this @SimenB! I see the PR merged. When can I expect the fix to be released to preview and/or stable?

@SimenB
Copy link
Member

SimenB commented 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)

@SimenB
Copy link
Member

SimenB commented Mar 5, 2022

A bit delayed, but released now @alyssajotice 🙂

It's in semver range of jest@28, so updating your lockfile should be enough. If you're on jest@27 you'll need to use resolutions (@sinonjs/fake-timers dropped node 10 in their v9 release, so unless you need node 10 support, you're safe to upgrade)

@SimenB
Copy link
Member

SimenB commented Mar 6, 2022

I've also released alpha.7 which pulls in the patched version, no lockfile shenanigans needed

@github-actions
Copy link

github-actions bot commented Apr 6, 2022

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants