From 0caffd38159cc0535d45a636d922a77385bcecb0 Mon Sep 17 00:00:00 2001 From: Mike Lewis Date: Wed, 11 Apr 2018 22:14:51 +0100 Subject: [PATCH] Generalise package main resolving issue Resolves #5967 --- .../__mocks__/mock-module-alt/index.js | 8 ++++++++ .../__mocks__/mock-module-alt/package.json | 4 ++++ .../__tests__/resolve-node-module.test.js | 7 ++++++- packages/jest-resolve/src/default_resolver.js | 6 +++++- 4 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 integration-tests/resolve-node-module/__mocks__/mock-module-alt/index.js create mode 100644 integration-tests/resolve-node-module/__mocks__/mock-module-alt/package.json diff --git a/integration-tests/resolve-node-module/__mocks__/mock-module-alt/index.js b/integration-tests/resolve-node-module/__mocks__/mock-module-alt/index.js new file mode 100644 index 000000000000..acc83d1123dc --- /dev/null +++ b/integration-tests/resolve-node-module/__mocks__/mock-module-alt/index.js @@ -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 = 'test'; diff --git a/integration-tests/resolve-node-module/__mocks__/mock-module-alt/package.json b/integration-tests/resolve-node-module/__mocks__/mock-module-alt/package.json new file mode 100644 index 000000000000..0297f3861468 --- /dev/null +++ b/integration-tests/resolve-node-module/__mocks__/mock-module-alt/package.json @@ -0,0 +1,4 @@ +{ + "name": "mock-module", + "main": "./" +} diff --git a/integration-tests/resolve-node-module/__tests__/resolve-node-module.test.js b/integration-tests/resolve-node-module/__tests__/resolve-node-module.test.js index b008272f042a..fb900b6a8550 100644 --- a/integration-tests/resolve-node-module/__tests__/resolve-node-module.test.js +++ b/integration-tests/resolve-node-module/__tests__/resolve-node-module.test.js @@ -15,7 +15,12 @@ it('should resolve entry as index.js when package main is "."', () => { expect(mockModule).toEqual('test'); }); -it('should resolve entry as index with other configured module file extention when package main is "."', () => { +it('should resolve entry as index.js when package main is "./"', () => { + const mockModule = require('mock-module-alt'); + expect(mockModule).toEqual('test'); +}); + +it('should resolve entry as index with other configured module file extension when package main is "."', () => { const mockJsxModule = require('mock-jsx-module'); expect(mockJsxModule).toEqual('test jsx'); }); diff --git a/packages/jest-resolve/src/default_resolver.js b/packages/jest-resolve/src/default_resolver.js index beffc1a19ef5..72f5706d4aa8 100644 --- a/packages/jest-resolve/src/default_resolver.js +++ b/packages/jest-resolve/src/default_resolver.js @@ -129,7 +129,7 @@ function resolveSync(target: Path, options: ResolverOptions): Path { pkgmain = JSON.parse(body).main; } catch (e) {} - if (pkgmain && pkgmain !== '.') { + if (pkgmain && !isCurrentDirectory(pkgmain)) { const resolveTarget = path.resolve(name, pkgmain); const result = tryResolve(resolveTarget); if (result) { @@ -175,3 +175,7 @@ function isDirectory(dir: Path): boolean { return result; } + +function isCurrentDirectory(testPath: Path): boolean { + return path.resolve('.') === path.resolve(testPath); +}