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 fc3d89e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
@@ -0,0 +1,31 @@
/**
* 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';

const { default: NodeEnvironment } = require('jest-environment-node');

Check failure on line 11 in packages/jest-runtime/src/__tests__/runtime_resetModules_unsafe_global_proxy.js

View workflow job for this annotation

GitHub Actions / Lint

Replace `·default:·NodeEnvironment·` with `default:·NodeEnvironment`

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({}, {

Check failure on line 23 in packages/jest-runtime/src/__tests__/runtime_resetModules_unsafe_global_proxy.js

View workflow job for this annotation

GitHub Actions / Lint

Replace `{},` with `⏎········{},⏎·······`
get(target, p, receiver) {

Check failure on line 24 in packages/jest-runtime/src/__tests__/runtime_resetModules_unsafe_global_proxy.js

View workflow job for this annotation

GitHub Actions / Lint

Insert `··`
if (p === "_isMockFunction") throw new Error("Unsafe global!");

Check failure on line 25 in packages/jest-runtime/src/__tests__/runtime_resetModules_unsafe_global_proxy.js

View workflow job for this annotation

GitHub Actions / Lint

Replace `··········if·(p·===·"_isMockFunction")·throw·new·Error("Unsafe·global!"` with `············if·(p·===·'_isMockFunction')·throw·new·Error('Unsafe·global!'`

Check failure on line 25 in packages/jest-runtime/src/__tests__/runtime_resetModules_unsafe_global_proxy.js

View workflow job for this annotation

GitHub Actions / Lint

Strings must use singlequote

Check failure on line 25 in packages/jest-runtime/src/__tests__/runtime_resetModules_unsafe_global_proxy.js

View workflow job for this annotation

GitHub Actions / Lint

Strings must use singlequote
}

Check failure on line 26 in packages/jest-runtime/src/__tests__/runtime_resetModules_unsafe_global_proxy.js

View workflow job for this annotation

GitHub Actions / Lint

Replace `}` with `··},`
});

Check failure on line 27 in packages/jest-runtime/src/__tests__/runtime_resetModules_unsafe_global_proxy.js

View workflow job for this annotation

GitHub Actions / Lint

Replace `}` with `··},⏎······`
runtime.resetModules();
});
});
});
2 changes: 1 addition & 1 deletion packages/jest-runtime/src/index.ts
Expand Up @@ -1218,7 +1218,7 @@ export default class Runtime {
if (
((typeof globalMock === 'object' && globalMock !== null) ||
typeof globalMock === 'function') &&
globalMock._isMockFunction === true
'_isMockFunction' in globalMock && globalMock._isMockFunction === true

Check failure on line 1221 in packages/jest-runtime/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint

Insert `⏎·············`
) {
globalMock.mockClear();
}
Expand Down

0 comments on commit fc3d89e

Please sign in to comment.