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(browser): don't fail when running --browser.headless if the browser projest is part of the workspace #7311

Merged
merged 1 commit into from
Jan 20, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading