Skip to content

Commit c885f41

Browse files
rafaelramalho19cpojer
authored andcommitted
Fixed the bug where mocking a file with the filename resolved as backticks would fail (#5426)
* Fixed the bug where mocking a file with the filename resolved as backticks would fail * Updated changelog * Added license info * Fixed the unit test related to the backticks issue and updated the changelog with the PR link * Update CHANGELOG.md * Update CHANGELOG.md
1 parent 03bf2a9 commit c885f41

File tree

5 files changed

+27
-1
lines changed

5 files changed

+27
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
### Fixes
99

10+
* `[jest-mock]` Add backticks support (\`\`) to `mock` a certain package via the
11+
`__mocks__` folder. ([#5426](https://github.com/facebook/jest/pull/5426))
1012
* `[jest-message-util]` Prevent an `ENOENT` crash when the test file contained a
1113
malformed source-map. ([#5405](https://github.com/facebook/jest/pull/5405)).
1214
* `[jest]` Add `import-local` to `jest` package.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
module.exports = {
9+
name: 'backticks-with-jest',
10+
};
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';

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import b from '../__test_modules__/b';
1919
import c from '../__test_modules__/c';
2020
import d from '../__test_modules__/d';
2121
import e from '../__test_modules__/e';
22+
import jestBackticks from '../__test_modules__/jest-backticks';
2223

2324
// The virtual mock call below will be hoisted above this `require` call.
2425
const virtualModule = require('virtual-module');
@@ -44,6 +45,7 @@ jest.mock('../__test_modules__/e', () => {
4445
},
4546
};
4647
});
48+
jest.mock(`../__test_modules__/jest-backticks`);
4749
jest.mock('virtual-module', () => 'kiwi', {virtual: true});
4850
// This has types that should be ignored by the out-of-scope variables check.
4951
jest.mock('has-flow-types', () => (props: {children: mixed}) => 3, {
@@ -125,4 +127,8 @@ describe('babel-plugin-jest-hoist', () => {
125127
it('works with virtual modules', () => {
126128
expect(virtualModule).toBe('kiwi');
127129
});
130+
131+
it('works if the file name is mocked via backticks and defined in the "__mocks__" directory', () => {
132+
expect(jestBackticks.name).toBe('backticks-with-jest');
133+
});
128134
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ const IDVisitor = {
7878
const FUNCTIONS: Object = Object.create(null);
7979
FUNCTIONS.mock = args => {
8080
if (args.length === 1) {
81-
return args[0].isStringLiteral();
81+
return args[0].isStringLiteral() || args[0].isLiteral();
8282
} else if (args.length === 2 || args.length === 3) {
8383
const moduleFactory = args[1];
8484
invariant(

0 commit comments

Comments
 (0)