Skip to content

Commit

Permalink
fix: unable to resolve path to mapped file with custom platform (#7312)
Browse files Browse the repository at this point in the history
  • Loading branch information
thymikee authored and SimenB committed Nov 1, 2018
1 parent 29ae868 commit f9fd98f
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 5 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@
- `[jest-config]` Use strings instead of `RegExp` instances in normalized configuration ([#7251](https://github.com/facebook/jest/pull/7251))
- `[jest-circus]` Make sure to display real duration even if time is mocked ([#7264](https://github.com/facebook/jest/pull/7264))
- `[expect]` Improves the failing message for `toStrictEqual` matcher. ([#7224](https://github.com/facebook/jest/pull/7224))
- `[jest-mock]` [**BREAKING**] Fix bugs with mock/spy result tracking of recursive functions ([#6381](https://github.com/facebook/jest/pull/6381)
- `[jest-mock]` [**BREAKING**] Fix bugs with mock/spy result tracking of recursive functions ([#6381](https://github.com/facebook/jest/pull/6381))
- `[jest-resolve]` Fix not being able to resolve path to mapped file with custom platform ([#7312](https://github.com/facebook/jest/pull/7312))

### Chore & Maintenance

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ exports[`moduleNameMapper wrong configuration 1`] = `
12 | module.exports = () => 'test';
13 |
at packages/jest-resolve/build/index.js:410:17
at packages/jest-resolve/build/index.js:429:17
at index.js:10:1
"
Expand Down
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

0 comments on commit f9fd98f

Please sign in to comment.