Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ interface SupportedOption {

/** Indicates the option only applies to the root Vitest configuration. */
readonly rootOnly?: boolean;

/**
* Creates error messages specific to an invalid value. When absent, or when no
* messages are returned, a generic message based on `expects` is used.
*/
readonly invalidMessages?: (value: unknown) => string[];
}

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

const SEQUENCE_OPTION_FLAGS = [...SUPPORTED_SEQUENCE_OPTIONS.keys()]
.map((name) => `"--sequence.${name}"`)
.join(', ');

/**
* The Vitest CLI options that can be provided via the `runnerArgs` option.
* The keys are the camel case option names as provided by the Vitest command line parser.
Expand All @@ -86,16 +96,36 @@ const SUPPORTED_OPTIONS: ReadonlyMap<string, SupportedOption> = new Map<string,
[
'sequence',
{
expects: `only the following options: ${[...SUPPORTED_SEQUENCE_OPTIONS.keys()]
.map((name) => `"--sequence.${name}"`)
.join(', ')}`,
expects: `only the following options: ${SEQUENCE_OPTION_FLAGS}`,
isValid: (value) =>
typeof value === 'object' &&
value !== null &&
Object.entries(value).every(
([name, nestedValue]) =>
SUPPORTED_SEQUENCE_OPTIONS.get(name)?.isValid(nestedValue) === true,
),
Comment thread
MeAkib marked this conversation as resolved.
Resolved
invalidMessages: (value) => {
if (typeof value !== 'object' || value === null) {
return [];
}
Comment thread
MeAkib marked this conversation as resolved.
Resolved

const messages: string[] = [];
for (const [name, nestedValue] of Object.entries(value)) {
const nestedOption = SUPPORTED_SEQUENCE_OPTIONS.get(name);
if (!nestedOption) {
messages.push(
`The Vitest "--sequence.${name}" option is not supported. ` +
`The "--sequence" option accepts only the following options: ${SEQUENCE_OPTION_FLAGS}.`,
);
} else if (!nestedOption.isValid(nestedValue)) {
messages.push(
`The Vitest "--sequence.${name}" option requires ${nestedOption.expects}.`,
);
}
}

return messages;
},
},
],
[
Expand Down Expand Up @@ -264,7 +294,12 @@ export function parseRunnerArgs(
project[name] = value;
}
} else {
errors.push(`The Vitest "${toFlag(name)}" option requires ${supported.expects}.`);
const messages = supported.invalidMessages?.(value);
if (messages?.length) {
errors.push(...messages);
} else {
errors.push(`The Vitest "${toFlag(name)}" option requires ${supported.expects}.`);
}
}

continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,14 @@ describe('parseRunnerArgs', () => {

it('should reject an unsupported nested sequence option', () => {
expect(() => parse('--sequence.concurrent')).toThrowError(
/The Vitest "--sequence" option requires only the following options: "--sequence.shuffle", "--sequence.seed"\./,
// eslint-disable-next-line max-len
/The Vitest "--sequence.concurrent" option is not supported\. The "--sequence" option accepts only the following options: "--sequence.shuffle", "--sequence.seed"\./,
);
});

it('should reject an invalid value for a supported nested sequence option', () => {
expect(() => parse('--sequence.seed=-1')).toThrowError(
/The Vitest "--sequence.seed" option requires a non-negative integer\./,
);
});

Expand Down
Loading