Skip to content

Commit

Permalink
Enable manual mocks with nested folders
Browse files Browse the repository at this point in the history
  • Loading branch information
MicheleBertoli committed Dec 31, 2016
1 parent b9cb0ac commit cca8cc8
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
"
`;
Expand Down
16 changes: 14 additions & 2 deletions packages/jest-haste-map/src/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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',
' */',
Expand Down
16 changes: 13 additions & 3 deletions packages/jest-haste-map/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>): ?RegExp => {
if (list && list.length) {
return new RegExp(
Expand Down Expand Up @@ -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` +
Expand Down Expand Up @@ -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];
}

Expand Down Expand Up @@ -701,8 +708,11 @@ class HasteMap extends EventEmitter {
}

static H: HType;

static getMockName;
}

HasteMap.H = H;
HasteMap.getMockName = getMockName;

module.exports = HasteMap;
9 changes: 9 additions & 0 deletions packages/jest-runtime/src/__tests__/Runtime-mock-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/
'use strict';

const path = require('path');

let createRuntime;

describe('Runtime', () => {
Expand All @@ -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'),
Expand All @@ -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);
}),
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.isMock = true;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.isMock = false;

0 comments on commit cca8cc8

Please sign in to comment.