From 0a459853fb33a0aa61ca9f0fc2af90118f0c429d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Wed, 23 May 2018 09:41:59 +0200 Subject: [PATCH 1/3] Properly resolve mocked node modules without package.json defined --- .../__mocks__/mock-module-without-pkg/index.js | 8 ++++++++ .../__tests__/resolve-node-module.test.js | 6 ++++++ packages/jest-haste-map/src/module_map.js | 2 +- 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 integration-tests/resolve-node-module/__mocks__/mock-module-without-pkg/index.js diff --git a/integration-tests/resolve-node-module/__mocks__/mock-module-without-pkg/index.js b/integration-tests/resolve-node-module/__mocks__/mock-module-without-pkg/index.js new file mode 100644 index 000000000000..7a7cf9456e36 --- /dev/null +++ b/integration-tests/resolve-node-module/__mocks__/mock-module-without-pkg/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 mock-module-without-pkg'; 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 index 33172ad2fad3..0862f3565a52 100644 --- a/integration-tests/resolve-node-module/__tests__/resolve-node-module.test.js +++ b/integration-tests/resolve-node-module/__tests__/resolve-node-module.test.js @@ -10,6 +10,7 @@ jest.mock('mock-module'); jest.mock('mock-module-alt'); jest.mock('mock-jsx-module'); +jest.mock('mock-module-without-pkg'); it('should resolve entry as index.js when package main is "."', () => { const mockModule = require('mock-module'); @@ -25,3 +26,8 @@ it('should resolve entry as index with other configured module file extension wh const mockJsxModule = require('mock-jsx-module'); expect(mockJsxModule).toEqual('test jsx'); }); + +it('should resolve entry as index without package.json', () => { + const mockModuleWithoutPkg = require('mock-module-without-pkg'); + expect(mockModuleWithoutPkg).toEqual('test mock-module-without-pkg'); +}); diff --git a/packages/jest-haste-map/src/module_map.js b/packages/jest-haste-map/src/module_map.js index 79ffb8c77b59..19cfedee7457 100644 --- a/packages/jest-haste-map/src/module_map.js +++ b/packages/jest-haste-map/src/module_map.js @@ -56,7 +56,7 @@ export default class ModuleMap { } getMockModule(name: string): ?Path { - return this._raw.mocks[name]; + return this._raw.mocks[name] || this._raw.mocks[name + '/index']; } getRawModuleMap(): RawModuleMap { From 5e1e23831d47366555972cdfacc75b779c2c6124 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Wed, 23 May 2018 10:11:42 +0200 Subject: [PATCH 2/3] update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 35c467ddde3a..d5bf716c8400 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -168,6 +168,8 @@ regardless of order ([#6150](https://github.com/facebook/jest/pull/6150)) * `[pretty-format]` [**BREAKING**] Remove undefined props from React elements ([#6162](https://github.com/facebook/jest/pull/6162)) +* `[jest-haste-map]` Properly resolve mocked node modules without package.json defined + ([#6232](https://github.com/facebook/jest/pull/6232)) ### Chore & Maintenance From 2c7490247efac03f78f8b464f2e5d6c70dbccc56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Wed, 23 May 2018 10:15:50 +0200 Subject: [PATCH 3/3] lint md --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d5bf716c8400..0de811914eab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -168,8 +168,8 @@ regardless of order ([#6150](https://github.com/facebook/jest/pull/6150)) * `[pretty-format]` [**BREAKING**] Remove undefined props from React elements ([#6162](https://github.com/facebook/jest/pull/6162)) -* `[jest-haste-map]` Properly resolve mocked node modules without package.json defined - ([#6232](https://github.com/facebook/jest/pull/6232)) +* `[jest-haste-map]` Properly resolve mocked node modules without package.json + defined ([#6232](https://github.com/facebook/jest/pull/6232)) ### Chore & Maintenance