Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the bug where mocking a file with the filename resolved as backticks would fail #5426

Merged
merged 7 commits into from
Feb 1, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

### Fixes

* `[mock]` Add backticks support (\`\`) to `mock` a certain package via the `__mocks__` folder.([#5426](https://github.com/facebook/jest/pull/5426))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be [jest-mock]?

Copy link
Member

@SimenB SimenB Feb 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. I meant to fix that - forgot, though. Thanks!

* `[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