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
.
- Use
requestAnimationFrame
on your code - 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
- Add comment for facebook/jest#5147 — committed to ant-design/ant-design by afc163 6 years ago
- Fix test case for new jsdom (#9527) * Fix test case for new jsdom * use setTimeout as raf in jest jsdom * Fix cancelAnimationFrame * Add comment for facebook/jest#5147 * longer timeout ... — committed to ant-design/ant-design by afc163 6 years ago
- Add the defaultActiveTabKey property for the Card component (#9526) * Add the defaultActiveTabKey property for the Card component (close #8789, #8942) * `activeTabKey` should be added * Improve... — committed to ant-design/ant-design by u3u 6 years ago
You can mock
requestAnimationFrame
with your own implementation.Here is a workaround:
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 withraf
(PR welcome as well!), but closing this asraf
does work as it should in Jest 22