Skip to content

Commit

Permalink
fix(testing): resolve jest package from the project root in plugin (#…
Browse files Browse the repository at this point in the history
…27342)

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
  • Loading branch information
leosvelperez authored Aug 8, 2024
1 parent 712dbbc commit 46dcee6
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions packages/jest/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,20 +211,22 @@ async function buildJestTargets(

let metadata: ProjectConfiguration['metadata'];
if (options?.ciTargetName) {
// Resolve the version of `jest-runtime` that `jest` is using.
const jestPath = require.resolve('jest');
const jest = require(jestPath) as typeof import('jest');
// nx-ignore-next-line
const { default: Runtime } = require(require.resolve('jest-runtime', {
paths: [dirname(jestPath)],
// nx-ignore-next-line
})) as typeof import('jest-runtime');
const { default: Runtime } = requireJestUtil<typeof import('jest-runtime')>(
'jest-runtime',
projectRoot,
context
);

const jestContext = await Runtime.createContext(config.projectConfig, {
maxWorkers: 1,
watchman: false,
});

const jest = require(resolveJestPath(
projectRoot,
context
)) as typeof import('jest');
const source = new jest.SearchSource(jestContext);

const specs = await source.getTestPaths(config.globalConfig);
Expand Down Expand Up @@ -403,3 +405,31 @@ function normalizeOptions(options: JestPluginOptions): JestPluginOptions {
options.targetName ??= 'test';
return options;
}

let resolvedJestPaths: Record<string, string>;
function resolveJestPath(
projectRoot: string,
context: CreateNodesContext
): string {
resolvedJestPaths ??= {};
if (resolvedJestPaths[projectRoot]) {
return resolvedJestPaths[projectRoot];
}

return require.resolve('jest', {
paths: [projectRoot, context.workspaceRoot, __dirname],
});
}

/**
* Resolves a jest util package version that `jest` is using.
*/
function requireJestUtil<T>(
packageName: string,
projectRoot: string,
context: CreateNodesContext
): T {
const jestPath = resolveJestPath(projectRoot, context);

return require(require.resolve(packageName, { paths: [dirname(jestPath)] }));
}

0 comments on commit 46dcee6

Please sign in to comment.