From ca92599d4e139f7674fb33ba4c99592d3375e3fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Brais=20Pi=C3=B1eiro?= Date: Wed, 4 Nov 2020 06:49:02 +0100 Subject: [PATCH] Don't throw an error if mock dependency can't be found (#10779) --- CHANGELOG.md | 2 ++ .../src/__tests__/dependency_resolver.test.ts | 14 ++++++++++++++ packages/jest-resolve-dependencies/src/index.ts | 12 ++++++++---- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a42439c24887..fc914b8cdca0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/packages/jest-resolve-dependencies/src/__tests__/dependency_resolver.test.ts b/packages/jest-resolve-dependencies/src/__tests__/dependency_resolver.test.ts index 27323aaf024e..0eff9cf84064 100644 --- a/packages/jest-resolve-dependencies/src/__tests__/dependency_resolver.test.ts +++ b/packages/jest-resolve-dependencies/src/__tests__/dependency_resolver.test.ts @@ -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')), + ]); +}); diff --git a/packages/jest-resolve-dependencies/src/index.ts b/packages/jest-resolve-dependencies/src/index.ts index ed6212e7bf14..19228328d7a7 100644 --- a/packages/jest-resolve-dependencies/src/index.ts +++ b/packages/jest-resolve-dependencies/src/index.ts @@ -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(