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

Don't throw an error if mock dependency can't be found #10779

Merged
merged 2 commits into from
Nov 4, 2020
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Fixes

- `[jest-resolve-dependencies]` Continue dependency resolution if mock dependency can't be found ([#10779](https://github.com/facebook/jest/pull/10779))

### Chore & Maintenance

### Performance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,17 @@ test('resolves dependencies correctly when dependency resolution fails', () => {

expect(resolved).toEqual([]);
});

test('resolves dependencies correctly when mock dependency resolution fails', () => {
jest.spyOn(runtimeContextResolver, 'getMockModule').mockImplementation(() => {
throw new Error('getMockModule has failed');
});

const resolved = dependencyResolver.resolve(
path.resolve(__dirname, '__fixtures__', 'file.test.js'),
);

expect(resolved).toEqual([
expect.stringContaining(path.join('__tests__', '__fixtures__', 'file.js')),
]);
});
12 changes: 8 additions & 4 deletions packages/jest-resolve-dependencies/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,14 @@ class DependencyResolver {

// If we resolve a dependency, then look for a mock dependency
// of the same name in that dependency's directory.
resolvedMockDependency = this._resolver.getMockModule(
resolvedDependency,
path.basename(dependency),
);
try {
resolvedMockDependency = this._resolver.getMockModule(
resolvedDependency,
path.basename(dependency),
);
} catch {
// leave resolvedMockDependency as undefined if nothing can be found
}

if (resolvedMockDependency) {
const dependencyMockDir = path.resolve(
Expand Down