Skip to content

Commit

Permalink
fix(jest-runtime): Guard _isMockFunction access with in (#14188)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuckton authored Jun 21, 2023
1 parent ab13484 commit ca0557b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
Original file line number Diff line number Diff line change
@@ -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!');
},
},
);
expect(() => runtime.resetModules()).not.toThrow();
});
});
});
1 change: 1 addition & 0 deletions packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
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 ca0557b

Please sign in to comment.