From e9e999fc24db6d6e74f234441da7611aa2eaa6f4 Mon Sep 17 00:00:00 2001 From: Min Idzelis Date: Mon, 18 Sep 2017 10:29:22 -0400 Subject: [PATCH] Manual user mocks not working with custom resolver (#4427) --- .../src/__tests__/defaultResolver.js | 10 +++++ .../__tests__/runtime_require_mock.test.js | 14 ++++++ packages/jest-runtime/src/index.js | 45 +++++++++---------- 3 files changed, 46 insertions(+), 23 deletions(-) create mode 100644 packages/jest-runtime/src/__tests__/defaultResolver.js diff --git a/packages/jest-runtime/src/__tests__/defaultResolver.js b/packages/jest-runtime/src/__tests__/defaultResolver.js new file mode 100644 index 000000000000..089aa58e6055 --- /dev/null +++ b/packages/jest-runtime/src/__tests__/defaultResolver.js @@ -0,0 +1,10 @@ +/** + * Copyright (c) 2017-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. + * + */ +import resolver from 'jest-resolve/build/default_resolver.js'; +module.exports = resolver; diff --git a/packages/jest-runtime/src/__tests__/runtime_require_mock.test.js b/packages/jest-runtime/src/__tests__/runtime_require_mock.test.js index 9ea0fcb9d049..e8bf6614caed 100644 --- a/packages/jest-runtime/src/__tests__/runtime_require_mock.test.js +++ b/packages/jest-runtime/src/__tests__/runtime_require_mock.test.js @@ -160,5 +160,19 @@ describe('Runtime', () => { expect(exports2.modulePath).toEqual('subdir2/__mocks__/my_module.js'); }); }); + + it('uses manual mocks when using a custom resolver', () => { + return createRuntime(__filename, { + // using the default resolver as a custom resolver + resolver: require.resolve('./defaultResolver.js'), + }).then(runtime => { + const exports = runtime.requireMock( + runtime.__mockRootPath, + './ManuallyMocked', + ); + + expect(exports.isManualMockModule).toBe(true); + }); + }); }); }); diff --git a/packages/jest-runtime/src/index.js b/packages/jest-runtime/src/index.js index de418865011a..b9c1b637cde4 100644 --- a/packages/jest-runtime/src/index.js +++ b/packages/jest-runtime/src/index.js @@ -361,29 +361,28 @@ class Runtime { modulePath = this._resolveModule(from, manualMock); } else { modulePath = this._resolveModule(from, moduleName); - - // If the actual module file has a __mocks__ dir sitting immediately next - // to it, look to see if there is a manual mock for this file. - // - // subDir1/my_module.js - // subDir1/__mocks__/my_module.js - // subDir2/my_module.js - // subDir2/__mocks__/my_module.js - // - // Where some other module does a relative require into each of the - // respective subDir{1,2} directories and expects a manual mock - // corresponding to that particular my_module.js file. - const moduleDir = path.dirname(modulePath); - const moduleFileName = path.basename(modulePath); - const potentialManualMock = path.join( - moduleDir, - '__mocks__', - moduleFileName, - ); - if (fs.existsSync(potentialManualMock)) { - manualMock = true; - modulePath = potentialManualMock; - } + } + // If the actual module file has a __mocks__ dir sitting immediately next + // to it, look to see if there is a manual mock for this file. + // + // subDir1/my_module.js + // subDir1/__mocks__/my_module.js + // subDir2/my_module.js + // subDir2/__mocks__/my_module.js + // + // Where some other module does a relative require into each of the + // respective subDir{1,2} directories and expects a manual mock + // corresponding to that particular my_module.js file. + const moduleDir = path.dirname(modulePath); + const moduleFileName = path.basename(modulePath); + const potentialManualMock = path.join( + moduleDir, + '__mocks__', + moduleFileName, + ); + if (fs.existsSync(potentialManualMock)) { + manualMock = true; + modulePath = potentialManualMock; } if (manualMock) {