jest: requestAnimationFrame does not invoke callback (v22, jsdom: latest version)

Do you want to request a feature or report a bug? A bug

What is the current behavior? The requestAnimationFrame doesn’t invoke callback. Maybe it happened because the jsdom is using setTimeout under the hood, but useFakeTimers and runAllTimers not helping.

If the current behavior is a bug, please provide the steps to reproduce and either a repl.it demo through https://repl.it/languages/jest or a minimal repository on GitHub that we can yarn install and yarn test.

  1. Use requestAnimationFrame on your code
  2. try to test what is happened inside callback

What is the expected behavior? requestAnimationFrame should invoke its callback

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system. OS: High Sierra node: 8.9.1 jest: 22.0.3

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 6
  • Comments: 17 (6 by maintainers)

Commits related to this issue

Most upvoted comments

You can mock requestAnimationFrame with your own implementation.

Here is a workaround:

beforeEach(() => {
  jest.spyOn(window, 'requestAnimationFrame').mockImplementation(cb => cb());
});

afterEach(() => {
  window.requestAnimationFrame.mockRestore();
});

I can replace requestAnimationFrame with my own implementation like this (bit naive, but fine if I just want to advance timers)

global.requestAnimationFrame = fn => setTimeout(fn, 16);

But it must go inside setup-jest file, as if its inside my test file, it doesn’t work. (probably global is different between the two, I’m running JSDOM 15 and jest 24.8.0)

(I have used this approach to test react-spring components and it works pretty well)

That’s just async javascript for you.

You can open up a separate feature request for fakeTimers to work with raf (PR welcome as well!), but closing this as raf does work as it should in Jest 22