Skip to content

Commit 5485abd

Browse files
committed
fix(jest-runtime): Guard '_isMockFunction' access with 'in'
1 parent 6460335 commit 5485abd

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed
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+
runtime.resetModules();
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)