Skip to content

Commit 656e210

Browse files
authoredFeb 12, 2024··
fix(cli): parse --browser=<name> correctly (#5179)
1 parent 828858f commit 656e210

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed
 

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,12 @@ export const cliOptionsConfig: VitestCLIOptions = {
303303
},
304304
browser: {
305305
description: 'Run tests in the browser. Equivalent to --browser.enabled (default: false)',
306-
argument: '', // allow boolean
306+
argument: '<name>',
307307
transform(browser) {
308308
if (typeof browser === 'boolean')
309309
return { enabled: browser }
310+
if (browser === 'true' || browser === 'false')
311+
return { enabled: browser !== 'false' }
310312
if (typeof browser === 'string')
311313
return { enabled: true, name: browser }
312314
return browser

‎test/core/test/cli-test.test.ts

+36-7
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@ import { createCLI } from '../../../packages/vitest/src/node/cli/cac.js'
33

44
const vitestCli = createCLI()
55

6-
function parseArguments(commands: string, full = false) {
6+
function parseArguments(commands: string, full = false, includeArgs = false) {
77
const cliArgs = commands.trim().replace(/\s+/g, ' ').split(' ')
8-
const { options } = vitestCli.parse(['node', '/index.js', ...cliArgs], {
8+
const { options, args } = vitestCli.parse(['node', '/index.js', ...cliArgs], {
99
run: false,
1010
})
1111
// remove -- and color from the options since they are always present
1212
if (!full) {
1313
delete options['--']
1414
delete options.color
1515
}
16+
17+
if (includeArgs)
18+
return { options, args }
19+
1620
return options
1721
}
1822

@@ -149,11 +153,11 @@ test('array options', () => {
149153
`)
150154

151155
expect(parseArguments(`
152-
--reporter json
153-
--reporter=default
154-
--coverage.reporter=json
155-
--coverage.reporter html
156-
--coverage.extension=ts
156+
--reporter json
157+
--reporter=default
158+
--coverage.reporter=json
159+
--coverage.reporter html
160+
--coverage.extension=ts
157161
--coverage.extension=tsx
158162
`)).toMatchInlineSnapshot(`
159163
{
@@ -217,3 +221,28 @@ test('cache is parsed correctly', () => {
217221
cache: { dir: 'test/cache.json' },
218222
})
219223
})
224+
225+
test('browser as implicit boolean', () => {
226+
const { options, args } = parseArguments('--browser', false, true)
227+
expect(options).toEqual({ browser: { enabled: true } })
228+
expect(args).toEqual([])
229+
})
230+
231+
test('browser as explicit boolean', () => {
232+
const { options, args } = parseArguments('--browser=true', false, true)
233+
expect(options).toEqual({ browser: { enabled: true } })
234+
expect(args).toEqual([])
235+
})
236+
237+
test('browser as explicit boolean with space', () => {
238+
const { options, args } = parseArguments('--browser true', false, true)
239+
expect(options).toEqual({ browser: { enabled: true } })
240+
expect(args).toEqual([])
241+
})
242+
243+
test('browser by name', () => {
244+
const { options, args } = parseArguments('--browser=firefox', false, true)
245+
246+
expect(args).toEqual([])
247+
expect(options).toEqual({ browser: { enabled: true, name: 'firefox' } })
248+
})

0 commit comments

Comments
 (0)
Please sign in to comment.