Fix jest test that runs the polyfill 10 times

Summary: @​public

jest is running the polyfill multiple times on the same environment (cc @cpojer, need to fix that!). By default jest doesn't have XMLHttpRequest polyfilled so it'll define a property with writable to be false. It'll fatal the second time it tries to override XMLHttpRequest.

The hacky workaround is to make properties that do not exist with writable: true. But the long term fix would be to make jest stop running the polyfill multiple times.

Reviewed By: @javache

Differential Revision: D2532019

fb-gh-sync-id: a82abf69541781a64a0744798c736f90833e28cb
This commit is contained in:
Christopher Chedeau 2015-10-12 11:50:43 -07:00 committed by facebook-github-bot-4
parent 62d0586e59
commit da359c312a
1 changed files with 12 additions and 5 deletions

View File

@ -46,26 +46,33 @@ function handleError(e, isFatal) {
/**
* Assigns a new global property, replacing the existing one if there is one.
*
*
* Existing properties are preserved as `originalPropertyName`. Both properties
* will maintain the same enumerability & configurability.
*
*
* This allows you to undo the more aggressive polyfills, should you need to.
* For example, if you want to route network requests through DevTools (to trace
* them):
*
* GLOBAL.XMLHTTPRequest = GLOBAL.originalXMLHTTPRequest;
*
* global.XMLHttpRequest = global.originalXMLHttpRequest;
*
* For more info on that particular case, see:
* https://github.com/facebook/react-native/issues/934
*/
function polyfillGlobal(name, newValue, scope=GLOBAL) {
var descriptor = Object.getOwnPropertyDescriptor(scope, name);
var descriptor = Object.getOwnPropertyDescriptor(scope, name) || {
// jest for some bad reasons runs the polyfill code multiple times. In jest
// environment, XmlHttpRequest doesn't exist so getOwnPropertyDescriptor
// returns undefined and defineProperty default for writable is false.
// Therefore, the second time it runs, defineProperty will fatal :(
writable: true,
};
if (scope[name] !== undefined) {
var backupName = `original${name[0].toUpperCase()}${name.substr(1)}`;
Object.defineProperty(scope, backupName, {...descriptor, value: scope[name]});
}
Object.defineProperty(scope, name, {...descriptor, value: newValue});
}