From e43a8f5666b65c945a7e5d7acbc2af5ae1d40fcb Mon Sep 17 00:00:00 2001 From: Vladimir Date: Mon, 20 Jan 2025 17:21:45 +0100 Subject: [PATCH] fix(browser): don't fail when running --browser.headless if the browser projest is part of the workspace (#7311) --- .../vitest/src/node/config/resolveConfig.ts | 28 +++++++------ test/config/test/browser-configs.test.ts | 42 +++++++++++++++++++ test/config/test/failures.test.ts | 6 --- 3 files changed, 58 insertions(+), 18 deletions(-) diff --git a/packages/vitest/src/node/config/resolveConfig.ts b/packages/vitest/src/node/config/resolveConfig.ts index 5c2fe4f10526..a72f44261dc6 100644 --- a/packages/vitest/src/node/config/resolveConfig.ts +++ b/packages/vitest/src/node/config/resolveConfig.ts @@ -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('')) + } } } diff --git a/test/config/test/browser-configs.test.ts b/test/config/test/browser-configs.test.ts index 8cb122b40d48..de0ad210685a 100644 --- a/test/config/test/browser-configs.test.ts +++ b/test/config/test/browser-configs.test.ts @@ -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) +}) diff --git a/test/config/test/failures.test.ts b/test/config/test/failures.test.ts index 0e1d4cadf50e..fd453927dc88 100644 --- a/test/config/test/failures.test.ts +++ b/test/config/test/failures.test.ts @@ -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: {