From 5485abd6ea44d975a789c97af11db4537d339a41 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Thu, 8 Jun 2023 15:36:14 -0400 Subject: [PATCH 1/3] fix(jest-runtime): Guard '_isMockFunction' access with 'in' --- ...untime_resetModules_unsafe_global_proxy.js | 32 +++++++++++++++++++ packages/jest-runtime/src/index.ts | 1 + 2 files changed, 33 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..e2f8241cca09 --- /dev/null +++ b/packages/jest-runtime/src/__tests__/runtime_resetModules_unsafe_global_proxy.js @@ -0,0 +1,32 @@ +/** + * 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'; + +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(); From 0446565fc072a7b030dbf0cceb26f0851cb9ed6e Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 21 Jun 2023 10:53:38 +0200 Subject: [PATCH 2/3] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 921c44b96219..33f9a37d00fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ - `[jest-mock]` Improve user input validation and error messages of `spyOn` and `replaceProperty` methods ([#14087](https://github.com/facebook/jest/pull/14087)) - `[jest-runtime]` Bind `jest.isolateModulesAsync` to `this` ([#14083](https://github.com/facebook/jest/pull/14083)) - `[jest-runtime]` Forward `wrapperLength` to the `Script` constructor as `columnOffset` for accurate debugging ([#14148](https://github.com/facebook/jest/pull/14148)) +- `[jest-runtime]` Guard `_isMockFunction` access with `in` ([#14188](https://github.com/facebook/jest/pull/14188)) - `[jest-snapshot]` Fix a potential bug when not using prettier and improve performance ([#14036](https://github.com/facebook/jest/pull/14036)) - `[@jest/transform]` Do not instrument `.json` modules ([#14048](https://github.com/facebook/jest/pull/14048)) From 92cfa13ac09305520dc998e45470c5396db43f51 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 21 Jun 2023 10:56:40 +0200 Subject: [PATCH 3/3] add assertion --- .../src/__tests__/runtime_resetModules_unsafe_global_proxy.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index e2f8241cca09..b276b2f12e3b 100644 --- a/packages/jest-runtime/src/__tests__/runtime_resetModules_unsafe_global_proxy.js +++ b/packages/jest-runtime/src/__tests__/runtime_resetModules_unsafe_global_proxy.js @@ -26,7 +26,7 @@ describe('Runtime', () => { }, }, ); - runtime.resetModules(); + expect(() => runtime.resetModules()).not.toThrow(); }); }); });