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

requestAnimationFrame callbacks are never called with legacy fake timers when advancing time #11565

Closed
eps1lon opened this issue Jun 13, 2021 · 3 comments · Fixed by #11567
Closed

Comments

@eps1lon
Copy link
Contributor

eps1lon commented Jun 13, 2021

🐛 Bug Report

Callbacks scheduled with requestAnimationFrame are not called when using jest.useFakeTimers('legacy') and jest.advanceTimersByTime(17)

To Reproduce

test.each(["legacy", "modern"])(
  "%s fake timers wait for requestAnimationFrame",
  async (timerImplementation) => {
    jest.useFakeTimers(timerImplementation);
    let exited = false;
    // implemented in JSDOm with `setInterval(callback, 16.6666)`
    requestAnimationFrame(() => {
      exited = true;
    });

    jest.advanceTimersByTime(15);

    expect(exited).toBe(false);

    jest.advanceTimersByTime(2);

    expect(exited).toBe(true);
  }
);

Expected behavior

Tests pass with legacy and modern fake timers

Link to repl or repo (highly encouraged)

https://github.com/eps1lon/jest-fake-timers-request-animation-frame

envinfo

  System:
    OS: Linux 5.4 Ubuntu 20.04.2 LTS (Focal Fossa)
    CPU: (6) x64 Intel(R) Core(TM) i5-9400 CPU @ 2.90GHz
  Binaries:
    Node: 12.22.1 - ~/.nvm/versions/node/v12.22.1/bin/node
    Yarn: 1.22.5 - /usr/bin/yarn
    npm: 6.14.12 - ~/.nvm/versions/node/v12.22.1/bin/npm
  npmPackages:
    jest: ^27.0.4 => 27.0.4
@SimenB
Copy link
Member

SimenB commented Jun 14, 2021

Legacy timers do not support rAF: https://github.com/facebook/jest/blob/d1882f2e6033186bd310240add41ffe50c2a9259/packages/jest-fake-timers/src/legacyFakeTimers.ts#L31-L40 (and jsdom uses its own setInterval, not the one exposed, so mocking one doesn't mock the other)

PR welcome!

@eps1lon
Copy link
Contributor Author

eps1lon commented Jun 14, 2021

Thanks for the quick response.

Legacy timers do not support rAF:

I'm not sure why waitFor from @testing-library/dom was working before we merged testing-library/dom-testing-library#966 though. Maybe it just worked incidentally because we used to real setImmediate which would flush animation frames at some point (even though the timing would be completely off).

PR welcome!

Let me check if I can throw something together quickly. Not sure if this will be useful though since that would still require updating jest itself.

@github-actions
Copy link

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 Jul 15, 2021
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