Skip to content

Commit cbda8cf

Browse files
authored
fix(testing): getJestProjectsAsync no longer duplicates project paths (#28311)
<!-- 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 Using `getJestProjectsAsync` to include both `project.json` and inferred targets can result in errors due to duplcicated paths. You see an error like this: ``` Error: Whoops! Two projects resolved to the same config path: /Users/jack/projects/ocean/libs/nx-cloud/data-access-organization-dashboard/jest.config.ts: Project 1: /Users/jack/projects/ocean/libs/nx-cloud/data-access-organization-dashboard/jest.config.ts Project 2: /Users/jack/projects/ocean/libs/nx-cloud/data-access-organization-dashboard This usually means that your "projects" config includes a directory that doesn't have any configuration recognizable by Jest. Please fix it. at ensureNoDuplicateConfigs (/Users/jack/projects/ocean/node_modules/jest-config/build/index.js:325:13) at readConfigs (/Users/jack/projects/ocean/node_modules/jest-config/build/index.js:474:5) at processTicksAndRejections (node:internal/process/task_queues:95:5) at async runCLI (/Users/jack/projects/ocean/node_modules/jest-cli/node_modules/@jest/core/build/cli/index.js:151:59) at async Object.run (/Users/jack/projects/ocean/node_modules/jest-cli/build/run.js:130:37) ``` ## Expected Behavior Using `getJestProjectsAsync` should work and include all inferred projects. ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #
1 parent d714099 commit cbda8cf

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

packages/jest/src/utils/config/get-jest-projects.spec.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -462,10 +462,7 @@ describe('getJestProjectsAsync', () => {
462462
},
463463
},
464464
});
465-
const expectedResults = [
466-
'<rootDir>/projects/test-1',
467-
'<rootDir>/projects/test-1/jest1.config.ts',
468-
];
465+
const expectedResults = ['<rootDir>/projects/test-1/jest1.config.ts'];
469466
expect(await getJestProjectsAsync()).toEqual(expectedResults);
470467
});
471468

packages/jest/src/utils/config/get-jest-projects.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
type TargetConfiguration,
55
} from '@nx/devkit';
66
import { readWorkspaceConfig } from 'nx/src/project-graph/file-utils';
7-
import { join } from 'path';
7+
import { join, parse } from 'path';
88
import * as yargs from 'yargs-parser';
99

1010
function getJestConfigProjectPath(projectJestConfigPath: string): string {
@@ -120,9 +120,20 @@ export async function getJestProjectsAsync() {
120120
}
121121
}
122122

123+
removeDuplicates(jestConfigurations);
123124
return Array.from(jestConfigurations);
124125
}
125126

127+
// If two paths result in same project, prefer the more specific path.
128+
// e.g. <rootDir>/demo/jest.config.js over <rootDir>/demo
129+
function removeDuplicates(configs: Set<string>): void {
130+
configs.forEach((config) => {
131+
const { dir, ext } = parse(config);
132+
// If the directory has been added previously, remove it and keep the current, more specific path.
133+
if (ext) configs.delete(dir);
134+
});
135+
}
136+
126137
function collectJestConfigFromJestExecutor(
127138
targetConfiguration: TargetConfiguration,
128139
jestConfigurations: Set<string>

0 commit comments

Comments
 (0)