Skip to content

Commit e43a8f5

Browse files
authored
fix(browser): don't fail when running --browser.headless if the browser projest is part of the workspace (#7311)
1 parent fa4634b commit e43a8f5

File tree

3 files changed

+58
-18
lines changed

3 files changed

+58
-18
lines changed

packages/vitest/src/node/config/resolveConfig.ts

+16-12
Original file line numberDiff line numberDiff line change
@@ -235,20 +235,24 @@ export function resolveConfig(
235235

236236
if (browser.enabled) {
237237
if (!browser.name && !browser.instances) {
238-
throw new Error(`Vitest Browser Mode requires "browser.name" (deprecated) or "browser.instances" options, none were set.`)
239-
}
240-
241-
const configs = browser.instances
242-
if (browser.name && browser.instances) {
243-
// --browser=chromium filters configs to a single one
244-
browser.instances = browser.instances.filter(instance => instance.browser === browser.name)
238+
// CLI can enable `--browser.*` flag to change config of workspace projects
239+
// the same flag will be applied to the root config that doesn't have to have "name" or "instances"
240+
// in this case we just disable the browser mode
241+
browser.enabled = false
245242
}
243+
else {
244+
const instances = browser.instances
245+
if (browser.name && browser.instances) {
246+
// --browser=chromium filters configs to a single one
247+
browser.instances = browser.instances.filter(instance => instance.browser === browser.name)
248+
}
246249

247-
if (browser.instances && !browser.instances.length) {
248-
throw new Error([
249-
`"browser.instances" was set in the config, but the array is empty. Define at least one browser config.`,
250-
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?` : '',
251-
].join(''))
250+
if (browser.instances && !browser.instances.length) {
251+
throw new Error([
252+
`"browser.instances" was set in the config, but the array is empty. Define at least one browser config.`,
253+
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?` : '',
254+
].join(''))
255+
}
252256
}
253257
}
254258

test/config/test/browser-configs.test.ts

+42
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,45 @@ test('coverage provider v8 works correctly in browser mode if instances are filt
197197
'chromium',
198198
])
199199
})
200+
201+
test('can enable browser-cli options for multi-project workspace', async () => {
202+
const { projects } = await vitest(
203+
{
204+
browser: {
205+
enabled: true,
206+
headless: true,
207+
},
208+
},
209+
{
210+
workspace: [
211+
{
212+
test: {
213+
name: 'unit',
214+
},
215+
},
216+
{
217+
test: {
218+
browser: {
219+
enabled: true,
220+
provider: 'playwright',
221+
instances: [
222+
{ browser: 'chromium', name: 'browser' },
223+
],
224+
},
225+
},
226+
},
227+
],
228+
},
229+
)
230+
expect(projects.map(p => p.name)).toEqual([
231+
'unit',
232+
'browser',
233+
])
234+
235+
// unit config
236+
expect(projects[0].config.browser.enabled).toBe(false)
237+
238+
// browser config
239+
expect(projects[1].config.browser.enabled).toBe(true)
240+
expect(projects[1].config.browser.headless).toBe(true)
241+
})

test/config/test/failures.test.ts

-6
Original file line numberDiff line numberDiff line change
@@ -411,12 +411,6 @@ test('maxConcurrency 0 prints a warning', async () => {
411411
expect(stderr).toMatch('The option "maxConcurrency" cannot be set to 0. Using default value 5 instead.')
412412
})
413413

414-
test('browser.name or browser.instances are required', async () => {
415-
const { stderr, exitCode } = await runVitestCli('--browser.enabled')
416-
expect(exitCode).toBe(1)
417-
expect(stderr).toMatch('Vitest Browser Mode requires "browser.name" (deprecated) or "browser.instances" options, none were set.')
418-
})
419-
420414
test('browser.instances is empty', async () => {
421415
const { stderr } = await runVitest({
422416
browser: {

0 commit comments

Comments
 (0)