fix(runner): validate `config.tsconfig` (#34849)

This commit is contained in:
Simon Knott 2025-02-20 09:11:50 +01:00 committed by GitHub
parent d5adeb3cf4
commit aca3fb275b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 42 additions and 0 deletions

View File

@ -254,6 +254,13 @@ function validateConfig(file: string, config: Config) {
else if (typeof config.workers === 'string' && !config.workers.endsWith('%'))
throw errorWithFile(file, `config.workers must be a number or percentage`);
}
if ('tsconfig' in config && config.tsconfig !== undefined) {
if (typeof config.tsconfig !== 'string')
throw errorWithFile(file, `config.tsconfig must be a string`);
if (!fs.existsSync(path.resolve(file, '..', config.tsconfig)))
throw errorWithFile(file, `config.tsconfig does not exist`);
}
}
function validateProject(file: string, project: Project, title: string) {

View File

@ -337,6 +337,9 @@ function overridesFromOptions(options: { [key: string]: any }): ConfigCLIOverrid
overrides.use = overrides.use || {};
overrides.use.trace = options.trace;
}
if (overrides.tsconfig && !fs.existsSync(overrides.tsconfig))
throw new Error(`--tsconfig "${options.tsconfig}" does not exist`);
return overrides;
}

View File

@ -698,3 +698,35 @@ test('should merge ct configs', async ({ runInlineTest }) => {
});
expect(result.exitCode).toBe(0);
});
test('should throw on invalid config.tsconfig option', async ({ runInlineTest }) => {
const result = await runInlineTest({
'playwright.config.ts': `
export default {
tsconfig: true,
};
`,
});
expect(result.exitCode).toBe(1);
expect(result.output).toContain(`config.tsconfig must be a string`);
});
test('should throw on nonexistant config.tsconfig', async ({ runInlineTest }) => {
const result = await runInlineTest({
'playwright.config.ts': `
export default {
tsconfig: './does-not-exist.json',
};
`,
});
expect(result.exitCode).toBe(1);
expect(result.output).toContain(`config.tsconfig does not exist`);
});
test('should throw on invalid --tsconfig', async ({ runInlineTest }) => {
const result = await runInlineTest({}, { 'tsconfig': 'does-not-exist.json' });
expect(result.exitCode).toBe(1);
expect(result.output).toContain(`--tsconfig "does-not-exist.json" does not exist`);
});