From 2c9e9c55bc23520009b4edfebf91e55d86ba6e06 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Thu, 8 Jun 2023 15:36:14 -0400 Subject: [PATCH] fix(jest-runtime): Guard '_isMockFunction' access with 'in' --- ...untime_resetModules_unsafe_global_proxy.js | 34 +++++++++++++++++++ packages/jest-runtime/src/index.ts | 1 + 2 files changed, 35 insertions(+) create mode 100644 packages/jest-runtime/src/__tests__/runtime_resetModules_unsafe_global_proxy.js diff --git a/packages/jest-runtime/src/__tests__/runtime_resetModules_unsafe_global_proxy.js b/packages/jest-runtime/src/__tests__/runtime_resetModules_unsafe_global_proxy.js new file mode 100644 index 000000000000..d2da6ce71bb0 --- /dev/null +++ b/packages/jest-runtime/src/__tests__/runtime_resetModules_unsafe_global_proxy.js @@ -0,0 +1,34 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + */ + +'use strict'; + +import NodeEnvironment from 'jest-environment-node'; + +let createRuntime; + +describe('Runtime', () => { + beforeEach(() => { + createRuntime = require('createRuntime'); + }); + + describe('resetModules', () => { + it('does not throw when accessing _isMockFunction on an unsafe global', async () => { + const runtime = await createRuntime(__filename); + runtime._environment.global.UNSAFE_GLOBAL = new Proxy( + {}, + { + get(target, p, receiver) { + if (p === '_isMockFunction') throw new Error('Unsafe global!'); + }, + }, + ); + runtime.resetModules(); + }); + }); +}); diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index f9945f8e7f72..8de2690977dd 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -1218,6 +1218,7 @@ export default class Runtime { if ( ((typeof globalMock === 'object' && globalMock !== null) || typeof globalMock === 'function') && + '_isMockFunction' in globalMock && globalMock._isMockFunction === true ) { globalMock.mockClear();