Skip to content

Commit

Permalink
Don't throw an error if mock dependency can't be found (#10779)
Browse files Browse the repository at this point in the history
  • Loading branch information
brapifra committed Nov 4, 2020
1 parent a7ae4f2 commit ca92599
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
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
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
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

0 comments on commit ca92599

Please sign in to comment.