react-timer-mixin icon indicating copy to clipboard operation
react-timer-mixin copied to clipboard

Change the order of preference for setting GLOBAL

Open connected-mgosbee opened this issue 9 years ago • 4 comments

When using jsdom in combination with Enzyme for testing react-native using jest we run into an error where window.requestAnimationFrame is undefined. However, running in the node context, global.requestAnimationFrame is made available when testing. Since GLOBAL preferred window over global when both exist it causes errors in testing React-Native applications in Jest and jsdom.

For example, the following code causes TimerMixin to fail when testing a React application.

var jsdom = require('jsdom').jsdom;
global.document = jsdom('');
global.window = document.defaultView;
Object.keys(document.defaultView).forEach((property) => {
    if (typeof global[property] === 'undefined') {
        global[property] = document.defaultView[property];
    }
});
global.navigator = {
    userAgent: 'node.js'
};

You can see global.window is being set, and will definitely cause TimerMixin to fail. It would be safer even in web contexts when assuming window.global would be undefined.

Read more here at AirBnb docs. http://airbnb.io/enzyme/docs/guides/jsdom.html

connected-mgosbee avatar Nov 29 '16 16:11 connected-mgosbee

There is no reason, from what I can tell, for the CI test to fail. Can I get someone to look at this?

connected-mgosbee avatar Nov 29 '16 16:11 connected-mgosbee

This is a little tough. When global and window are both present but differ, global is generally the true global object. It's not clear in what cases it would be better to use window instead.

sophiebits avatar Nov 29 '16 17:11 sophiebits

(That is, your case makes some sense but I'm not sure if there are others or if there are ones where global would be better.)

sophiebits avatar Nov 29 '16 17:11 sophiebits

I was unable to think of a scenario when window.global would have to exist in Web context. I do understand the hesitation, but as far as I can tell, using window first likely wouldn't be necessary.

Another possibility is to explicitly check for requestAnimationFrame, setTimeout, setInterval, etc... in either global or window. Where it exists, use that.

Does that make more sense?

connected-mgosbee avatar Nov 29 '16 18:11 connected-mgosbee