Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/jest/src/options-converter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,23 @@ describe('Convert options to Jest CLI arguments', () => {
const argv = optionsConverter.convertToCliArgs({ '--': ['non-flag-1', 'non-flag-2'] } as any);
expect(argv).toEqual(['non-flag-1 non-flag-2']);
});

// --- findRelatedTests regression tests ---
// Jest treats --findRelatedTests as a boolean flag; source files are positional
// args (yargs `_`). Angular CLI delivers array schema fields as a proper string[].
describe('findRelatedTests', () => {
it('should emit --findRelatedTests once and files as trailing positionals (single file)', () => {
const argv = optionsConverter.convertToCliArgs({
findRelatedTests: ['src/foo.spec.ts'],
} as any);
expect(argv).toEqual(['--findRelatedTests', 'src/foo.spec.ts']);
});

it('should emit --findRelatedTests once and files as trailing positionals (multiple files)', () => {
const argv = optionsConverter.convertToCliArgs({
findRelatedTests: ['src/foo.spec.ts', 'src/bar.spec.ts'],
} as any);
expect(argv).toEqual(['--findRelatedTests', 'src/foo.spec.ts', 'src/bar.spec.ts']);
});
});
});
26 changes: 23 additions & 3 deletions packages/jest/src/options-converter.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
import { SchemaObject as JestBuilderSchema } from './schema';

/**
* Options that Jest treats as a boolean flag with file paths as trailing positional
* arguments (parsed via yargs `_`). For these options the converter emits the flag
* once and appends every path as a bare positional at the end of argv.
*
* Example: `--findRelatedTests file1.ts file2.ts`
* Jest yargs parsing: `findRelatedTests=true`, `_=['file1.ts', 'file2.ts']`
*/
const POSITIONAL_ARRAY_OPTIONS = new Set(['findRelatedTests']);

export class OptionsConverter {
convertToCliArgs(options: Partial<JestBuilderSchema>): string[] {
const argv = [];
const positionalArgs: string[] = [];
let nonFlagArgs: string | undefined;

for (const option of Object.keys(options)) {
let optionValue = options[option];
if (option == '--') {
const optionValue = options[option];

if (option === '--') {
nonFlagArgs = (optionValue as string[]).join(' ');
} else if (POSITIONAL_ARRAY_OPTIONS.has(option)) {
// These options use a boolean flag + trailing positional file paths.
// Angular CLI delivers them as a proper string array (one element per file).
argv.push(`--${option}`);
positionalArgs.push(...(optionValue as string[]));
} else if (optionValue === true) {
argv.push(`--${option}`);
} else if (typeof optionValue === 'string' || typeof optionValue === 'number') {
Expand All @@ -18,9 +36,11 @@ export class OptionsConverter {
}
}
}

if (nonFlagArgs) {
argv.push(nonFlagArgs);
}
return argv;

return [...argv, ...positionalArgs];
}
}
1 change: 0 additions & 1 deletion packages/jest/tests/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ module.exports = [
command:
'node ../../../packages/jest/tests/validate.js my-shared-library --find-related-tests projects/my-shared-library/src/lib/my-shared-library.service.ts projects/my-shared-library/src/lib/my-shared-library.component.ts --expect-suites=2 --expect-tests=2',
},

// E2E sanity
{
id: 'e2e-simple-app',
Expand Down
Loading