Skip to content

Commit 4f91e76

Browse files
authored
fix(cli): parse --bail correctly (#11896)
1 parent 1b2380c commit 4f91e76

3 files changed

Lines changed: 24 additions & 2 deletions

File tree

โ€Ždocs/guide/cli-generated.mdโ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ Default hook timeout in milliseconds (default: `10000`). Use `0` to disable time
611611

612612
### bail
613613

614-
- **CLI:** `--bail <number>`
614+
- **CLI:** `--bail [number]`
615615
- **Config:** [bail](/config/bail)
616616

617617
Stop test execution when given number of tests have failed (default: `0`)

โ€Žpackages/vitest/src/node/cli/cli-config.tsโ€Ž

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,17 @@ export const cliOptionsConfig: VitestCLIOptions = {
609609
},
610610
bail: {
611611
description: 'Stop test execution when given number of tests have failed (default: `0`)',
612-
argument: '<number>',
612+
argument: '[number]',
613+
transform(value) {
614+
if (value === true) {
615+
return 1
616+
}
617+
if (typeof value === 'number') {
618+
return value
619+
}
620+
621+
throw new TypeError(`Unexpected value "--bail=${value}". Use "--bail=1 ${value}" instead.`)
622+
},
613623
},
614624
retry: {
615625
description: 'Retry the test specific number of times if it fails (default: `0`)',

โ€Žtest/unit/test/cli-test.test.tsโ€Ž

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,18 @@ test('maxConcurrency is parsed correctly', () => {
227227
expect(getCLIOptions('--max-concurrency=1000')).toEqual({ maxConcurrency: 1000 })
228228
})
229229

230+
test('bail is parsed correctly', () => {
231+
expect(getCLIOptions('--bail')).toEqual({ bail: 1 })
232+
expect(getCLIOptions('--bail --run')).toEqual({ bail: 1, run: true })
233+
expect(getCLIOptions('--bail 2')).toEqual({ bail: 2 })
234+
expect(getCLIOptions('--bail=2')).toEqual({ bail: 2 })
235+
expect(getCLIOptions('--bail=0')).toEqual({ bail: 0 })
236+
237+
expect(() => getCLIOptions('--bail example.test.ts')).toThrowErrorMatchingInlineSnapshot(
238+
`[TypeError: Unexpected value "--bail=example.test.ts". Use "--bail=1 example.test.ts" instead.]`,
239+
)
240+
})
241+
230242
test('project is parsed correctly', () => {
231243
// cac copies the raw value to the shorthand key, it is ignored by vitest
232244
expect(getCLIOptions('--project space_1')).toEqual({ project: ['space_1'], p: 'space_1' })

0 commit comments

Comments
ย (0)