Skip to content

Commit

Permalink
fix(init): invalid browser config (#7475)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio authored Feb 13, 2025
1 parent 0e563ea commit 8fe641b
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/vitest/src/create/browser/creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ async function generateWorkspaceFile(options: {
` enabled: true,`,
` provider: '${options.provider}',`,
options.provider !== 'preview' && ` // ${getProviderDocsLink(options.provider)}`,
` configs: [`,
` instances: [`,
...options.browsers.map(browser => ` { browser: '${browser}' },`),
` ],`,
` },`,
Expand Down Expand Up @@ -300,7 +300,7 @@ async function generateFrameworkConfigFile(options: {
` enabled: true,`,
` provider: '${options.provider}',`,
options.provider !== 'preview' && ` // ${getProviderDocsLink(options.provider)}`,
` configs: [`,
` instances: [`,
...options.browsers.map(browser => ` { browser: '${browser}' },`),
` ],`,
` },`,
Expand Down
11 changes: 11 additions & 0 deletions test/cli/fixtures/browser-init/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"scripts": {
"test:browser": "vitest --workspace=vitest.workspace.ts"
},
"dependencies": {
"vitest": "latest"
},
"devDependencies": {
"@vitest/browser": "latest"
}
}
1 change: 1 addition & 0 deletions test/cli/fixtures/browser-init/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
90 changes: 90 additions & 0 deletions test/cli/test/init.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { readdir, readFile, rm, writeFile } from 'node:fs/promises'
import { join } from 'node:path'
import { beforeEach, expect, test } from 'vitest'
import { runVitestCli } from '../../test-utils'

const ARROW_DOWN = '\u001B[B'
const ENTER = '\n'

const cwd = 'fixtures/browser-init'

beforeEach(async () => {
await cleanup()
return cleanup

async function cleanup() {
for (const file of await getFiles()) {
if (file !== 'package.json') {
await rm(`${cwd}/${file}`, { recursive: true })
}
}
await writeFile(`${cwd}/vitest.config.ts`, '{}', 'utf8')
}
})

test('initializes project', async () => {
const { vitest } = await runVitestCli({ nodeOptions: { cwd } }, 'init', 'browser')

await vitest.waitForStdout('This utility will help you set up a browser testing environment.')
await vitest.waitForStdout('? Choose a language for your tests')
vitest.write(ENTER)

await vitest.waitForStdout('Choose a browser provider')
vitest.write(`${ARROW_DOWN}${ARROW_DOWN}${ENTER}`)

await vitest.waitForStdout('Choose a browser')
vitest.write(ENTER)

await vitest.waitForStdout('Choose your framework')
vitest.write(ENTER)

await vitest.waitForStdout('✔ All packages are already installed.')
await vitest.waitForStdout('✔ Added "test:browser" script to your package.json.')
await vitest.waitForStdout(`✔ Created example test file in ${join('vitest-example', 'HelloWorld.test.ts')}`)
await vitest.waitForStdout('All done! Run your tests with pnpm test:browser')

expect(await getFiles()).toMatchInlineSnapshot(`
[
"package.json",
"vitest-example",
"vitest.config.ts",
"vitest.workspace.ts",
]
`)

expect(await getFileContent('/vitest.workspace.ts')).toMatchInlineSnapshot(`
"import { defineWorkspace } from 'vitest/config'
export default defineWorkspace([
// If you want to keep running your existing tests in Node.js, uncomment the next line.
// 'vitest.config.ts',
{
extends: 'vitest.config.ts',
test: {
browser: {
enabled: true,
provider: 'preview',
instances: [
],
},
},
},
])
"
`)

expect(await getFiles('/vitest-example')).toMatchInlineSnapshot(`
[
"HelloWorld.test.ts",
"HelloWorld.ts",
]
`)
})

async function getFiles(subDir = '') {
return await readdir(cwd + subDir)
}

async function getFileContent(subDir = '') {
return await readFile(cwd + subDir, 'utf8')
}
1 change: 1 addition & 0 deletions test/cli/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default defineConfig({
watch: {
ignored: [
'**/fixtures/browser-multiple/**/*',
'**/fixtures/browser-init/**/*',
],
},
},
Expand Down
4 changes: 4 additions & 0 deletions test/test-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ export async function runCli(command: string, _options?: CliOptions | string, ..
return output()
}

if (args[0] === 'init') {
return output()
}

if (args[0] !== 'list' && args.includes('--watch')) {
if (command === 'vitest') {
// Wait for initial test run to complete
Expand Down

0 comments on commit 8fe641b

Please sign in to comment.