From 8f565dbd38dfd7618bbc91c08b278b7b275cf381 Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Fri, 4 Oct 2024 14:38:32 -0400 Subject: [PATCH] fix(testing): getJestProjectsAsync no longer duplicates project paths --- .../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