From 143cfa33c0d7938409db174b0c0936e95aecc354 Mon Sep 17 00:00:00 2001 From: Mackenzie Turner Date: Thu, 18 Jan 2018 13:14:32 -0500 Subject: [PATCH 1/3] Fix package main resolving issue --- packages/jest-resolve/src/default_resolver.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-resolve/src/default_resolver.js b/packages/jest-resolve/src/default_resolver.js index 324777bec194..beffc1a19ef5 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) { + if (pkgmain && pkgmain !== '.') { const resolveTarget = path.resolve(name, pkgmain); const result = tryResolve(resolveTarget); if (result) { From ba1139168eb1edc3aef18b6f1bae544fa3cb90a9 Mon Sep 17 00:00:00 2001 From: Mackenzie Turner Date: Thu, 18 Jan 2018 13:34:41 -0500 Subject: [PATCH 2/3] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 44b83f2bd464..9affff0985ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Fixes +* `[jest-resolve]` Add condition to avoid infinite loop when node module package main is ".". + ([#5344)](https://github.com/facebook/jest/pull/5344) * `[jest-config]` fix unexpected condition to avoid infinite recursion in Windows platform. ([#5161](https://github.com/facebook/jest/pull/5161)) From f23c37937b57668a4af64f3fc0f16995be257285 Mon Sep 17 00:00:00 2001 From: Mackenzie Turner Date: Thu, 18 Jan 2018 17:29:37 -0500 Subject: [PATCH 3/3] Add test for pkgmain fix --- .../__tests__/resolve-node-module.test.js | 16 ++++++++++++++ .../__mocks__/mock-jsx-module/index.jsx | 8 +++++++ .../__mocks__/mock-jsx-module/package.json | 4 ++++ .../__mocks__/mock-module/index.js | 8 +++++++ .../__mocks__/mock-module/package.json | 4 ++++ .../__tests__/resolve-node-module.test.js | 21 +++++++++++++++++++ .../resolve-node-module/package.json | 6 ++++++ 7 files changed, 67 insertions(+) create mode 100644 integration-tests/__tests__/resolve-node-module.test.js create mode 100644 integration-tests/resolve-node-module/__mocks__/mock-jsx-module/index.jsx create mode 100644 integration-tests/resolve-node-module/__mocks__/mock-jsx-module/package.json create mode 100644 integration-tests/resolve-node-module/__mocks__/mock-module/index.js create mode 100644 integration-tests/resolve-node-module/__mocks__/mock-module/package.json create mode 100644 integration-tests/resolve-node-module/__tests__/resolve-node-module.test.js create mode 100644 integration-tests/resolve-node-module/package.json diff --git a/integration-tests/__tests__/resolve-node-module.test.js b/integration-tests/__tests__/resolve-node-module.test.js new file mode 100644 index 000000000000..bccde99d538a --- /dev/null +++ b/integration-tests/__tests__/resolve-node-module.test.js @@ -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); +}); diff --git a/integration-tests/resolve-node-module/__mocks__/mock-jsx-module/index.jsx b/integration-tests/resolve-node-module/__mocks__/mock-jsx-module/index.jsx new file mode 100644 index 000000000000..dfca40e809c1 --- /dev/null +++ b/integration-tests/resolve-node-module/__mocks__/mock-jsx-module/index.jsx @@ -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'; diff --git a/integration-tests/resolve-node-module/__mocks__/mock-jsx-module/package.json b/integration-tests/resolve-node-module/__mocks__/mock-jsx-module/package.json new file mode 100644 index 000000000000..14403b253601 --- /dev/null +++ b/integration-tests/resolve-node-module/__mocks__/mock-jsx-module/package.json @@ -0,0 +1,4 @@ +{ + "name": "mock-jsx-module", + "main": "." +} diff --git a/integration-tests/resolve-node-module/__mocks__/mock-module/index.js b/integration-tests/resolve-node-module/__mocks__/mock-module/index.js new file mode 100644 index 000000000000..acc83d1123dc --- /dev/null +++ b/integration-tests/resolve-node-module/__mocks__/mock-module/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/package.json b/integration-tests/resolve-node-module/__mocks__/mock-module/package.json new file mode 100644 index 000000000000..d9bf69878606 --- /dev/null +++ b/integration-tests/resolve-node-module/__mocks__/mock-module/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 new file mode 100644 index 000000000000..b008272f042a --- /dev/null +++ b/integration-tests/resolve-node-module/__tests__/resolve-node-module.test.js @@ -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'); +}); diff --git a/integration-tests/resolve-node-module/package.json b/integration-tests/resolve-node-module/package.json new file mode 100644 index 000000000000..0a586922137e --- /dev/null +++ b/integration-tests/resolve-node-module/package.json @@ -0,0 +1,6 @@ +{ + "jest": { + "moduleFileExtensions": ["js", "json", "jsx"], + "testEnvironment": "node" + } +}