Visitar URL original
fix(browser): apply API options without browser config (#10852) · vitest-dev/vitest@ad05583 · GitHub
Skip to content

Commit ad05583

Browse files
authored
fix(browser): apply API options without browser config (#10852)
1 parent a8ea3c8 commit ad05583

2 files changed

Lines changed: 44 additions & 4 deletions

File tree

‎packages/vitest/src/node/plugins/testConfig.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,17 @@ export function TestConfigPlugin(
7979
// By default, Vite extends arrays, for example, but CLI options should have the priority
8080
config.test = deepMerge({}, config.test ?? {}, options)
8181

82-
// apply browser CLI options only if the config already has the browser config and not disabled manually
82+
// apply browser CLI options only if the config already has the browser config and not disabled manually,
83+
// but let the API create it for a root config without projects (the CLI can't set `provider` or `instances`)
84+
const createsRootBrowserConfig =
85+
!globalConfig && !config.test.projects && !!(browser?.provider || browser?.instances)
8386
if (
84-
config.test.browser &&
8587
browser &&
86-
(config.test.browser.enabled !== false || browser.enabled)
88+
(config.test.browser || createsRootBrowserConfig) &&
89+
(config.test.browser?.enabled !== false || browser.enabled)
8790
) {
8891
config.test.browser = mergeConfig(
89-
config.test.browser,
92+
config.test.browser ?? {},
9093
browser,
9194
) as ResolvedBrowserOptions
9295
}

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,43 @@ test('assigns names as browsers', async () => {
181181
expect(projects.map((p) => p.projectConfig.name)).toEqual(['chromium', 'firefox', 'webkit'])
182182
})
183183

184+
test('applies programmatic browser options without existing browser config', async () => {
185+
const v = await vitest({
186+
$cliConfig: {
187+
browser: {
188+
enabled: true,
189+
headless: true,
190+
provider: preview(),
191+
instances: [{ browser: 'chromium' }],
192+
},
193+
},
194+
})
195+
196+
expect(v.projects.map((project) => project.name)).toEqual(['chromium'])
197+
})
198+
199+
test('applies programmatic browser options only to projects with browser config', async () => {
200+
const v = await vitest({
201+
projects: [
202+
{ test: { name: 'node' } },
203+
{ test: { name: 'browser', browser: { instances: [{ browser: 'chromium' }] } } },
204+
],
205+
$cliConfig: {
206+
browser: {
207+
enabled: true,
208+
headless: true,
209+
provider: preview(),
210+
},
211+
},
212+
})
213+
214+
expect(v.config.browser.enabled).toBe(false)
215+
expect(v.projects.map((project) => [project.name, project.config.browser.enabled])).toEqual([
216+
['node', false],
217+
['browser (chromium)', true],
218+
])
219+
})
220+
184221
test('filters projects', async () => {
185222
const projects = await config({
186223
project: 'chromium',

0 commit comments

Comments
 (0)