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 warning with React 16 #4545

Closed
taion opened this issue Sep 27, 2017 · 30 comments · Fixed by #4568
Closed

requestAnimationFrame warning with React 16 #4545

taion opened this issue Sep 27, 2017 · 30 comments · Fixed by #4568

Comments

@taion
Copy link
Contributor

taion commented Sep 27, 2017

Do you want to request a feature or report a bug?
Bug-ish

What is the current behavior?
Warning emitted:

  ● Console

    console.error node_modules/fbjs/lib/warning.js:33
      Warning: React depends on requestAnimationFrame. Make sure that you load a polyfill in older browsers. http://fb.me/react-polyfills

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.
Run any Jest tests with React 16

What is the expected behavior?
No warning

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.
Jest v21.2.0
jest-environment-jsdom-11.0.0 v20.0.9

This would be resolved by jsdom/jsdom#1963 upstream. It's just sort of an annoying warning, and I don't want to define a custom environment just to stub polyfill rAF.

I think this is trackable here given that the React docs recommend Jest: https://facebook.github.io/react/docs/test-utils.html, and that it'd be ideal for Jest tests to not emit warnings when used with the latest React release.

@thymikee
Copy link
Collaborator

Please see this issue: #1644

@taion
Copy link
Contributor Author

taion commented Sep 27, 2017

I think this is qualitatively different because React Fiber out-of-the-box now expects RAF to be available. I'm not using RAF at all in my code under test – the warning comes from React itself.

@thymikee
Copy link
Collaborator

@cpojer @aaronabramov should we provide a default polyfill then?

@mbifulco
Copy link

Ditto - I'm getting the same warnings across my test suites. If the answer it so provide our own polyfill in the test environment, so be it... but I'm not sure how to do that for all my component test cases. Any tips there?

@cpojer
Copy link
Member

cpojer commented Sep 27, 2017

Yeah, let's add one to our jsdom env.

@taion
Copy link
Contributor Author

taion commented Sep 27, 2017

Can we re-open this issue to track that?

@thymikee thymikee reopened this Sep 27, 2017
@jameslockwood
Copy link

jameslockwood commented Sep 28, 2017

@mbifulco I managed to get this working for all test cases by loading a simple shim before each spec, using the setupFiles property in the jest config:

shim.js:

global.requestAnimationFrame = (callback) => {
    setTimeout(callback, 0);
};

jest config json:

"setupFiles": ["<rootDir>/path/to/shim.js", "<rootDir>/path/to/setup.js"]

The shim must be the first file in the setupFiles array if you have any other setup files to load (e.g. setup.js in the example above). If any dependencies are imported before the shim has loaded, then you'll still get the warning when running tests.

The second setup.js file is used to configure the react adapter:

import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

Enzyme.configure({ adapter: new Adapter() });

Versions: jest 21.2.0, react 16.0.0

@mbifulco
Copy link

Beautiful, thank you @jameslockwood - I'll give that a shot.

@SimenB
Copy link
Member

SimenB commented Sep 28, 2017

Should we include a proper polyfill, or just global.requestAnimationFrame = callback => setTimeout(callback, 0);?

@thymikee
Copy link
Collaborator

I think we should go with whatever React team recommends now.

@jameslockwood
Copy link

To quote the blog https://facebook.github.io/react/blog/2017/09/26/react-v16.0.html

React also depends on requestAnimationFrame (even in test environments). A simple shim for test environments would be:

