Skip to content

Commit

Permalink
Fix package main resolving issue (#5344)
Browse files Browse the repository at this point in the history
* Fix package main resolving issue

* Update changelog

* Add test for pkgmain fix
  • Loading branch information
kenzable authored and cpojer committed Jan 19, 2018
1 parent fbcbf21 commit 5943dc9
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## master

### Fixes

* `[jest-resolve]` Add condition to avoid infinite loop when node module package main is ".".
([#5344)](https://github.com/facebook/jest/pull/5344)

### Features
* `[jest-cli]` `--changedSince`: allow selectively running tests for code
changed since arbitrary revisions.
Expand Down
16 changes: 16 additions & 0 deletions integration-tests/__tests__/resolve-node-module.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* 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.
*
* @flow
*/
'use strict';

const runJest = require('../runJest');

test('resolve node module', () => {
const result = runJest('resolve-node-module');
expect(result.status).toBe(0);
});
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 jsx';
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "mock-jsx-module",
"main": "."
}
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
@@ -0,0 +1,21 @@
/**
* 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.
*/

'use strict';

jest.mock('mock-module');
jest.mock('mock-jsx-module');

it('should resolve entry as index.js when package main is "."', () => {
const mockModule = require('mock-module');
expect(mockModule).toEqual('test');
});

it('should resolve entry as index with other configured module file extention when package main is "."', () => {
const mockJsxModule = require('mock-jsx-module');
expect(mockJsxModule).toEqual('test jsx');
});
6 changes: 6 additions & 0 deletions integration-tests/resolve-node-module/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"jest": {
"moduleFileExtensions": ["js", "json", "jsx"],
"testEnvironment": "node"
}
}
2 changes: 1 addition & 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) {
if (pkgmain && pkgmain !== '.') {
const resolveTarget = path.resolve(name, pkgmain);
const result = tryResolve(resolveTarget);
if (result) {
Expand Down

0 comments on commit 5943dc9

Please sign in to comment.