Skip to content

Commit

Permalink
Generalise package main resolving issue
Browse files Browse the repository at this point in the history
Resolves jestjs#5967
  • Loading branch information
mtlewis committed Apr 11, 2018
1 parent 45c1746 commit 0caffd3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
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 = 'test';
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "mock-module",
"main": "./"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
6 changes: 5 additions & 1 deletion packages/jest-resolve/src/default_resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -175,3 +175,7 @@ function isDirectory(dir: Path): boolean {

return result;
}

function isCurrentDirectory(testPath: Path): boolean {
return path.resolve('.') === path.resolve(testPath);
}

0 comments on commit 0caffd3

Please sign in to comment.