fix(config): preserve raw values for config coercion (#2747)

Fixes #2746. Returning the inner yargs parser result applies option
coercers twice, so JSON-configured viewport and wsHeaders fail.
Returning the original JSON values after strict validation lets the
outer parser coerce them once and preserves CLI overrides. Tests: npm
run test -- tests/cli.test.ts and npm run check-format pass. The full
npm run test reaches five unrelated PWA failures because the test server
cannot bind 0.0.0.0:10236 with EACCES on this Windows host.
This commit is contained in:
Wu Shuwen
2026-09-15 08:23:41 +00:00
committed by GitHub
parent 342d2430ce
commit 906c83b004
2 changed files with 30 additions and 1 deletions
+2 -1
View File
@@ -456,7 +456,7 @@ export function parser(
throw new Error('Config must be a JSON object');
}
return yargs()
yargs()
.parserConfiguration({
'strip-aliased': true,
'camel-case-expansion': false,
@@ -467,6 +467,7 @@ export function parser(
.fail(false)
.exitProcess(false)
.parseSync([]);
return parsed;
} catch (err) {
throw new Error(`Invalid JSON config file: ${(err as Error).message}`);
}
+28
View File
@@ -487,6 +487,34 @@ describe('cli args parsing', () => {
assert.strictEqual(args.categoryMemory, true);
});
it('applies config coercion for viewport and wsHeaders', async () => {
using testConfig = createTempFile(
JSON.stringify({
wsEndpoint: 'ws://127.0.0.1:9222/devtools/browser/abc123',
wsHeaders: '{"Authorization":"Bearer token"}',
viewport: '1280x720',
}),
'cd4a.test.config.coercion.json',
);
const args = parseArguments(['--config', testConfig.path]);
assert.deepStrictEqual(args.viewport, {width: 1280, height: 720});
assert.deepStrictEqual(args.wsHeaders, {Authorization: 'Bearer token'});
});
it('lets cli options override coerced config values', async () => {
using testConfig = createTempFile(
JSON.stringify({viewport: '1280x720'}),
'cd4a.test.config.coercion-override.json',
);
const args = parseArguments([
'--config',
testConfig.path,
'--viewport',
'800x600',
]);
assert.deepStrictEqual(args.viewport, {width: 800, height: 600});
});
it('parses config should not allow no prefix', async () => {
using testConfig = createTempFile(
JSON.stringify({