Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): parse --browser=<name> correctly #5179

Merged
merged 2 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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' } })
})