Skip to content

Commit

Permalink
Enable manual mocks with nested folders (jestjs#2483)
Browse files Browse the repository at this point in the history
* Enable manual mocks with nested folders

* extract getMockName into a separate module

* avoid using regex

* add file headers
  • Loading branch information
MicheleBertoli authored and cpojer committed Jan 15, 2017
1 parent 3b39ae6 commit bddc4d4
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 8 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
24 changes: 24 additions & 0 deletions packages/jest-haste-map/src/__tests__/getMockName-test.js
Original file line number Diff line number Diff line change
@@ -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'));
});
});
4 changes: 2 additions & 2 deletions packages/jest-haste-map/src/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
' */',
Expand Down
21 changes: 21 additions & 0 deletions packages/jest-haste-map/src/getMockName.js
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion packages/jest-haste-map/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<string>): ?RegExp => {
if (list && list.length) {
return new RegExp(
Expand Down
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, 'ManuallyMocked'),
).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 bddc4d4

Please sign in to comment.