diff --git a/packages/jest-haste-map/src/__tests__/__snapshots__/index-test.js.snap b/packages/jest-haste-map/src/__tests__/__snapshots__/index-test.js.snap index 3e4a303b12c0..12bf932648b1 100644 --- a/packages/jest-haste-map/src/__tests__/__snapshots__/index-test.js.snap +++ b/packages/jest-haste-map/src/__tests__/__snapshots__/index-test.js.snap @@ -14,14 +14,14 @@ exports[`HasteMap tries to crawl using node as a fallback 1`] = ` exports[`HasteMap warns on duplicate mock files 1`] = ` "jest-haste-map: duplicate manual mock found: - Module name: blueberry - Duplicate Mock path: /fruits/__mocks__/subdir2/blueberry.js + Module name: subdir/blueberry + Duplicate Mock path: /fruits2/__mocks__/subdir/blueberry.js This warning is caused by two manual mock files with the same file name. Jest will use the mock file found in: -/fruits/__mocks__/subdir2/blueberry.js +/fruits2/__mocks__/subdir/blueberry.js Please delete one of the following two files: - /fruits/__mocks__/subdir1/blueberry.js -/fruits/__mocks__/subdir2/blueberry.js + /fruits1/__mocks__/subdir/blueberry.js +/fruits2/__mocks__/subdir/blueberry.js " `; diff --git a/packages/jest-haste-map/src/__tests__/index-test.js b/packages/jest-haste-map/src/__tests__/index-test.js index 577596130859..1588890304ed 100644 --- a/packages/jest-haste-map/src/__tests__/index-test.js +++ b/packages/jest-haste-map/src/__tests__/index-test.js @@ -80,6 +80,18 @@ let readFileSync; let workerFarmMock; let writeFileSync; +describe('getMockName', () => { + it('extracts mock name from file path', () => { + HasteMap = require('../'); + const {getMockName} = HasteMap; + + expect(getMockName('a/__b__/c.js', /__b__/)).toBe('c'); + expect(getMockName('a/__b__/index.js', /__b__/)).toBe('index'); + expect(getMockName('a/__b__/c/d.js', /__b__/)).toBe('c/d'); + expect(getMockName('a/__b__/c/d/index.js', /__b__/)).toBe('c/d'); + }); +}); + describe('HasteMap', () => { skipOnWindows.suite(); @@ -322,12 +334,12 @@ describe('HasteMap', () => { it('warns on duplicate mock files', () => { // Duplicate mock files for blueberry - mockFs['/fruits/__mocks__/subdir1/blueberry.js'] = [ + mockFs['/fruits1/__mocks__/subdir/blueberry.js'] = [ '/**', ' * @providesModule Blueberry1', ' */', ].join('\n'); - mockFs['/fruits/__mocks__/subdir2/blueberry.js'] = [ + mockFs['/fruits2/__mocks__/subdir/blueberry.js'] = [ '/**', ' * @providesModule Blueberry2', ' */', diff --git a/packages/jest-haste-map/src/index.js b/packages/jest-haste-map/src/index.js index dd0d8747f642..3e8033f83b42 100644 --- a/packages/jest-haste-map/src/index.js +++ b/packages/jest-haste-map/src/index.js @@ -95,7 +95,14 @@ const canUseWatchman = ((): boolean => { const escapePathSeparator = string => (path.sep === '\\') ? string.replace(/(\/|\\)/g, '\\\\') : string; -const getMockName = filePath => path.basename(filePath, path.extname(filePath)); +const getMockName = (filePath, mocksPattern) => { + const modulePath = filePath.split(mocksPattern)[1]; + const extractMockName = new RegExp( + `^\\${path.sep}?(.*?)(\\${path.sep}index)?\\${path.extname(modulePath)}$` + ); + return extractMockName.exec(modulePath)[1]; +}; + const getWhiteList = (list: ?Array): ?RegExp => { if (list && list.length) { return new RegExp( @@ -327,7 +334,7 @@ class HasteMap extends EventEmitter { this._options.mocksPattern && this._options.mocksPattern.test(filePath) ) { - const mockPath = getMockName(filePath); + const mockPath = getMockName(filePath, this._options.mocksPattern); if (mocks[mockPath]) { this._console.warn( `jest-haste-map: duplicate manual mock found:\n` + @@ -617,7 +624,7 @@ class HasteMap extends EventEmitter { this._options.mocksPattern && this._options.mocksPattern.test(filePath) ) { - const mockName = getMockName(filePath); + const mockName = getMockName(filePath, this._options.mocksPattern); delete hasteMap.mocks[mockName]; } @@ -701,8 +708,11 @@ class HasteMap extends EventEmitter { } static H: HType; + + static getMockName; } HasteMap.H = H; +HasteMap.getMockName = getMockName; module.exports = HasteMap; diff --git a/packages/jest-runtime/src/__tests__/Runtime-mock-test.js b/packages/jest-runtime/src/__tests__/Runtime-mock-test.js index ffd54a819252..d20410399c4a 100644 --- a/packages/jest-runtime/src/__tests__/Runtime-mock-test.js +++ b/packages/jest-runtime/src/__tests__/Runtime-mock-test.js @@ -9,6 +9,8 @@ */ 'use strict'; +const path = require('path'); + let createRuntime; describe('Runtime', () => { @@ -27,6 +29,7 @@ describe('Runtime', () => { root.jest.mock('RegularModule', () => mockReference); root.jest.mock('ManuallyMocked', () => mockReference); + root.jest.mock(path.join('nested1', 'nested2', 'nested3')); expect( runtime.requireModuleOrMock(runtime.__mockRootPath, 'RegularModule'), @@ -35,6 +38,12 @@ describe('Runtime', () => { expect( runtime.requireModuleOrMock(runtime.__mockRootPath, 'RegularModule'), ).toEqual(mockReference); + + expect( + runtime.requireModuleOrMock( + runtime.__mockRootPath, path.join('nested1', 'nested2', 'nested3') + ), + ).toEqual(mockReference); }), ); diff --git a/packages/jest-runtime/src/__tests__/test_root/__mocks__/nested1/nested2/nested3.js b/packages/jest-runtime/src/__tests__/test_root/__mocks__/nested1/nested2/nested3.js new file mode 100644 index 000000000000..2e6922e64cd6 --- /dev/null +++ b/packages/jest-runtime/src/__tests__/test_root/__mocks__/nested1/nested2/nested3.js @@ -0,0 +1,3 @@ +'use strict'; + +exports.isMock = true; diff --git a/packages/jest-runtime/src/__tests__/test_root/nested1/nested2/nested3.js b/packages/jest-runtime/src/__tests__/test_root/nested1/nested2/nested3.js new file mode 100644 index 000000000000..3dec268a2c34 --- /dev/null +++ b/packages/jest-runtime/src/__tests__/test_root/nested1/nested2/nested3.js @@ -0,0 +1,3 @@ +'use strict'; + +exports.isMock = false;