diff --git a/CHANGELOG.md b/CHANGELOG.md index 72b2a46da94f..b1d28937ce77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - `[jest-config]` Add missing options to the `defaults` object ([#6428](https://github.com/facebook/jest/pull/6428)) - `[expect]` Using symbolic property names in arrays no longer causes the `toEqual` matcher to fail ([#6391](https://github.com/facebook/jest/pull/6391)) - `[expect]` `toEqual` no longer tries to compare non-enumerable symbolic properties, to be consistent with non-symbolic properties. ([#6398](https://github.com/facebook/jest/pull/6398)) +- `[jest-mock]` Fix `MockNativeMethods` access in react-native `jest.mock()` ([#6505](https://github.com/facebook/jest/pull/6505)) ### Chore & Maintenance diff --git a/e2e/babel-plugin-jest-hoist/__test_modules__/f.js b/e2e/babel-plugin-jest-hoist/__test_modules__/f.js new file mode 100644 index 000000000000..46b073c7400c --- /dev/null +++ b/e2e/babel-plugin-jest-hoist/__test_modules__/f.js @@ -0,0 +1,8 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +export default () => 'unmocked'; diff --git a/e2e/babel-plugin-jest-hoist/__tests__/integration.test.js b/e2e/babel-plugin-jest-hoist/__tests__/integration.test.js index 2d09b1122a70..c825984639b3 100644 --- a/e2e/babel-plugin-jest-hoist/__tests__/integration.test.js +++ b/e2e/babel-plugin-jest-hoist/__tests__/integration.test.js @@ -61,6 +61,10 @@ jest.dontMock('../__test_modules__/Mocked'); const myObject = {mock: () => {}}; myObject.mock('apple', 27); +// Variable names prefixed with `mock` (ignore case) should not throw as out-of-scope +const MockMethods = () => {}; +jest.mock('../__test_modules__/f', () => MockMethods); + describe('babel-plugin-jest-hoist', () => { it('does not throw during transform', () => { const object = {}; diff --git a/packages/babel-plugin-jest-hoist/src/index.js b/packages/babel-plugin-jest-hoist/src/index.js index 5ace55013709..ba375a54cfd7 100644 --- a/packages/babel-plugin-jest-hoist/src/index.js +++ b/packages/babel-plugin-jest-hoist/src/index.js @@ -110,7 +110,7 @@ FUNCTIONS.mock = args => { if (!found) { invariant( (scope.hasGlobal(name) && WHITELISTED_IDENTIFIERS[name]) || - /^mock/.test(name) || + /^mock/i.test(name) || // Allow istanbul's coverage variable to pass. /^(?:__)?cov/.test(name), 'The module factory of `jest.mock()` is not allowed to ' + @@ -123,7 +123,7 @@ FUNCTIONS.mock = args => { '.\n' + 'Note: This is a precaution to guard against uninitialized mock ' + 'variables. If it is ensured that the mock is required lazily, ' + - 'variable names prefixed with `mock` are permitted.', + 'variable names prefixed with `mock` (case insensitive) are permitted.', ); } }