From 02361c753f4e08b5c7a667027b1dfe90db0fdb07 Mon Sep 17 00:00:00 2001 From: Tim Wang Date: Wed, 20 Jun 2018 15:43:52 +0800 Subject: [PATCH 1/6] Fix MockNativeMethods access in react-native --- packages/babel-plugin-jest-hoist/src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel-plugin-jest-hoist/src/index.js b/packages/babel-plugin-jest-hoist/src/index.js index 5ace55013709..1b12c9be8855 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) || + /^[Mm]ock/.test(name) || // Allow istanbul's coverage variable to pass. /^(?:__)?cov/.test(name), 'The module factory of `jest.mock()` is not allowed to ' + From 29a6bf0e3b42ca7861955a0e5b98bd9160366df6 Mon Sep 17 00:00:00 2001 From: Tim Wang Date: Wed, 20 Jun 2018 15:57:50 +0800 Subject: [PATCH 2/6] Update changelog and RegExp --- CHANGELOG.md | 1 + packages/babel-plugin-jest-hoist/src/index.js | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 72b2a46da94f..5bf34d251688 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()` ### Chore & Maintenance diff --git a/packages/babel-plugin-jest-hoist/src/index.js b/packages/babel-plugin-jest-hoist/src/index.js index 1b12c9be8855..bf945085fd10 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]) || - /^[Mm]ock/.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 ' + From afaf802f55058da7339967a512c0b505779815f3 Mon Sep 17 00:00:00 2001 From: Tim Wang Date: Wed, 20 Jun 2018 16:01:13 +0800 Subject: [PATCH 3/6] Update error message --- packages/babel-plugin-jest-hoist/src/index.js | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/packages/babel-plugin-jest-hoist/src/index.js b/packages/babel-plugin-jest-hoist/src/index.js index bf945085fd10..fed11dda458c 100644 --- a/packages/babel-plugin-jest-hoist/src/index.js +++ b/packages/babel-plugin-jest-hoist/src/index.js @@ -71,7 +71,7 @@ const WHITELISTED_IDENTIFIERS = { }; Object.keys(global).forEach(name => (WHITELISTED_IDENTIFIERS[name] = true)); -const JEST_GLOBAL = {name: 'jest'}; +const JEST_GLOBAL = { name: 'jest' }; const IDVisitor = { ReferencedIdentifier(path) { this.ids.add(path); @@ -92,7 +92,7 @@ FUNCTIONS.mock = args => { const ids = new Set(); const parentScope = moduleFactory.parentPath.scope; - moduleFactory.traverse(IDVisitor, {ids}); + moduleFactory.traverse(IDVisitor, { ids }); for (const id of ids) { const name = id.node.name; let found = false; @@ -110,20 +110,20 @@ FUNCTIONS.mock = args => { if (!found) { invariant( (scope.hasGlobal(name) && WHITELISTED_IDENTIFIERS[name]) || - /^mock/i.test(name) || - // Allow istanbul's coverage variable to pass. - /^(?:__)?cov/.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 ' + - 'reference any out-of-scope variables.\n' + - 'Invalid variable access: ' + - name + - '\n' + - 'Whitelisted objects: ' + - Object.keys(WHITELISTED_IDENTIFIERS).join(', ') + - '.\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.', + 'reference any out-of-scope variables.\n' + + 'Invalid variable access: ' + + name + + '\n' + + 'Whitelisted objects: ' + + Object.keys(WHITELISTED_IDENTIFIERS).join(', ') + + '.\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` (case insensitive) are permitted.', ); } } From f6f29c2257121a859f43ddd696e4c0678ca74de8 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 20 Jun 2018 10:03:22 +0200 Subject: [PATCH 4/6] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5bf34d251688..b1d28937ce77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +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()` +- `[jest-mock]` Fix `MockNativeMethods` access in react-native `jest.mock()` ([#6505](https://github.com/facebook/jest/pull/6505)) ### Chore & Maintenance From 5e816ff1fb97601348735787a6621163f65a325a Mon Sep 17 00:00:00 2001 From: Tim Wang Date: Wed, 20 Jun 2018 16:10:09 +0800 Subject: [PATCH 5/6] Reverted code format --- packages/babel-plugin-jest-hoist/src/index.js | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/packages/babel-plugin-jest-hoist/src/index.js b/packages/babel-plugin-jest-hoist/src/index.js index fed11dda458c..ff0177352321 100644 --- a/packages/babel-plugin-jest-hoist/src/index.js +++ b/packages/babel-plugin-jest-hoist/src/index.js @@ -71,7 +71,7 @@ const WHITELISTED_IDENTIFIERS = { }; Object.keys(global).forEach(name => (WHITELISTED_IDENTIFIERS[name] = true)); -const JEST_GLOBAL = { name: 'jest' }; +const JEST_GLOBAL = {name: 'jest'}; const IDVisitor = { ReferencedIdentifier(path) { this.ids.add(path); @@ -92,7 +92,7 @@ FUNCTIONS.mock = args => { const ids = new Set(); const parentScope = moduleFactory.parentPath.scope; - moduleFactory.traverse(IDVisitor, { ids }); + moduleFactory.traverse(IDVisitor, {ids}); for (const id of ids) { const name = id.node.name; let found = false; @@ -110,20 +110,20 @@ FUNCTIONS.mock = args => { if (!found) { invariant( (scope.hasGlobal(name) && WHITELISTED_IDENTIFIERS[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 ' + - 'reference any out-of-scope variables.\n' + - 'Invalid variable access: ' + - name + - '\n' + - 'Whitelisted objects: ' + - Object.keys(WHITELISTED_IDENTIFIERS).join(', ') + - '.\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` (case insensitive) are permitted.', + /^mock/i.test(name) || + // Allow istanbul's coverage variable to pass. + /^(?:__)?cov/.test(name), + 'The module factory of `jest.mock()` is not allowed to ' + + 'reference any out-of-scope variables.\n' + + 'Invalid variable access: ' + + name + + '\n' + + 'Whitelisted objects: ' + + Object.keys(WHITELISTED_IDENTIFIERS).join(', ') + + '.\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` (case insensitive) are permitted.', ); } } From 366cb5bf5f99269c5632b849d3e147e6fd26d06f Mon Sep 17 00:00:00 2001 From: Tim Wang Date: Wed, 20 Jun 2018 16:36:17 +0800 Subject: [PATCH 6/6] Add mock variable name test --- e2e/babel-plugin-jest-hoist/__test_modules__/f.js | 8 ++++++++ e2e/babel-plugin-jest-hoist/__tests__/integration.test.js | 4 ++++ packages/babel-plugin-jest-hoist/src/index.js | 2 +- 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 e2e/babel-plugin-jest-hoist/__test_modules__/f.js 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 ff0177352321..ba375a54cfd7 100644 --- a/packages/babel-plugin-jest-hoist/src/index.js +++ b/packages/babel-plugin-jest-hoist/src/index.js @@ -113,7 +113,7 @@ FUNCTIONS.mock = args => { /^mock/i.test(name) || // Allow istanbul's coverage variable to pass. /^(?:__)?cov/.test(name), - 'The module factory of `jest.mock()` is not allowed to ' + + 'The module factory of `jest.mock()` is not allowed to ' + 'reference any out-of-scope variables.\n' + 'Invalid variable access: ' + name +