From 083f4f12c8378df03070f0ea4fabf1c072494e5e Mon Sep 17 00:00:00 2001 From: Caleb Ukle Date: Thu, 6 Apr 2023 14:10:47 -0500 Subject: [PATCH] fix(testing): correctly error with jest.mock for invalid filepaths (#15445) --- packages/jest/plugins/resolver.ts | 10 +++++++--- scripts/patched-jest-resolver.js | 8 ++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/packages/jest/plugins/resolver.ts b/packages/jest/plugins/resolver.ts index 66abd9d7ae1cf..25849f52db33f 100644 --- a/packages/jest/plugins/resolver.ts +++ b/packages/jest/plugins/resolver.ts @@ -1,4 +1,4 @@ -import { dirname, extname } from 'path'; +import { dirname, extname, join } from 'path'; import { resolve as resolveExports } from 'resolve.exports'; import type defaultResolver from 'jest-resolve/build/defaultResolver'; @@ -81,7 +81,11 @@ module.exports = function (path: string, options: ResolveOptions) { ts = ts || require('typescript'); compilerSetup = compilerSetup || getCompilerSetup(options.rootDir); const { compilerOptions, host } = compilerSetup; - return ts.resolveModuleName(path, options.basedir, compilerOptions, host) - .resolvedModule.resolvedFileName; + return ts.resolveModuleName( + path, + join(options.basedir, 'fake-placeholder.ts'), + compilerOptions, + host + ).resolvedModule.resolvedFileName; } }; diff --git a/scripts/patched-jest-resolver.js b/scripts/patched-jest-resolver.js index c365ac167e144..6a0c038f0f708 100644 --- a/scripts/patched-jest-resolver.js +++ b/scripts/patched-jest-resolver.js @@ -91,7 +91,11 @@ module.exports = function (path, options) { } else if (path === '@nrwl/devkit/testing') { return join(__dirname, '../', './packages/devkit/testing.js'); } - return ts.resolveModuleName(path, options.basedir, compilerOptions, host) - .resolvedModule.resolvedFileName; + return ts.resolveModuleName( + path, + join(options.basedir, 'fake-placeholder.ts'), + compilerOptions, + host + ).resolvedModule.resolvedFileName; } };