From be4cd3eb7563e4c9b106f4df00f6ab6b0d361457 Mon Sep 17 00:00:00 2001 From: Bryandero98 Date: Wed, 2 Sep 2026 22:10:52 -0500 Subject: [PATCH] test: add regression coverage for parse() argv fallback under vite-node #158 reported that CAC.parse() with no explicit argv produced wrong results when the CLI was run via `npx vite-node file.ts ...` instead of plain `node`. Verified on current main that this no longer reproduces (most likely fixed as a side effect of b943ceb's rewrite of the old src/node.ts argv-detection logic into src/runtime.ts), but the existing suite only ever exercised the no-argv fallback path via plain `node` subprocesses, so there was no coverage for an alternative JS runtime. Adds vite-node as a devDependency and a test that runs the same basic-usage fixture through it, asserting parse() still resolves to the real CLI args. Co-Authored-By: Claude Sonnet 5 --- package.json | 1 + pnpm-lock.yaml | 29 +++++++++++++++++++++++++++++ tests/index.test.ts | 28 ++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) diff --git a/package.json b/package.json index e87a8b12..079394cd 100644 --- a/package.json +++ b/package.json @@ -59,6 +59,7 @@ "tsdown": "^0.22.14", "tsdown-preset-sxzz": "^0.7.0", "typescript": "^6.0.3", + "vite-node": "^6.0.0", "vitest": "^4.1.11" }, "husky": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0392f70e..39da8e04 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -160,6 +160,9 @@ importers: typescript: specifier: ^6.0.3 version: 6.0.3 + vite-node: + specifier: ^6.0.0 + version: 6.0.0(@types/node@26.4.0)(jiti@2.7.0)(yaml@2.9.0) vitest: specifier: ^4.1.11 version: 4.1.11(@types/node@26.4.0)(@vitest/coverage-v8@4.1.11)(vite@8.2.2(@types/node@26.4.0)(jiti@2.7.0)(yaml@2.9.0)) @@ -2198,6 +2201,11 @@ packages: resolution: {integrity: sha512-zj/ob3UsvJGN0whEAKFp53REA5X66hvffVqoCtVQAakJKnKlH+/PcOfMoFwIG/o4rElqLv/ycAFlx8ZlXUorCg==} engines: {node: '>=18.12.0'} + vite-node@6.0.0: + resolution: {integrity: sha512-oj4PVrT+pDh6GYf5wfUXkcZyekYS8kKPfLPXVl8qe324Ec6l4K2DUKNadRbZ3LQl0qGcDz+PyOo7ZAh00Y+JjQ==} + engines: {node: ^20.19.0 || >=22.12.0} + hasBin: true + vite@8.2.2: resolution: {integrity: sha512-cFKLV/PRgAUlIRm5WjMjJ86jrftzpqcgH+Us+DS8mI3CDNiH30Whrz8uHL3+MOLPAgqbMBAqWdAHAphOAM+z/Q==} engines: {node: ^20.19.0 || >=22.12.0} @@ -4385,6 +4393,27 @@ snapshots: verkit@0.3.2: {} + vite-node@6.0.0(@types/node@26.4.0)(jiti@2.7.0)(yaml@2.9.0): + dependencies: + cac: 7.0.0 + es-module-lexer: 2.3.2 + obug: 2.1.4 + pathe: 2.0.3 + vite: 8.2.2(@types/node@26.4.0)(jiti@2.7.0)(yaml@2.9.0) + transitivePeerDependencies: + - '@types/node' + - '@vitejs/devtools' + - esbuild + - jiti + - less + - sass + - sass-embedded + - stylus + - sugarss + - terser + - tsx + - yaml + vite@8.2.2(@types/node@26.4.0)(jiti@2.7.0)(yaml@2.9.0): dependencies: lightningcss: 1.33.0 diff --git a/tests/index.test.ts b/tests/index.test.ts index bbb176b4..2e7459ca 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -49,6 +49,34 @@ snapshotOutput({ args: ['build'], }) +test('parse() with no explicit argv under an alternative runtime (vite-node)', async () => { + // Regression test for https://github.com/cacjs/cac/issues/158. + // `.parse()` with no explicit argv falls back to `runtimeProcessArgs` + // (captured from `process.argv` in src/runtime.ts). Every other test in + // this file exercises that fallback only via a plain `node` subprocess; + // this runs the same fixture through vite-node, which executes the file + // in its own SSR/module context, to confirm the fallback still resolves + // to the real CLI args (not vite-node's own) when invoked as + // `vite-node basic-usage.ts foo bar --type ok command`. + const { stdout } = await x('npx', [ + 'vite-node', + example('basic-usage.ts'), + 'foo', + 'bar', + '--type', + 'ok', + 'command', + ]) + + expect(JSON.parse(stdout)).toEqual({ + args: ['foo', 'bar', 'command'], + options: { + '--': [], + type: 'ok', + }, + }) +}) + test('negated option', () => { const cli = cac()