diff --git a/integration-tests/custom-resolver/__mocks__/manual-mock.js b/integration-tests/custom-resolver/__mocks__/manual-mock.js new file mode 100644 index 000000000000..f0488585d2e7 --- /dev/null +++ b/integration-tests/custom-resolver/__mocks__/manual-mock.js @@ -0,0 +1 @@ +module.exports = require('bar'); diff --git a/integration-tests/custom-resolver/__tests__/custom-resolver.test.js b/integration-tests/custom-resolver/__tests__/custom-resolver.test.js index c938ff090d38..522fed7d0d8d 100644 --- a/integration-tests/custom-resolver/__tests__/custom-resolver.test.js +++ b/integration-tests/custom-resolver/__tests__/custom-resolver.test.js @@ -13,3 +13,18 @@ test('should use the custom resolver', () => { test('should have regenerator injected', () => { expect(global.fakeRegeneratorInjected).toEqual(true); }); + +test('should work with automock', () => { + jest.mock('foo'); + + const foo = require('foo'); + foo(); + + expect(foo).toHaveBeenCalled(); +}); + +test('should allow manual mocks to make require calls through the resolver', () => { + jest.mock('../manual-mock'); + + expect(require('../manual-mock')).toEqual('bar'); +}); diff --git a/integration-tests/custom-resolver/bar.js b/integration-tests/custom-resolver/bar.js index cc40a4649c9f..cb1c2c01e753 100644 --- a/integration-tests/custom-resolver/bar.js +++ b/integration-tests/custom-resolver/bar.js @@ -1 +1 @@ -module.exports = () => {}; +module.exports = 'bar'; diff --git a/integration-tests/custom-resolver/manual-mock.js b/integration-tests/custom-resolver/manual-mock.js new file mode 100644 index 000000000000..7c84c09f6033 --- /dev/null +++ b/integration-tests/custom-resolver/manual-mock.js @@ -0,0 +1 @@ +throw new Error('Must be mocked'); diff --git a/packages/jest-resolve/src/__tests__/resolve.test.js b/packages/jest-resolve/src/__tests__/resolve.test.js index 91986a73ac5f..f971235e3a9a 100644 --- a/packages/jest-resolve/src/__tests__/resolve.test.js +++ b/packages/jest-resolve/src/__tests__/resolve.test.js @@ -172,28 +172,6 @@ describe('getMockModule', () => { path.dirname(src), ); }); - - it('is possible to use custom resolver to resolve deps inside mock modules without moduleNameMapper', () => { - userResolver.mockImplementation(() => 'module'); - - const moduleMap = new ModuleMap({ - duplicates: [], - map: [], - mocks: [], - }); - const resolver = new Resolver(moduleMap, { - resolver: require.resolve('../__mocks__/userResolver'), - }); - const src = require.resolve('../'); - resolver.getMockModule(src, 'dependentModule'); - - expect(userResolver).toHaveBeenCalled(); - expect(userResolver.mock.calls[0][0]).toBe('dependentModule'); - expect(userResolver.mock.calls[0][1]).toHaveProperty( - 'basedir', - path.dirname(src), - ); - }); }); describe('nodeModulesPaths', () => { diff --git a/packages/jest-resolve/src/index.js b/packages/jest-resolve/src/index.js index 8687878d83e5..94d32cf85a75 100644 --- a/packages/jest-resolve/src/index.js +++ b/packages/jest-resolve/src/index.js @@ -374,22 +374,6 @@ Please check: } } } - if (resolver) { - // if moduleNameMapper didn't match anything, fallback to just the - // regular resolver - const module = - this.getModule(moduleName) || - Resolver.findNodeModule(moduleName, { - basedir: dirname, - browser: this._options.browser, - extensions, - moduleDirectory, - paths, - resolver, - rootDir: this._options.rootDir, - }); - return module; - } return null; }