Got this error after upgrading to React when I ran my Jest unit tests:
React depends on requestAnimationFrame. Make sure that you load a polyfill in older browsers.
How do I fix it?
I'm using Jest 18.1.0.
Got this error after upgrading to React when I ran my Jest unit tests:
React depends on requestAnimationFrame. Make sure that you load a polyfill in older browsers.
How do I fix it?
I'm using Jest 18.1.0.
If you are using create-react-app, some of these solutions will not work well (or at all, in the case of
setupFiles
). What does work well is creating a file atsrc/setupTests.js
and adding your mock in there:You can also add other global mocks in there (e.g.
localStorage
andnavigator.serviceWorker
).Turns out it was because I upgraded
enzyme
without upgradingreact
andreact-dom
.React 15.5 brings about some deprecations that caused many dependent libraries to have to update too. Make sure you are updating
react
,react-dom
and check the README of those dependent packages for new libraries you have to install. For instance, Enzyme 2.8.2 now requiresreact-addons-test-utils
as a dependency.Another working solution!
The idea is to load a simple shim before each spec, by using the setupFiles property in the jest config.
Create a file
shim.js
file (preferably in your root dir) and have this code in it:Next, you may be having redundant code that keep reappearing in all/most of your files - and you want to put them in a single file and have them run before each spec also, to do that:
Create a
setup.js
file in the root dir too. A good piece of redundant code to D.R.Y is the react enzyme adapter configuration code. Paste it hereNow create the
jest.config.js
file, to specify the paths of the two filesN.B: The jest config file take
json
, so make sure json's in. Also, if yourshim.js
andsetup.js
files are not in the same dir as yourjest.config.js
, adjust the path accordingly.Hope this helps!
Credit: https://github.com/facebook/jest/issues/4545
Just upgrade your
react-scripts
to 1.0.15 or above. It has been officially fixed after that version. See more details in https://github.com/facebook/create-react-app/issues/3199Search the comment of
gaearon commented on 31 Oct 2017
Here's a realistic way to mock requestAnimationFrame:
in your test, it repeatedly calls the RAF callback until it reaches the max elapsed time that you set. It happens instantly, so you don't need to stall it.
If you just need to polyfill it for tests, then you don't actually need the throttling.
Create a new file with this code:
Add that file to the
jest/setupFiles
array in your package.json.