Skip to content

Commit 49d7bbf

Browse files
timwangdevcpojer
authored andcommitted
[babel-plugin-jest-hoist] Fix MockNativeMethods access in react-native (#6505)
* Fix MockNativeMethods access in react-native * Update changelog and RegExp * Update error message * Update CHANGELOG.md * Reverted code format * Add mock variable name test
1 parent 73990f7 commit 49d7bbf

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- `[jest-config]` Add missing options to the `defaults` object ([#6428](https://github.com/facebook/jest/pull/6428))
66
- `[expect]` Using symbolic property names in arrays no longer causes the `toEqual` matcher to fail ([#6391](https://github.com/facebook/jest/pull/6391))
77
- `[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))
8+
- `[jest-mock]` Fix `MockNativeMethods` access in react-native `jest.mock()` ([#6505](https://github.com/facebook/jest/pull/6505))
89

910
### Chore & Maintenance
1011

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
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+
export default () => 'unmocked';

e2e/babel-plugin-jest-hoist/__tests__/integration.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ jest.dontMock('../__test_modules__/Mocked');
6161
const myObject = {mock: () => {}};
6262
myObject.mock('apple', 27);
6363

64+
// Variable names prefixed with `mock` (ignore case) should not throw as out-of-scope
65+
const MockMethods = () => {};
66+
jest.mock('../__test_modules__/f', () => MockMethods);
67+
6468
describe('babel-plugin-jest-hoist', () => {
6569
it('does not throw during transform', () => {
6670
const object = {};

packages/babel-plugin-jest-hoist/src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ FUNCTIONS.mock = args => {
110110
if (!found) {
111111
invariant(
112112
(scope.hasGlobal(name) && WHITELISTED_IDENTIFIERS[name]) ||
113-
/^mock/.test(name) ||
113+
/^mock/i.test(name) ||
114114
// Allow istanbul's coverage variable to pass.
115115
/^(?:__)?cov/.test(name),
116116
'The module factory of `jest.mock()` is not allowed to ' +
@@ -123,7 +123,7 @@ FUNCTIONS.mock = args => {
123123
'.\n' +
124124
'Note: This is a precaution to guard against uninitialized mock ' +
125125
'variables. If it is ensured that the mock is required lazily, ' +
126-
'variable names prefixed with `mock` are permitted.',
126+
'variable names prefixed with `mock` (case insensitive) are permitted.',
127127
);
128128
}
129129
}

0 commit comments

Comments
 (0)