Skip to content

Commit e84e218

Browse files
authored
fix: generate a separate config for "vitest init browser" instead of a workspace file (#7934)
1 parent dca0612 commit e84e218

File tree

3 files changed

+29
-63
lines changed

3 files changed

+29
-63
lines changed

packages/vitest/src/create/browser/creator.ts

Lines changed: 16 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -257,38 +257,6 @@ function fail() {
257257
process.exitCode = 1
258258
}
259259

260-
async function generateWorkspaceFile(options: {
261-
configPath: string
262-
rootConfig: string
263-
provider: string
264-
browsers: string[]
265-
}) {
266-
const relativeRoot = relative(dirname(options.configPath), options.rootConfig)
267-
const workspaceContent = [
268-
`import { defineWorkspace } from 'vitest/config'`,
269-
'',
270-
'export default defineWorkspace([',
271-
' // If you want to keep running your existing tests in Node.js, uncomment the next line.',
272-
` // '${relativeRoot}',`,
273-
` {`,
274-
` extends: '${relativeRoot}',`,
275-
` test: {`,
276-
` browser: {`,
277-
` enabled: true,`,
278-
` provider: '${options.provider}',`,
279-
options.provider !== 'preview' && ` // ${getProviderDocsLink(options.provider)}`,
280-
` instances: [`,
281-
...options.browsers.map(browser => ` { browser: '${browser}' },`),
282-
` ],`,
283-
` },`,
284-
` },`,
285-
` },`,
286-
`])`,
287-
'',
288-
].filter(c => typeof c === 'string').join('\n')
289-
await writeFile(options.configPath, workspaceContent)
290-
}
291-
292260
async function generateFrameworkConfigFile(options: {
293261
configPath: string
294262
framework: string
@@ -318,7 +286,6 @@ async function generateFrameworkConfigFile(options: {
318286
`})`,
319287
'',
320288
].filter(t => typeof t === 'string').join('\n')
321-
// this file is only generated if there is already NO root config which is an edge case
322289
await writeFile(options.configPath, configContent)
323290
}
324291

@@ -403,7 +370,6 @@ export async function create(): Promise<void> {
403370
return fail()
404371
}
405372

406-
// TODO: allow multiselect
407373
const { browsers } = await prompt({
408374
type: 'multiselect',
409375
name: 'browsers',
@@ -474,19 +440,24 @@ export async function create(): Promise<void> {
474440
log()
475441

476442
if (rootConfig) {
477-
let browserWorkspaceFile = resolve(dirname(rootConfig), `vitest.workspace.${lang}`)
478-
if (existsSync(browserWorkspaceFile)) {
479-
log(c.yellow('⚠'), c.yellow('A workspace file already exists. Creating a new one for the browser tests - you can merge them manually if needed.'))
480-
browserWorkspaceFile = resolve(process.cwd(), `vitest.workspace.browser.${lang}`)
481-
}
482-
scriptCommand = `vitest --workspace=${relative(process.cwd(), browserWorkspaceFile)}`
483-
await generateWorkspaceFile({
484-
configPath: browserWorkspaceFile,
485-
rootConfig,
443+
const configPath = resolve(dirname(rootConfig), `vitest.browser.config.${lang}`)
444+
scriptCommand = `vitest --config=${relative(process.cwd(), configPath)}`
445+
await generateFrameworkConfigFile({
446+
configPath,
447+
framework,
448+
frameworkPlugin,
486449
provider,
487450
browsers,
488451
})
489-
log(c.green('✔'), 'Created a workspace file for browser tests:', c.bold(relative(process.cwd(), browserWorkspaceFile)))
452+
log(
453+
c.green('✔'),
454+
'Created a new config file for browser tests:',
455+
c.bold(relative(process.cwd(), configPath)),
456+
// TODO: Can we modify the config ourselves?
457+
'\nSince you already have a Vitest config file, it is recommended to copy the contents of the new file ',
458+
'into your existing config located at ',
459+
c.bold(relative(process.cwd(), rootConfig)),
460+
)
490461
}
491462
else {
492463
const configPath = resolve(process.cwd(), `vitest.config.${lang}`)
@@ -497,7 +468,7 @@ export async function create(): Promise<void> {
497468
provider,
498469
browsers,
499470
})
500-
log(c.green('✔'), 'Created a config file for browser tests', c.bold(relative(process.cwd(), configPath)))
471+
log(c.green('✔'), 'Created a config file for browser tests:', c.bold(relative(process.cwd(), configPath)))
501472
}
502473

503474
log()

test/cli/fixtures/browser-init/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"scripts": {
3-
"test:browser": "vitest --workspace=vitest.workspace.ts"
3+
"test:browser": "vitest --config=vitest.browser.config.ts"
44
},
55
"dependencies": {
66
"vitest": "latest"

test/cli/test/init.test.ts

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,29 +47,24 @@ test('initializes project', async () => {
4747
[
4848
"package.json",
4949
"vitest-example",
50+
"vitest.browser.config.ts",
5051
"vitest.config.ts",
51-
"vitest.workspace.ts",
5252
]
5353
`)
5454

55-
expect(await getFileContent('/vitest.workspace.ts')).toMatchInlineSnapshot(`
56-
"import { defineWorkspace } from 'vitest/config'
57-
58-
export default defineWorkspace([
59-
// If you want to keep running your existing tests in Node.js, uncomment the next line.
60-
// 'vitest.config.ts',
61-
{
62-
extends: 'vitest.config.ts',
63-
test: {
64-
browser: {
65-
enabled: true,
66-
provider: 'preview',
67-
instances: [
68-
],
69-
},
55+
expect(await getFileContent('/vitest.browser.config.ts')).toMatchInlineSnapshot(`
56+
"import { defineConfig } from 'vitest/config'
57+
58+
export default defineConfig({
59+
test: {
60+
browser: {
61+
enabled: true,
62+
provider: 'preview',
63+
instances: [
64+
],
7065
},
7166
},
72-
])
67+
})
7368
"
7469
`)
7570

0 commit comments

Comments
 (0)