-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
AriPerkkio
merged 1 commit into
vitest-dev:main
from
AriPerkkio:fix/init-browser-config
Feb 13, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 inbeforeEach
?There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 hitCTRL+c
couple of times and it always left the files on fs in that immediate termination.