From da359c312a497295796b7e3510be17dcb26139fd Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Mon, 12 Oct 2015 11:50:43 -0700 Subject: [PATCH] Fix jest test that runs the polyfill 10 times MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../InitializeJavaScriptAppEngine.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/Libraries/JavaScriptAppEngine/Initialization/InitializeJavaScriptAppEngine.js b/Libraries/JavaScriptAppEngine/Initialization/InitializeJavaScriptAppEngine.js index 5a1015758..bc10123ac 100644 --- a/Libraries/JavaScriptAppEngine/Initialization/InitializeJavaScriptAppEngine.js +++ b/Libraries/JavaScriptAppEngine/Initialization/InitializeJavaScriptAppEngine.js @@ -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}); }