Skip to content

Commit

Permalink
fix(cli): parse --browser=<name> correctly (#5179)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio authored Feb 12, 2024
1 parent 828858f commit 656e210
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
4 changes: 3 additions & 1 deletion packages/vitest/src/node/cli/cli-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,12 @@ export const cliOptionsConfig: VitestCLIOptions = {
},
browser: {
description: 'Run tests in the browser. Equivalent to --browser.enabled (default: false)',
argument: '', // allow boolean
argument: '<name>',
transform(browser) {
if (typeof browser === 'boolean')
return { enabled: browser }
if (browser === 'true' || browser === 'false')
return { enabled: browser !== 'false' }
if (typeof browser === 'string')
return { enabled: true, name: browser }
return browser
Expand Down
43 changes: 36 additions & 7 deletions test/core/test/cli-test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ import { createCLI } from '../../../packages/vitest/src/node/cli/cac.js'

const vitestCli = createCLI()

function parseArguments(commands: string, full = false) {
function parseArguments(commands: string, full = false, includeArgs = false) {
const cliArgs = commands.trim().replace(/\s+/g, ' ').split(' ')
const { options } = vitestCli.parse(['node', '/index.js', ...cliArgs], {
const { options, args } = vitestCli.parse(['node', '/index.js', ...cliArgs], {
run: false,
})
// remove -- and color from the options since they are always present
if (!full) {
delete options['--']
delete options.color
}

if (includeArgs)
return { options, args }

return options
}

Expand Down Expand Up @@ -149,11 +153,11 @@ test('array options', () => {
`)

expect(parseArguments(`
--reporter json
--reporter=default
--coverage.reporter=json
--coverage.reporter html
--coverage.extension=ts
--reporter json
--reporter=default
--coverage.reporter=json
--coverage.reporter html
--coverage.extension=ts
--coverage.extension=tsx
`)).toMatchInlineSnapshot(`
{
Expand Down Expand Up @@ -217,3 +221,28 @@ test('cache is parsed correctly', () => {
cache: { dir: 'test/cache.json' },
})
})

test('browser as implicit boolean', () => {
const { options, args } = parseArguments('--browser', false, true)
expect(options).toEqual({ browser: { enabled: true } })
expect(args).toEqual([])
})

test('browser as explicit boolean', () => {
const { options, args } = parseArguments('--browser=true', false, true)
expect(options).toEqual({ browser: { enabled: true } })
expect(args).toEqual([])
})

test('browser as explicit boolean with space', () => {
const { options, args } = parseArguments('--browser true', false, true)
expect(options).toEqual({ browser: { enabled: true } })
expect(args).toEqual([])
})

test('browser by name', () => {
const { options, args } = parseArguments('--browser=firefox', false, true)

expect(args).toEqual([])
expect(options).toEqual({ browser: { enabled: true, name: 'firefox' } })
})

0 comments on commit 656e210

Please sign in to comment.