Skip to content

Commit 93fbd02

Browse files
authoredMay 4, 2023
fix(cli): improve cac errors when mixing boolean and dot notation (#3302)
1 parent 5382e8b commit 93fbd02

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed
 

‎packages/vitest/src/node/cli.ts

+27-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,33 @@ cli
7777
.command('[...filters]')
7878
.action((filters, options) => start('test', filters, options))
7979

80-
cli.parse()
80+
try {
81+
cli.parse()
82+
}
83+
catch (originalError) {
84+
// CAC may fail to parse arguments when boolean flags and dot notation are mixed
85+
// e.g. "--coverage --coverage.reporter text" will fail, when "--coverage.enabled --coverage.reporter text" will pass
86+
const fullArguments = cli.rawArgs.join(' ')
87+
const conflictingArgs: { arg: string; dotArgs: string[] }[] = []
88+
89+
for (const arg of cli.rawArgs) {
90+
if (arg.startsWith('--') && !arg.includes('.') && fullArguments.includes(`${arg}.`)) {
91+
const dotArgs = cli.rawArgs.filter(rawArg => rawArg.startsWith(arg) && rawArg.includes('.'))
92+
conflictingArgs.push({ arg, dotArgs })
93+
}
94+
}
95+
96+
if (conflictingArgs.length === 0)
97+
throw originalError
98+
99+
const error = conflictingArgs
100+
.map(({ arg, dotArgs }) =>
101+
`A boolean argument "${arg}" was used with dot notation arguments "${dotArgs.join(' ')}".`
102+
+ `\nPlease specify the "${arg}" argument with dot notation as well: "${arg}.enabled"`)
103+
.join('\n')
104+
105+
throw new Error(error)
106+
}
81107

82108
async function runRelated(relatedFiles: string[] | string, argv: CliOptions): Promise<void> {
83109
argv.related = relatedFiles

‎test/config/test/failures.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,17 @@ test('c8 coverage provider cannot be used with browser', async () => {
4343

4444
expect(error).toMatch('Error: @vitest/coverage-c8 does not work with --browser. Use @vitest/coverage-istanbul instead')
4545
})
46+
47+
test('boolean coverage flag without dot notation, with more dot notation options', async () => {
48+
const { error } = await runVitest('run', ['--coverage', '--coverage.reporter', 'text'])
49+
50+
expect(error).toMatch('Error: A boolean argument "--coverage" was used with dot notation arguments "--coverage.reporter".')
51+
expect(error).toMatch('Please specify the "--coverage" argument with dot notation as well: "--coverage.enabled"')
52+
})
53+
54+
test('boolean browser flag without dot notation, with more dot notation options', async () => {
55+
const { error } = await runVitest('run', ['--browser', '--browser.name', 'chrome'])
56+
57+
expect(error).toMatch('Error: A boolean argument "--browser" was used with dot notation arguments "--browser.name".')
58+
expect(error).toMatch('Please specify the "--browser" argument with dot notation as well: "--browser.enabled"')
59+
})

‎test/config/test/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export async function runVitest(mode: 'run' | 'watch', cliArguments: string[]) {
66
let error = ''
77

88
subprocess.stderr?.on('data', (data) => {
9-
error = stripAnsi(data.toString())
9+
error += stripAnsi(data.toString())
1010

1111
// Sometimes on Windows CI execa doesn't exit properly. Force exit when stderr is caught.
1212
subprocess.kill()

‎test/config/vitest.config.ts

+3
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@ import { defineConfig } from 'vitest/config'
33
export default defineConfig({
44
test: {
55
testTimeout: 60_000,
6+
chaiConfig: {
7+
truncateThreshold: 999,
8+
},
69
},
710
})

0 commit comments

Comments
 (0)
Please sign in to comment.