Skip to content

Commit ca0557b

Browse files
authored
fix(jest-runtime): Guard _isMockFunction access with in (#14188)
1 parent ab13484 commit ca0557b

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- `[jest-mock]` Improve user input validation and error messages of `spyOn` and `replaceProperty` methods ([#14087](https://github.com/facebook/jest/pull/14087))
1818
- `[jest-runtime]` Bind `jest.isolateModulesAsync` to `this` ([#14083](https://github.com/facebook/jest/pull/14083))
1919
- `[jest-runtime]` Forward `wrapperLength` to the `Script` constructor as `columnOffset` for accurate debugging ([#14148](https://github.com/facebook/jest/pull/14148))
20+
- `[jest-runtime]` Guard `_isMockFunction` access with `in` ([#14188](https://github.com/facebook/jest/pull/14188))
2021
- `[jest-snapshot]` Fix a potential bug when not using prettier and improve performance ([#14036](https://github.com/facebook/jest/pull/14036))
2122
- `[@jest/transform]` Do not instrument `.json` modules ([#14048](https://github.com/facebook/jest/pull/14048))
2223

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
*/
8+
9+
'use strict';
10+
11+
let createRuntime;
12+
13+
describe('Runtime', () => {
14+
beforeEach(() => {
15+
createRuntime = require('createRuntime');
16+
});
17+
18+
describe('resetModules', () => {
19+
it('does not throw when accessing _isMockFunction on an unsafe global', async () => {
20+
const runtime = await createRuntime(__filename);
21+
runtime._environment.global.UNSAFE_GLOBAL = new Proxy(
22+
{},
23+
{
24+
get(target, p, receiver) {
25+
if (p === '_isMockFunction') throw new Error('Unsafe global!');
26+
},
27+
},
28+
);
29+
expect(() => runtime.resetModules()).not.toThrow();
30+
});
31+
});
32+
});

packages/jest-runtime/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,7 @@ export default class Runtime {
12181218
if (
12191219
((typeof globalMock === 'object' && globalMock !== null) ||
12201220
typeof globalMock === 'function') &&
1221+
'_isMockFunction' in globalMock &&
12211222
globalMock._isMockFunction === true
12221223
) {
12231224
globalMock.mockClear();

0 commit comments

Comments
 (0)