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

Manual user mocks not working with custom resolver #4427 #4489

Merged
merged 1 commit into from
Oct 4, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions packages/jest-runtime/src/__tests__/defaultResolver.js
Original file line number Diff line number Diff line change
@@ -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;
14 changes: 14 additions & 0 deletions packages/jest-runtime/src/__tests__/runtime_require_mock.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
});
45 changes: 22 additions & 23 deletions packages/jest-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down