Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable manual mocks with nested folders #2483

Merged
merged 4 commits into from
Jan 15, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
22 changes: 22 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,22 @@
const path = require('path');
const getMockName = require('../getMockName');

describe('getMockName', () => {
it('extracts mock name from file path', () => {
expect(
getMockName(path.join('a', '__b__', 'c.js'), /__b__/)
).toBe('c');

expect(
getMockName(path.join('a', '__b__', 'index.js'), /__b__/)
).toBe('index');

expect(
getMockName(path.join('a', '__b__', 'c', 'd.js'), /__b__/)
).toBe(path.join('c', 'd'));

expect(
getMockName(path.join('a', '__b__', 'c', 'd', 'index.js'), /__b__/)
).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
13 changes: 13 additions & 0 deletions packages/jest-haste-map/src/getMockName.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const path = require('path');

const getMockName = (filePath, mocksPattern) => {
const modulePath = filePath.split(mocksPattern)[1];

const extractMockName = new RegExp(
`^\\${path.sep}?(.*?)(\\${path.sep}index)?\\${path.extname(modulePath)}$`
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm really worried about performance here. Right now, mocksPattern is a config option but it isn't really configurable and I'm ok making it non-configurable just like the snapshots folder because it causes trouble.

Can you change this either by:

  • Optimize this code for performance. Think running this on 50k files, it should be less than 100ms or at least not slower than what we do here currently.
  • Change the mocksPattern option to be hard-coded, so splitting at __mocks__ and looking at the right-hand side of this should work and be fast.


return extractMockName.exec(modulePath)[1];
};

module.exports = getMockName;
6 changes: 3 additions & 3 deletions 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 Expand Up @@ -327,7 +327,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 +617,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
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;