Skip to content

Commit 5943dc9

Browse files
kenzablecpojer
authored andcommittedJan 19, 2018
Fix package main resolving issue (#5344)
* Fix package main resolving issue * Update changelog * Add test for pkgmain fix
1 parent fbcbf21 commit 5943dc9

File tree

9 files changed

+73
-1
lines changed

9 files changed

+73
-1
lines changed
 

‎CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
## master
22

3+
### Fixes
4+
5+
* `[jest-resolve]` Add condition to avoid infinite loop when node module package main is ".".
6+
([#5344)](https://github.com/facebook/jest/pull/5344)
7+
38
### Features
49
* `[jest-cli]` `--changedSince`: allow selectively running tests for code
510
changed since arbitrary revisions.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
* @flow
8+
*/
9+
'use strict';
10+
11+
const runJest = require('../runJest');
12+
13+
test('resolve node module', () => {
14+
const result = runJest('resolve-node-module');
15+
expect(result.status).toBe(0);
16+
});
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 jsx';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "mock-jsx-module",
3+
"main": "."
4+
}
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';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "mock-module",
3+
"main": "."
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
'use strict';
9+
10+
jest.mock('mock-module');
11+
jest.mock('mock-jsx-module');
12+
13+
it('should resolve entry as index.js when package main is "."', () => {
14+
const mockModule = require('mock-module');
15+
expect(mockModule).toEqual('test');
16+
});
17+
18+
it('should resolve entry as index with other configured module file extention when package main is "."', () => {
19+
const mockJsxModule = require('mock-jsx-module');
20+
expect(mockJsxModule).toEqual('test jsx');
21+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"jest": {
3+
"moduleFileExtensions": ["js", "json", "jsx"],
4+
"testEnvironment": "node"
5+
}
6+
}

‎packages/jest-resolve/src/default_resolver.js

+1-1
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) {
132+
if (pkgmain && pkgmain !== '.') {
133133
const resolveTarget = path.resolve(name, pkgmain);
134134
const result = tryResolve(resolveTarget);
135135
if (result) {

0 commit comments

Comments
 (0)
Please sign in to comment.