🐛 proxy sandboxshould not delete untouched globals (#2310)

This commit is contained in:
李凤宝(Ted) 2022-10-20 10:55:40 +08:00 committed by GitHub
parent cec27b6c16
commit 6544b17f9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 1 deletions

View File

@ -502,6 +502,24 @@ describe('variables in whitelist', () => {
sandbox.inactive();
expect('__REACT_ERROR_OVERLAY_GLOBAL_HOOK__' in window).toBeFalsy();
});
it('should not modify those global context that have not been touched during the app running', () => {
const original = {
iframeReady: function t1() {},
};
window.__REACT_ERROR_OVERLAY_GLOBAL_HOOK__ = original;
const sandbox = new ProxySandbox('whitelist-variables');
const { proxy } = sandbox;
sandbox.active();
// globals in whitelist
proxy.System = {};
expect(window.System).toEqual({});
// regular globals
proxy.someOther = {};
expect('someOther' in window).toBeFalsy();
sandbox.inactive();
expect(window.__REACT_ERROR_OVERLAY_GLOBAL_HOOK__).toBe(original);
});
});
describe('should work with nest sandbox', () => {

View File

@ -149,7 +149,7 @@ export default class ProxySandbox implements SandBox {
if (process.env.NODE_ENV === 'test' || --activeSandboxCount === 0) {
// reset the global value to the prev value
globalVariableWhiteList.forEach((p) => {
Object.keys(this.globalWhitelistPrevDescriptor).forEach((p) => {
const descriptor = this.globalWhitelistPrevDescriptor[p];
if (descriptor) {
Object.defineProperty(this.globalContext, p, descriptor);