Skip to content

Commit b01ab18

Browse files
cpojerFacebook Github Bot 6
authored andcommitted
Fix moduleNameMapper not being able to use relative paths.
Reviewed By: dmitriiabramov Differential Revision: D3111621 fb-gh-sync-id: 0bc993171d5008a63056b5ba04e2080aa53f23c0 fbshipit-source-id: 0bc993171d5008a63056b5ba04e2080aa53f23c0
1 parent 7e57567 commit b01ab18

File tree

4 files changed

+92
-6
lines changed

4 files changed

+92
-6
lines changed

src/HasteModuleLoader/HasteModuleLoader.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -468,10 +468,7 @@ class Loader {
468468
absolutePath = moduleName;
469469
} else {
470470
moduleType = 'user';
471-
if (
472-
constants.IS_PATH_BASED_MODULE_NAME.test(moduleName) ||
473-
(!this._getModule(moduleName) && !this._getMockModule(moduleName))
474-
) {
471+
if (!this._getModule(moduleName) && !this._getMockModule(moduleName)) {
475472
absolutePath = this._resolveModuleName(currPath, moduleName);
476473
// Look up if this module has an associated manual mock.
477474
const mockModule = this._getMockModule(moduleName);
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the BSD-style license found in the
5+
* LICENSE file in the root directory of this source tree. An additional grant
6+
* of patent rights can be found in the PATENTS file in the same directory.
7+
*
8+
* @emails oncall+jsinfra
9+
*/
10+
'use strict';
11+
12+
jest.disableAutomock();
13+
jest.mock('../../environments/JSDOMEnvironment');
14+
15+
const path = require('path');
16+
const normalizeConfig = require('../../config/normalize');
17+
18+
describe('HasteModuleLoader', () => {
19+
let HasteModuleLoader;
20+
let HasteResolver;
21+
let JSDOMEnvironment;
22+
23+
const rootDir = path.join(__dirname, 'test_root');
24+
const rootPath = path.join(rootDir, 'root.js');
25+
const config = normalizeConfig({
26+
cacheDirectory: global.CACHE_DIRECTORY,
27+
name: 'HasteModuleLoader-mock-tests',
28+
rootDir,
29+
});
30+
31+
function buildLoader() {
32+
const environment = new JSDOMEnvironment(config);
33+
const resolver = new HasteResolver(config, {resetCache: false});
34+
return resolver.getHasteMap().then(
35+
response => resolver.end().then(() =>
36+
new HasteModuleLoader(config, environment, response)
37+
)
38+
);
39+
}
40+
41+
beforeEach(() => {
42+
HasteModuleLoader = require('../HasteModuleLoader');
43+
HasteResolver = require('../../resolvers/HasteResolver');
44+
JSDOMEnvironment = require('../../environments/JSDOMEnvironment');
45+
});
46+
47+
describe('jest.mock', () => {
48+
pit('uses uses explicitly set mocks instead of automocking', () => {
49+
return buildLoader().then(loader => {
50+
const mockReference = {isMock: true};
51+
const root = loader.requireModule(rootPath, './root.js');
52+
// Erase module registry because root.js requires most other modules.
53+
root.jest.resetModuleRegistry();
54+
55+
root.jest.mock('RegularModule', () => mockReference);
56+
root.jest.mock('ManuallyMocked', () => mockReference);
57+
58+
expect(
59+
loader.requireModuleOrMock(rootPath, 'RegularModule')
60+
).toEqual(mockReference);
61+
62+
expect(
63+
loader.requireModuleOrMock(rootPath, 'RegularModule')
64+
).toEqual(mockReference);
65+
});
66+
});
67+
});
68+
69+
describe('jest.setMock', () => {
70+
pit('uses uses explicitly set mocks instead of automocking', () => {
71+
return buildLoader().then(loader => {
72+
const mockReference = {isMock: true};
73+
const root = loader.requireModule(rootPath, './root.js');
74+
// Erase module registry because root.js requires most other modules.
75+
root.jest.resetModuleRegistry();
76+
77+
root.jest.setMock('RegularModule', mockReference);
78+
root.jest.setMock('ManuallyMocked', mockReference);
79+
80+
expect(
81+
loader.requireModuleOrMock(rootPath, 'RegularModule')
82+
).toBe(mockReference);
83+
84+
expect(
85+
loader.requireModuleOrMock(rootPath, 'RegularModule')
86+
).toBe(mockReference);
87+
});
88+
});
89+
});
90+
});

src/HasteModuleLoader/__tests__/HasteModuleLoader-requireModuleOrMock-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ describe('HasteModuleLoader', function() {
121121
exports = loader.requireModuleOrMock(rootPath, 'cat.png');
122122
expect(exports.isRelativeImageStub).toBe(true);
123123

124-
exports = loader.requireModuleOrMock(rootPath, 'dog.png');
124+
exports = loader.requireModuleOrMock(rootPath, '../photos/dog.png');
125125
expect(exports.isRelativeImageStub).toBe(true);
126126

127127
exports = loader.requireModuleOrMock(rootPath, 'module/name/test');

src/constants.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,3 @@ const pkg = require('../package.json');
1616
exports.VERSION = pkg.version;
1717
exports.MAX_WORKERS = os.cpus().length;
1818
exports.NODE_MODULES = path.sep + 'node_modules' + path.sep;
19-
exports.IS_PATH_BASED_MODULE_NAME = /^(?:\.\.?\/|\/)/;

0 commit comments

Comments
 (0)