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

fix: unable to resolve path to mapped file with custom platform #7312

Merged
merged 5 commits into from
Nov 1, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions e2e/resolve/__tests__/resolve.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ test('should resolve filename.native.js', () => {
expect(platform.extension).toBe('native.js');
});

test('should resolve filename.native.js with moduleNameMapper', () => {
expect(testRequire('test2mapper')).not.toThrow();
expect(platform.extension).toBe('native.js');
});

test('should resolve filename.js', () => {
expect(testRequire('../test3')).not.toThrow();
expect(platform.extension).toBe('js');
Expand Down
9 changes: 7 additions & 2 deletions e2e/resolve/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
"name": "custom-resolve",
"jest": {
"haste": {
"platforms": ["native"],
"platforms": [
"native"
],
"defaultPlatform": "android"
},
"testEnvironment": "node"
"testEnvironment": "node",
"moduleNameMapper": {
"test2mapper": "<rootDir>/test2mapper"
}
}
}
8 changes: 8 additions & 0 deletions e2e/resolve/test2mapper.js
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.
*/

module.exports = {extension: '.js'};
8 changes: 8 additions & 0 deletions e2e/resolve/test2mapper.native.js
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.
*/

module.exports = {extension: 'native.js'};
1 change: 1 addition & 0 deletions packages/jest-resolve/src/__tests__/resolve.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ describe('getMockModule', () => {

const moduleMap = ModuleMap.create('/');
const resolver = new Resolver(moduleMap, {
extensions: ['.js'],
moduleNameMapper: [
{
moduleName: '$1',
Expand Down
15 changes: 14 additions & 1 deletion packages/jest-resolve/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,23 @@ class Resolver {
_resolveStubModuleName(from: Path, moduleName: string): ?Path {
const dirname = path.dirname(from);
const paths = this._options.modulePaths;
const extensions = this._options.extensions;
const extensions = this._options.extensions.slice();
const moduleDirectory = this._options.moduleDirectories;
const moduleNameMapper = this._options.moduleNameMapper;
const resolver = this._options.resolver;
const defaultPlatform = this._options.defaultPlatform;

if (this._supportsNativePlatform) {
extensions.unshift(
...this._options.extensions.map(ext => '.' + NATIVE_PLATFORM + ext),
);
}

if (defaultPlatform) {
extensions.unshift(
...this._options.extensions.map(ext => '.' + defaultPlatform + ext),
);
}

if (moduleNameMapper) {
for (const {moduleName: mappedModuleName, regex} of moduleNameMapper) {
Expand Down