From cbda8cf27591c0659b69c357e374e5aa17f61cc3 Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Fri, 4 Oct 2024 16:50:05 -0400 Subject: [PATCH] fix(testing): getJestProjectsAsync no longer duplicates project paths (#28311) ## 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) Fixes # --- .../jest/src/utils/config/get-jest-projects.spec.ts | 5 +---- packages/jest/src/utils/config/get-jest-projects.ts | 13 ++++++++++++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/packages/jest/src/utils/config/get-jest-projects.spec.ts b/packages/jest/src/utils/config/get-jest-projects.spec.ts index 06f930a63da33..857c615db4016 100644 --- a/packages/jest/src/utils/config/get-jest-projects.spec.ts +++ b/packages/jest/src/utils/config/get-jest-projects.spec.ts @@ -462,10 +462,7 @@ describe('getJestProjectsAsync', () => { }, }, }); - const expectedResults = [ - '/projects/test-1', - '/projects/test-1/jest1.config.ts', - ]; + const expectedResults = ['/projects/test-1/jest1.config.ts']; expect(await getJestProjectsAsync()).toEqual(expectedResults); }); diff --git a/packages/jest/src/utils/config/get-jest-projects.ts b/packages/jest/src/utils/config/get-jest-projects.ts index d9c68aa0e8072..b7307ab2b52f3 100644 --- a/packages/jest/src/utils/config/get-jest-projects.ts +++ b/packages/jest/src/utils/config/get-jest-projects.ts @@ -4,7 +4,7 @@ import { type TargetConfiguration, } from '@nx/devkit'; import { readWorkspaceConfig } from 'nx/src/project-graph/file-utils'; -import { join } from 'path'; +import { join, parse } from 'path'; import * as yargs from 'yargs-parser'; function getJestConfigProjectPath(projectJestConfigPath: string): string { @@ -120,9 +120,20 @@ export async function getJestProjectsAsync() { } } + removeDuplicates(jestConfigurations); return Array.from(jestConfigurations); } +// If two paths result in same project, prefer the more specific path. +// e.g. /demo/jest.config.js over /demo +function removeDuplicates(configs: Set): void { + configs.forEach((config) => { + const { dir, ext } = parse(config); + // If the directory has been added previously, remove it and keep the current, more specific path. + if (ext) configs.delete(dir); + }); +} + function collectJestConfigFromJestExecutor( targetConfiguration: TargetConfiguration, jestConfigurations: Set