Skip to content

Commit

Permalink
fix(browser): don't fail when running --browser.headless if the brows…
Browse files Browse the repository at this point in the history
…er projest is part of the workspace (#7311)
  • Loading branch information
sheremet-va authored Jan 20, 2025
1 parent fa4634b commit e43a8f5
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 18 deletions.
28 changes: 16 additions & 12 deletions packages/vitest/src/node/config/resolveConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,20 +235,24 @@ export function resolveConfig(

if (browser.enabled) {
if (!browser.name && !browser.instances) {
throw new Error(`Vitest Browser Mode requires "browser.name" (deprecated) or "browser.instances" options, none were set.`)
}

const configs = browser.instances
if (browser.name && browser.instances) {
// --browser=chromium filters configs to a single one
browser.instances = browser.instances.filter(instance => instance.browser === browser.name)
// CLI can enable `--browser.*` flag to change config of workspace projects
// the same flag will be applied to the root config that doesn't have to have "name" or "instances"
// in this case we just disable the browser mode
browser.enabled = false
}
else {
const instances = browser.instances
if (browser.name && browser.instances) {
// --browser=chromium filters configs to a single one
browser.instances = browser.instances.filter(instance => instance.browser === browser.name)
}

if (browser.instances && !browser.instances.length) {
throw new Error([
`"browser.instances" was set in the config, but the array is empty. Define at least one browser config.`,
browser.name && configs?.length ? ` The "browser.name" was set to "${browser.name}" which filtered all configs (${configs.map(c => c.browser).join(', ')}). Did you mean to use another name?` : '',
].join(''))
if (browser.instances && !browser.instances.length) {
throw new Error([
`"browser.instances" was set in the config, but the array is empty. Define at least one browser config.`,
browser.name && instances?.length ? ` The "browser.name" was set to "${browser.name}" which filtered all configs (${instances.map(c => c.browser).join(', ')}). Did you mean to use another name?` : '',
].join(''))
}
}
}

Expand Down
42 changes: 42 additions & 0 deletions test/config/test/browser-configs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,45 @@ test('coverage provider v8 works correctly in browser mode if instances are filt
'chromium',
])
})

test('can enable browser-cli options for multi-project workspace', async () => {
const { projects } = await vitest(
{
browser: {
enabled: true,
headless: true,
},
},
{
workspace: [
{
test: {
name: 'unit',
},
},
{
test: {
browser: {
enabled: true,
provider: 'playwright',
instances: [
{ browser: 'chromium', name: 'browser' },
],
},
},
},
],
},
)
expect(projects.map(p => p.name)).toEqual([
'unit',
'browser',
])

// unit config
expect(projects[0].config.browser.enabled).toBe(false)

// browser config
expect(projects[1].config.browser.enabled).toBe(true)
expect(projects[1].config.browser.headless).toBe(true)
})
6 changes: 0 additions & 6 deletions test/config/test/failures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,12 +411,6 @@ test('maxConcurrency 0 prints a warning', async () => {
expect(stderr).toMatch('The option "maxConcurrency" cannot be set to 0. Using default value 5 instead.')
})

test('browser.name or browser.instances are required', async () => {
const { stderr, exitCode } = await runVitestCli('--browser.enabled')
expect(exitCode).toBe(1)
expect(stderr).toMatch('Vitest Browser Mode requires "browser.name" (deprecated) or "browser.instances" options, none were set.')
})

test('browser.instances is empty', async () => {
const { stderr } = await runVitest({
browser: {
Expand Down

0 comments on commit e43a8f5

Please sign in to comment.