Skip to content

Commit

Permalink
fix(jest-runtime): Guard '_isMockFunction' access with 'in'
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuckton committed Jun 8, 2023
1 parent 6460335 commit 2c9e9c5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
@@ -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();
});
});
});
1 change: 1 addition & 0 deletions packages/jest-runtime/src/index.ts
Expand Up @@ -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();
Expand Down

0 comments on commit 2c9e9c5

Please sign in to comment.