Skip to content

Commit 0caffd3

Browse files
committed
Generalise package main resolving issue
Resolves jestjs#5967
1 parent 45c1746 commit 0caffd3

4 files changed

Lines changed: 23 additions & 2 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
module.exports = 'test';
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "mock-module",
3+
"main": "./"
4+
}

integration-tests/resolve-node-module/__tests__/resolve-node-module.test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ it('should resolve entry as index.js when package main is "."', () => {
1515
expect(mockModule).toEqual('test');
1616
});
1717

18-
it('should resolve entry as index with other configured module file extention when package main is "."', () => {
18+
it('should resolve entry as index.js when package main is "./"', () => {
19+
const mockModule = require('mock-module-alt');
20+
expect(mockModule).toEqual('test');
21+
});
22+
23+
it('should resolve entry as index with other configured module file extension when package main is "."', () => {
1924
const mockJsxModule = require('mock-jsx-module');
2025
expect(mockJsxModule).toEqual('test jsx');
2126
});

packages/jest-resolve/src/default_resolver.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ function resolveSync(target: Path, options: ResolverOptions): Path {
129129
pkgmain = JSON.parse(body).main;
130130
} catch (e) {}
131131

132-
if (pkgmain && pkgmain !== '.') {
132+
if (pkgmain && !isCurrentDirectory(pkgmain)) {
133133
const resolveTarget = path.resolve(name, pkgmain);
134134
const result = tryResolve(resolveTarget);
135135
if (result) {
@@ -175,3 +175,7 @@ function isDirectory(dir: Path): boolean {
175175

176176
return result;
177177
}
178+
179+
function isCurrentDirectory(testPath: Path): boolean {
180+
return path.resolve('.') === path.resolve(testPath);
181+
}

0 commit comments

Comments
 (0)