Skip to content

Commit

Permalink
Fixed the bug where mocking a file with the filename resolved as back…
Browse files Browse the repository at this point in the history
…ticks 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
  • Loading branch information
rafaelramalho19 authored and cpojer committed Feb 1, 2018
1 parent 03bf2a9 commit c885f41
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

### Fixes

* `[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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +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',
};
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';
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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, {
Expand Down Expand Up @@ -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');
});
});
2 changes: 1 addition & 1 deletion packages/babel-plugin-jest-hoist/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit c885f41

Please sign in to comment.