Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(init): invalid browser config #7475

Merged
merged 1 commit into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why can't you just have the cleanup content in beforeEach?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand - could you provide example?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see now you are calling the cleanup twice 🤔

Why do you need to call it twice though?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well it should be enough to call it just as afterEach. But while I was developing this test case I had to hit CTRL+c couple of times and it always left the files on fs in that immediate termination.


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