diff --git a/packages/jest/src/plugins/plugin.ts b/packages/jest/src/plugins/plugin.ts index fdcb33388b..4311cd2ae2 100644 --- a/packages/jest/src/plugins/plugin.ts +++ b/packages/jest/src/plugins/plugin.ts @@ -436,6 +436,7 @@ function resolveJestPath(projectRoot: string, workspaceRoot: string): string { return resolvedJestPaths[projectRoot]; } +let resolvedJestCorePaths: Record; /** * Resolves a jest util package version that `jest` is using. */ @@ -446,5 +447,15 @@ function requireJestUtil( ): T { const jestPath = resolveJestPath(projectRoot, workspaceRoot); - return require(require.resolve(packageName, { paths: [dirname(jestPath)] })); + resolvedJestCorePaths ??= {}; + if (!resolvedJestCorePaths[jestPath]) { + // nx-ignore-next-line + resolvedJestCorePaths[jestPath] = require.resolve('@jest/core', { + paths: [dirname(jestPath)], + }); + } + + return require(require.resolve(packageName, { + paths: [dirname(resolvedJestCorePaths[jestPath])], + })); }