Skip to content

Commit da359c3

Browse files
vjeuxfacebook-github-bot-4
authored andcommitted
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
1 parent 62d0586 commit da359c3

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

Libraries/JavaScriptAppEngine/Initialization/InitializeJavaScriptAppEngine.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,33 @@ function handleError(e, isFatal) {
4646

4747
/**
4848
* Assigns a new global property, replacing the existing one if there is one.
49-
*
49+
*
5050
* Existing properties are preserved as `originalPropertyName`. Both properties
5151
* will maintain the same enumerability & configurability.
52-
*
52+
*
5353
* This allows you to undo the more aggressive polyfills, should you need to.
5454
* For example, if you want to route network requests through DevTools (to trace
5555
* them):
5656
*
57-
* GLOBAL.XMLHTTPRequest = GLOBAL.originalXMLHTTPRequest;
58-
*
57+
* global.XMLHttpRequest = global.originalXMLHttpRequest;
58+
*
5959
* For more info on that particular case, see:
6060
* https://github.com/facebook/react-native/issues/934
6161
*/
6262
function polyfillGlobal(name, newValue, scope=GLOBAL) {
63-
var descriptor = Object.getOwnPropertyDescriptor(scope, name);
63+
var descriptor = Object.getOwnPropertyDescriptor(scope, name) || {
64+
// jest for some bad reasons runs the polyfill code multiple times. In jest
65+
// environment, XmlHttpRequest doesn't exist so getOwnPropertyDescriptor
66+
// returns undefined and defineProperty default for writable is false.
67+
// Therefore, the second time it runs, defineProperty will fatal :(
68+
writable: true,
69+
};
6470

6571
if (scope[name] !== undefined) {
6672
var backupName = `original${name[0].toUpperCase()}${name.substr(1)}`;
6773
Object.defineProperty(scope, backupName, {...descriptor, value: scope[name]});
6874
}
75+
6976
Object.defineProperty(scope, name, {...descriptor, value: newValue});
7077
}
7178

0 commit comments

Comments
 (0)