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

Fixes automocks when using resolvers #6036

Merged
merged 1 commit into from
Apr 20, 2018
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
1 change: 1 addition & 0 deletions integration-tests/custom-resolver/__mocks__/manual-mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('bar');
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
2 changes: 1 addition & 1 deletion integration-tests/custom-resolver/bar.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = () => {};
module.exports = 'bar';
1 change: 1 addition & 0 deletions integration-tests/custom-resolver/manual-mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw new Error('Must be mocked');
22 changes: 0 additions & 22 deletions packages/jest-resolve/src/__tests__/resolve.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
16 changes: 0 additions & 16 deletions packages/jest-resolve/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down