Skip to content

Commit 922ddfd

Browse files
committed
fix(@angular/build): report the failing sequence sub-option in runner args errors
The `runnerArgs` validation reports every invalid value for the `--sequence` option with a single message that only lists the supported sub-option names: `--sequence.seed=-1` fails with 'The Vitest "--sequence" option requires only the following options: "--sequence.shuffle", "--sequence.seed".' although the user already used one of those options, and nothing states the actual value requirement. Report each failing sub-option individually, matching the phrasing used for top-level options: an unknown name such as `--sequence.concurrent` is reported as not supported alongside the accepted names, and an invalid value such as `--sequence.seed=-1` is reported with the sub-option's own value requirement. An invalid `--sequence.shuffle` value is not testable through the real Vitest parser, which types the option as a boolean flag and treats a provided value as a test file filter.
1 parent 63a1c6a commit 922ddfd

2 files changed

Lines changed: 47 additions & 5 deletions

File tree

‎packages/angular/build/src/builders/unit-test/runners/vitest/runner-args.ts‎

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ interface SupportedOption {
4949

5050
/** Indicates the option only applies to the root Vitest configuration. */
5151
readonly rootOnly?: boolean;
52+
53+
/**
54+
* Creates error messages specific to an invalid value. When absent, or when no
55+
* messages are returned, a generic message based on `expects` is used.
56+
*/
57+
readonly invalidMessages?: (value: unknown) => string[];
5258
}
5359

5460
const isBoolean = (value: unknown): boolean => typeof value === 'boolean';
@@ -63,6 +69,10 @@ const SUPPORTED_SEQUENCE_OPTIONS: ReadonlyMap<string, SupportedOption> = new Map
6369
['seed', COUNT_OPTION],
6470
]);
6571

72+
const SEQUENCE_OPTION_FLAGS = [...SUPPORTED_SEQUENCE_OPTIONS.keys()]
73+
.map((name) => `"--sequence.${name}"`)
74+
.join(', ');
75+
6676
/**
6777
* The Vitest CLI options that can be provided via the `runnerArgs` option.
6878
* The keys are the camel case option names as provided by the Vitest command line parser.
@@ -86,16 +96,36 @@ const SUPPORTED_OPTIONS: ReadonlyMap<string, SupportedOption> = new Map<string,
8696
[
8797
'sequence',
8898
{
89-
expects: `only the following options: ${[...SUPPORTED_SEQUENCE_OPTIONS.keys()]
90-
.map((name) => `"--sequence.${name}"`)
91-
.join(', ')}`,
99+
expects: `only the following options: ${SEQUENCE_OPTION_FLAGS}`,
92100
isValid: (value) =>
93101
typeof value === 'object' &&
94102
value !== null &&
95103
Object.entries(value).every(
96104
([name, nestedValue]) =>
97105
SUPPORTED_SEQUENCE_OPTIONS.get(name)?.isValid(nestedValue) === true,
98106
),
107+
invalidMessages: (value) => {
108+
if (typeof value !== 'object' || value === null) {
109+
return [];
110+
}
111+
112+
const messages: string[] = [];
113+
for (const [name, nestedValue] of Object.entries(value)) {
114+
const nestedOption = SUPPORTED_SEQUENCE_OPTIONS.get(name);
115+
if (!nestedOption) {
116+
messages.push(
117+
`The Vitest "--sequence.${name}" option is not supported. ` +
118+
`The "--sequence" option accepts only the following options: ${SEQUENCE_OPTION_FLAGS}.`,
119+
);
120+
} else if (!nestedOption.isValid(nestedValue)) {
121+
messages.push(
122+
`The Vitest "--sequence.${name}" option requires ${nestedOption.expects}.`,
123+
);
124+
}
125+
}
126+
127+
return messages;
128+
},
99129
},
100130
],
101131
[
@@ -264,7 +294,12 @@ export function parseRunnerArgs(
264294
project[name] = value;
265295
}
266296
} else {
267-
errors.push(`The Vitest "${toFlag(name)}" option requires ${supported.expects}.`);
297+
const messages = supported.invalidMessages?.(value);
298+
if (messages?.length) {
299+
errors.push(...messages);
300+
} else {
301+
errors.push(`The Vitest "${toFlag(name)}" option requires ${supported.expects}.`);
302+
}
268303
}
269304

270305
continue;

‎packages/angular/build/src/builders/unit-test/runners/vitest/runner-args_spec.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,14 @@ describe('parseRunnerArgs', () => {
122122

123123
it('should reject an unsupported nested sequence option', () => {
124124
expect(() => parse('--sequence.concurrent')).toThrowError(
125-
/The Vitest "--sequence" option requires only the following options: "--sequence.shuffle", "--sequence.seed"\./,
125+
// eslint-disable-next-line max-len
126+
/The Vitest "--sequence.concurrent" option is not supported\. The "--sequence" option accepts only the following options: "--sequence.shuffle", "--sequence.seed"\./,
127+
);
128+
});
129+
130+
it('should reject an invalid value for a supported nested sequence option', () => {
131+
expect(() => parse('--sequence.seed=-1')).toThrowError(
132+
/The Vitest "--sequence.seed" option requires a non-negative integer\./,
126133
);
127134
});
128135

0 commit comments

Comments
 (0)