Skip to content

Commit

Permalink
fix(testing): getJestProjectsAsync no longer duplicates project paths
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo committed Oct 4, 2024
1 parent f743808 commit 8f565db
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
5 changes: 1 addition & 4 deletions packages/jest/src/utils/config/get-jest-projects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,10 +462,7 @@ describe('getJestProjectsAsync', () => {
},
},
});
const expectedResults = [
'<rootDir>/projects/test-1',
'<rootDir>/projects/test-1/jest1.config.ts',
];
const expectedResults = ['<rootDir>/projects/test-1/jest1.config.ts'];
expect(await getJestProjectsAsync()).toEqual(expectedResults);
});

Expand Down
13 changes: 12 additions & 1 deletion packages/jest/src/utils/config/get-jest-projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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. <rootDir>/demo/jest.config.js over <rootDir>/demo
function removeDuplicates(configs: Set<string>): 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<string>
Expand Down

0 comments on commit 8f565db

Please sign in to comment.