global.requestAnimationFrame = function(callback) {
 setTimeout(callback, 0);
};```

@mbifulco
Copy link

mbifulco commented Sep 28, 2017

Ah, I can't use "setupFiles" in jest config, because I'm in a create-react-app environment that hasn't ejected yet. Looks like I should be keeping an eye on this issue on create-react-app.

yarn test v1.0.1
$ react-scripts test --env=jsdom --coverage
Out of the box, Create React App only supports overriding these Jest options:

  • collectCoverageFrom
  • coverageReporters
  • coverageThreshold
  • snapshotSerializers.

These options in your package.json Jest configuration are not currently supported by Create React App:

  • setupFiles

If you wish to override other Jest options, you need to eject from the default setup. You can do so by running npm run eject but remember that this is a one-way operation. You may also file an issue with Create React App to discuss supporting more options out of the box.

error Command failed with exit code 1.

@Timer
Copy link

Timer commented Sep 28, 2017

@mbifulco

Create React App supports test setup, simply create a file named src/setupTests.js and add your polyfill to it, e.g.:

global.requestAnimationFrame = function(callback) {
  setTimeout(callback, 0);
};

@sebastiangraef
Copy link

sebastiangraef commented Sep 28, 2017

Just including it in setupTests.js didn't fix it for me, but creating a new file with the polyfill and importing that at the top of setupTests.js does it. Like mentioned by @jameslockwood over here:

facebook/create-react-app#3199 (comment)

@lukerollans
Copy link

lukerollans commented Sep 29, 2017

For anyone looking for a complete, temporary workaround, my src/setupTests.js looks like this

// TODO: Remove this `raf` polyfill once the below issue is sorted
// https://github.com/facebookincubator/create-react-app/issues/3199#issuecomment-332842582
import raf from './tempPolyfills'

import Enzyme  from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

Enzyme.configure({ adapter: new Adapter() });

and src/tempPolyfills.js

const raf = global.requestAnimationFrame = (cb) => {
  setTimeout(cb, 0)
}

export default raf

aaronjensen added a commit to aaronjensen/react-fetch-component that referenced this issue Sep 29, 2017
aaronjensen added a commit to aaronjensen/react-fetch-component that referenced this issue Sep 29, 2017
@markozxuu
Copy link

@jameslockwood Works with create-react-app ? 😄

SimenB added a commit to SimenB/jest that referenced this issue Sep 30, 2017
cpojer pushed a commit that referenced this issue Sep 30, 2017
* Add simple rAF polyfill in jsdom environment

Closes #4545

* Fix flow error

* Tweak test

* Try to log out stderr on CI

* Use snake case naming for test file

* Update to newest yarn on ci

* Revert "Try to log out stderr on CI"

This reverts commit 08d58c5.

* Remove extra -- from appveyor to avoid warning on newer yarn

* Include time since window initialised in rAF implementation
rgarner added a commit to barnardos/consent-form-builder that referenced this issue Nov 21, 2017
rgarner added a commit to barnardos/consent-form-builder that referenced this issue Nov 23, 2017
rgarner added a commit to barnardos/consent-form-builder that referenced this issue Nov 27, 2017
rgarner added a commit to barnardos/consent-form-builder that referenced this issue Nov 27, 2017
rgarner added a commit to barnardos/consent-form-builder that referenced this issue Nov 27, 2017
rgarner added a commit to barnardos/consent-form-builder that referenced this issue Nov 27, 2017
joelcarr pushed a commit to barnardos/consent-form-builder that referenced this issue Nov 28, 2017
rgarner added a commit to barnardos/consent-form-builder that referenced this issue Nov 28, 2017
joelcarr pushed a commit to barnardos/consent-form-builder that referenced this issue Nov 28, 2017
vanderhoop added a commit to stride-nyc/remote_retro that referenced this issue Nov 29, 2017
rgarner added a commit to barnardos/consent-form-builder that referenced this issue Nov 29, 2017
joelcarr pushed a commit to barnardos/consent-form-builder that referenced this issue Nov 29, 2017
rgarner added a commit to barnardos/consent-form-builder that referenced this issue Nov 29, 2017
ryota-murakami added a commit to ryota-murakami/clock-up that referenced this issue Dec 27, 2017
robbieaverill added a commit to creative-commoners/silverstripe-ckan-registry that referenced this issue Jan 10, 2019
ScopeyNZ pushed a commit to creative-commoners/silverstripe-ckan-registry that referenced this issue Jan 15, 2019
@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 May 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.