Skip to content
12 changes: 9 additions & 3 deletions packages/vitest/src/node/config/resolveConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,17 +314,23 @@ export function resolveConfig(
}
}

// apply browser CLI options only if the config already has the browser config and not disabled manually
// apply browser CLI options only if the config already has the browser config,
// OR if this is the root config and we explicitly passed CLI options.
if (
vitest._cliOptions.browser
&& resolved.browser
&& (resolved.browser || vitest.vite?.config === viteConfig)
// if enabled is set to `false`, but CLI overrides it, then always override it
&& (resolved.browser.enabled !== false || vitest._cliOptions.browser.enabled)
&& (resolved.browser?.enabled !== false || vitest._cliOptions.browser.enabled)
) {
resolved.browser = mergeConfig(
resolved.browser,
vitest._cliOptions.browser,
) as ResolvedBrowserOptions
if (resolved.browser.instances) {
resolved.browser.instances.forEach((instance) => {
instance.name ??= resolved.name ? `${resolved.name} (${instance.browser})` : instance.browser
})
}
}

resolved.browser ??= {} as any
Expand Down
49 changes: 49 additions & 0 deletions test/cli/test/config/browser-configs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,55 @@ test('filters projects with a wildcard', async () => {
])
})

test('allows browser mode without projects', async () => {
await vitest({}, {
browser: {
enabled: true,
headless: true,
provider: playwright(),
instances: [{ browser: 'chromium' }],
},
})
})

test('applies browser CLI options to root config without existing browser config', async () => {
const { projects } = await vitest({
browser: {
enabled: true,
provider: preview(),
headless: true,
instances: [
{ browser: 'chromium' },
{ browser: 'firefox' },
],
},
}, {})
expect(projects.map(p => p.name)).toEqual([
'chromium',
'firefox',
])
})

test('assigns instance names from CLI options without existing browser config', async () => {
const { projects } = await vitest({
browser: {
enabled: true,
provider: preview(),
headless: true,
instances: [
{ browser: 'chromium' },
{ browser: 'firefox', name: 'my-firefox' },
],
},
}, {
name: 'myproject',
})
expect(projects.map(p => p.name)).toEqual([
'myproject (chromium)',
'my-firefox',
])
})

test('assigns names as browsers in a custom project', async () => {
const { projects } = await vitest({}, {
projects: [
Expand Down
Loading