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__/getMockName-test.js b/packages/jest-haste-map/src/__tests__/getMockName-test.js new file mode 100644 index 000000000000..4556e9a17018 --- /dev/null +++ b/packages/jest-haste-map/src/__tests__/getMockName-test.js @@ -0,0 +1,24 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + */ +'use strict'; + +const path = require('path'); +const getMockName = require('../getMockName'); + +describe('getMockName', () => { + it('extracts mock name from file path', () => { + expect( + getMockName(path.join('a', '__mocks__', 'c.js')) + ).toBe('c'); + + expect( + getMockName(path.join('a', '__mocks__', 'c', 'd.js')) + ).toBe(path.join('c', 'd')); + }); +}); diff --git a/packages/jest-haste-map/src/__tests__/index-test.js b/packages/jest-haste-map/src/__tests__/index-test.js index 577596130859..e4079278d7e0 100644 --- a/packages/jest-haste-map/src/__tests__/index-test.js +++ b/packages/jest-haste-map/src/__tests__/index-test.js @@ -322,12 +322,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/getMockName.js b/packages/jest-haste-map/src/getMockName.js new file mode 100644 index 000000000000..8c83081da98d --- /dev/null +++ b/packages/jest-haste-map/src/getMockName.js @@ -0,0 +1,21 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @flow + */ +'use strict'; + +const path = require('path'); + +const mocksPattern = '__mocks__'; + +const getMockName = (filePath: string) => { + const mockPath = filePath.split(mocksPattern)[1]; + return mockPath.substring(1, mockPath.lastIndexOf(path.extname(mockPath))); +}; + +module.exports = getMockName; diff --git a/packages/jest-haste-map/src/index.js b/packages/jest-haste-map/src/index.js index c384e148b395..5ef784ae18c1 100644 --- a/packages/jest-haste-map/src/index.js +++ b/packages/jest-haste-map/src/index.js @@ -23,6 +23,7 @@ const {EventEmitter} = require('events'); const H = require('./constants'); const HasteFS = require('./HasteFS'); const HasteModuleMap = require('./ModuleMap'); +const getMockName = require('./getMockName'); const crypto = require('crypto'); const execSync = require('child_process').execSync; @@ -95,7 +96,6 @@ const canUseWatchman = ((): boolean => { const escapePathSeparator = string => (path.sep === '\\') ? string.replace(/(\/|\\)/g, '\\\\') : string; -const getMockName = filePath => path.basename(filePath, path.extname(filePath)); const getWhiteList = (list: ?Array): ?RegExp => { if (list && list.length) { return new RegExp( diff --git a/packages/jest-runtime/src/__tests__/Runtime-mock-test.js b/packages/jest-runtime/src/__tests__/Runtime-mock-test.js index 2aaf536871f1..9a160ef8fa6a 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, 'ManuallyMocked'), ).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;