Skip to content

Commit

Permalink
[babel-plugin-jest-hoist] Fix MockNativeMethods access in react-native (
Browse files Browse the repository at this point in the history
#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
  • Loading branch information
timwangdev authored and cpojer committed Jun 20, 2018
1 parent 73990f7 commit 49d7bbf
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions e2e/babel-plugin-jest-hoist/__test_modules__/f.js
Original file line number Diff line number Diff line change
@@ -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';
4 changes: 4 additions & 0 deletions e2e/babel-plugin-jest-hoist/__tests__/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-plugin-jest-hoist/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ' +
Expand All @@ -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.',
);
}
}
Expand Down

0 comments on commit 49d7bbf

Please sign in to comment.