From ba119e8caf806fe127425b2c9cdb7988e681d13f Mon Sep 17 00:00:00 2001 From: Rafael Ramalho Date: Tue, 30 Jan 2018 14:29:50 +0000 Subject: [PATCH 1/6] Fixed the bug where mocking a file with the filename resolved as backticks would fail --- .../__test_modules__/__mocks__/jest-backticks.js | 3 +++ .../__test_modules__/jest-backticks.js | 8 ++++++++ .../babel-plugin-jest-hoist/__tests__/integration.test.js | 6 ++++++ packages/babel-plugin-jest-hoist/src/index.js | 2 +- 4 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 integration-tests/babel-plugin-jest-hoist/__test_modules__/__mocks__/jest-backticks.js create mode 100644 integration-tests/babel-plugin-jest-hoist/__test_modules__/jest-backticks.js diff --git a/integration-tests/babel-plugin-jest-hoist/__test_modules__/__mocks__/jest-backticks.js b/integration-tests/babel-plugin-jest-hoist/__test_modules__/__mocks__/jest-backticks.js new file mode 100644 index 000000000000..37bbbcdab8e0 --- /dev/null +++ b/integration-tests/babel-plugin-jest-hoist/__test_modules__/__mocks__/jest-backticks.js @@ -0,0 +1,3 @@ +module.exports = { + name: 'backticks-with-jest', +}; diff --git a/integration-tests/babel-plugin-jest-hoist/__test_modules__/jest-backticks.js b/integration-tests/babel-plugin-jest-hoist/__test_modules__/jest-backticks.js new file mode 100644 index 000000000000..46b073c7400c --- /dev/null +++ b/integration-tests/babel-plugin-jest-hoist/__test_modules__/jest-backticks.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/integration-tests/babel-plugin-jest-hoist/__tests__/integration.test.js b/integration-tests/babel-plugin-jest-hoist/__tests__/integration.test.js index cc2a708121cd..53e8ca19e78a 100644 --- a/integration-tests/babel-plugin-jest-hoist/__tests__/integration.test.js +++ b/integration-tests/babel-plugin-jest-hoist/__tests__/integration.test.js @@ -19,6 +19,7 @@ import b from '../__test_modules__/b'; import c from '../__test_modules__/c'; import d from '../__test_modules__/d'; import e from '../__test_modules__/e'; +import jestBackticks from '../__test_modules__/jest-backticks'; // The virtual mock call below will be hoisted above this `require` call. const virtualModule = require('virtual-module'); @@ -44,6 +45,7 @@ jest.mock('../__test_modules__/e', () => { }, }; }); +jest.mock('../__test_modules__/jest-backticks'); jest.mock('virtual-module', () => 'kiwi', {virtual: true}); // This has types that should be ignored by the out-of-scope variables check. jest.mock('has-flow-types', () => (props: {children: mixed}) => 3, { @@ -125,4 +127,8 @@ describe('babel-plugin-jest-hoist', () => { it('works with virtual modules', () => { expect(virtualModule).toBe('kiwi'); }); + + it('works if the file name is mocked via backticks and defined in the "__mocks__" directory', () => { + expect(jestBackticks.name).toBe('backticks-with-jest'); + }); }); diff --git a/packages/babel-plugin-jest-hoist/src/index.js b/packages/babel-plugin-jest-hoist/src/index.js index 3ac3b9f55a33..de290a3f006d 100644 --- a/packages/babel-plugin-jest-hoist/src/index.js +++ b/packages/babel-plugin-jest-hoist/src/index.js @@ -78,7 +78,7 @@ const IDVisitor = { const FUNCTIONS: Object = Object.create(null); FUNCTIONS.mock = args => { if (args.length === 1) { - return args[0].isStringLiteral(); + return args[0].isStringLiteral() || args[0].isLiteral(); } else if (args.length === 2 || args.length === 3) { const moduleFactory = args[1]; invariant( From 1ced7efefd6062e9ae70a1cc1138b577a9990ca7 Mon Sep 17 00:00:00 2001 From: Rafael Ramalho Date: Tue, 30 Jan 2018 17:02:52 +0000 Subject: [PATCH 2/6] Updated changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2822ebb21d9d..1259abde4b4d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ ### Fixes +* `[mock]` Add backticks support (\`\`) to `mock` a certain package via the `__mocks__` folder. * `[jest]` Add `import-local` to `jest` package. ([#5353](https://github.com/facebook/jest/pull/5353)) * `[expect]` Support class instances in `.toHaveProperty()` matcher. From fb87d1a0855dd60c669d0c55a2ced7bcda30487a Mon Sep 17 00:00:00 2001 From: Rafael Ramalho Date: Tue, 30 Jan 2018 17:10:50 +0000 Subject: [PATCH 3/6] Added license info --- .../__test_modules__/__mocks__/jest-backticks.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/integration-tests/babel-plugin-jest-hoist/__test_modules__/__mocks__/jest-backticks.js b/integration-tests/babel-plugin-jest-hoist/__test_modules__/__mocks__/jest-backticks.js index 37bbbcdab8e0..66ba92e31490 100644 --- a/integration-tests/babel-plugin-jest-hoist/__test_modules__/__mocks__/jest-backticks.js +++ b/integration-tests/babel-plugin-jest-hoist/__test_modules__/__mocks__/jest-backticks.js @@ -1,3 +1,10 @@ +/** + * 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. + */ + module.exports = { name: 'backticks-with-jest', }; From 12a12f1b9a1f550b28de856a2aea9a34237d6294 Mon Sep 17 00:00:00 2001 From: Rafael Ramalho Date: Wed, 31 Jan 2018 10:14:07 +0000 Subject: [PATCH 4/6] Fixed the unit test related to the backticks issue and updated the changelog with the PR link --- CHANGELOG.md | 2 +- .../babel-plugin-jest-hoist/__tests__/integration.test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b5d9f2c81a2..c8ecafd7eebe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ ### Fixes -* `[mock]` Add backticks support (\`\`) to `mock` a certain package via the `__mocks__` folder. +* `[mock]` Add backticks support (\`\`) to `mock` a certain package via the `__mocks__` folder.([#5426](https://github.com/facebook/jest/pull/5426)) * `[jest-message-util]` Prevent an `ENOENT` crash when the test file contained a malformed source-map. ([#5405](https://github.com/facebook/jest/pull/5405)). * `[jest]` Add `import-local` to `jest` package. diff --git a/integration-tests/babel-plugin-jest-hoist/__tests__/integration.test.js b/integration-tests/babel-plugin-jest-hoist/__tests__/integration.test.js index 53e8ca19e78a..2d09b1122a70 100644 --- a/integration-tests/babel-plugin-jest-hoist/__tests__/integration.test.js +++ b/integration-tests/babel-plugin-jest-hoist/__tests__/integration.test.js @@ -45,7 +45,7 @@ jest.mock('../__test_modules__/e', () => { }, }; }); -jest.mock('../__test_modules__/jest-backticks'); +jest.mock(`../__test_modules__/jest-backticks`); jest.mock('virtual-module', () => 'kiwi', {virtual: true}); // This has types that should be ignored by the out-of-scope variables check. jest.mock('has-flow-types', () => (props: {children: mixed}) => 3, { From 9a728113a4447336b4b6f92115c8314b4e63eaa7 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 1 Feb 2018 07:27:10 +0100 Subject: [PATCH 5/6] Update CHANGELOG.md --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c8ecafd7eebe..2c3f9b96f800 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,8 @@ ### Fixes -* `[mock]` Add backticks support (\`\`) to `mock` a certain package via the `__mocks__` folder.([#5426](https://github.com/facebook/jest/pull/5426)) +* `[jest-mock]` Add backticks support (\`\`) to `mock` a certain package via the + `__mocks__` folder.([#5426](https://github.com/facebook/jest/pull/5426)) * `[jest-message-util]` Prevent an `ENOENT` crash when the test file contained a malformed source-map. ([#5405](https://github.com/facebook/jest/pull/5405)). * `[jest]` Add `import-local` to `jest` package. From 932ae3977d411f6b16867eeddeb98b7d938472c4 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 1 Feb 2018 07:27:54 +0100 Subject: [PATCH 6/6] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c3f9b96f800..9efc1df442c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ ### Fixes * `[jest-mock]` Add backticks support (\`\`) to `mock` a certain package via the - `__mocks__` folder.([#5426](https://github.com/facebook/jest/pull/5426)) + `__mocks__` folder. ([#5426](https://github.com/facebook/jest/pull/5426)) * `[jest-message-util]` Prevent an `ENOENT` crash when the test file contained a malformed source-map. ([#5405](https://github.com/facebook/jest/pull/5405)). * `[jest]` Add `import-local` to `jest` package.