-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix(browser): correctly inherit CLI options #7858
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
Merged
sheremet-va
merged 12 commits into
vitest-dev:main
from
sheremet-va:fix/inherit-browser-cli-options
Apr 29, 2025
Merged
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9aefb50
fix(browser): correctly inherit CLI options
sheremet-va 8db97d5
chore: add crypto
sheremet-va 3a0d108
chore: pass down CI flag
sheremet-va 31d390b
chore: try
sheremet-va f207719
fix: throw and error if name and instances are not set
sheremet-va d89ad56
chore: fix error
sheremet-va 82d14e3
test: refactor
sheremet-va 8a34a94
test: check config before without passing down CLI flags
sheremet-va 16e2375
fix: throw an error if --browser is provided, but not configured
sheremet-va 0d42aa5
fix: don't panic if orchestrator is not set yet
sheremet-va 3c8bf3d
chore: typo
sheremet-va dd92a95
Merge branch 'main' into fix/inherit-browser-cli-options
sheremet-va 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,9 @@ | ||
| import { defineConfig } from 'vitest/config'; | ||
|
|
||
| export default defineConfig({ | ||
| test: { | ||
| browser: { | ||
| enabled: false, | ||
| }, | ||
| }, | ||
| }) |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,7 +1,10 @@ | ||
| import type { ViteUserConfig } from 'vitest/config' | ||
| import type { UserConfig, VitestOptions } from 'vitest/node' | ||
| import crypto from 'node:crypto' | ||
| import { resolve } from 'pathe' | ||
| import { expect, onTestFinished, test } from 'vitest' | ||
| import { createVitest } from 'vitest/node' | ||
| import { runVitestCli, useFS } from '../../test-utils' | ||
|
|
||
| async function vitest(cliOptions: UserConfig, configValue: UserConfig = {}, viteConfig: ViteUserConfig = {}, vitestOptions: VitestOptions = {}) { | ||
| const vitest = await createVitest('test', { ...cliOptions, watch: false }, { ...viteConfig, test: configValue as any }, vitestOptions) | ||
|
|
@@ -303,3 +306,143 @@ test('can enable browser-cli options for multi-project workspace', async () => { | |
| expect(projects[1].config.browser.enabled).toBe(true) | ||
| expect(projects[1].config.browser.headless).toBe(true) | ||
| }) | ||
|
|
||
| function getCliConfig(options: UserConfig, cli: string[], fs: Record<string, string> = {}) { | ||
| const root = resolve(process.cwd(), `vitest-test-${crypto.randomUUID()}`) | ||
| useFS(root, { | ||
| ...fs, | ||
| 'basic.test.ts': /* ts */` | ||
| import { test } from 'vitest' | ||
| test('basic', () => { | ||
| expect(1).toBe(1) | ||
| }) | ||
| `, | ||
| 'vitest.config.ts': /* ts */ ` | ||
| export default { | ||
| test: { | ||
| reporters: [ | ||
| { | ||
| onInit(vitest) { | ||
| const browser = vitest.config.browser | ||
| console.log(JSON.stringify({ | ||
| browser: { | ||
| headless: browser.headless, | ||
| browser: browser.enabled, | ||
| ui: browser.ui, | ||
| }, | ||
| workspace: vitest.projects.map(p => { | ||
| return { | ||
| name: p.name, | ||
| headless: p.config.browser.headless, | ||
| browser: p.config.browser.enabled, | ||
| ui: p.config.browser.ui, | ||
| } | ||
| }) | ||
| })) | ||
| // throw an error to avoid running tests | ||
| throw new Error('stop') | ||
|
Comment on lines
+347
to
+348
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This utility looks great - I've thought about building something like this before. Sometimes it's not enough to check resolved options just via |
||
| }, | ||
| }, | ||
| ], | ||
| ...${JSON.stringify(options)} | ||
| } | ||
| } | ||
| `, | ||
| }) | ||
| return runVitestCli( | ||
| { | ||
| nodeOptions: { | ||
| env: { | ||
| CI: 'false', | ||
| GITHUB_ACTIONS: undefined, | ||
| }, | ||
| }, | ||
| }, | ||
| '--root', | ||
| root, | ||
| '--no-watch', | ||
| ...cli, | ||
| ) | ||
| } | ||
|
|
||
| test('[e2e] CLI options correctly override inline workspace options', async () => { | ||
| const vitest = await getCliConfig({ | ||
| workspace: [ | ||
| { | ||
| test: { | ||
| name: 'unit', | ||
| }, | ||
| }, | ||
| { | ||
| test: { | ||
| name: 'browser', | ||
| browser: { | ||
| enabled: true, | ||
| headless: true, | ||
| instances: [ | ||
| { | ||
| browser: 'chromium', | ||
| }, | ||
| ], | ||
| }, | ||
sheremet-va marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }, | ||
| }, | ||
| ], | ||
| }, ['--browser.headless=false']) | ||
|
|
||
| const config = JSON.parse(vitest.stdout) | ||
|
|
||
| expect(config.workspace).toHaveLength(2) | ||
| expect(config.workspace[0].name).toBe('unit') | ||
| expect(config.workspace[0].browser).toBe(false) | ||
|
|
||
| expect(config.workspace[1].name).toBe('browser (chromium)') | ||
| expect(config.workspace[1].headless).toBe(false) | ||
| expect(config.workspace[1].browser).toBe(true) | ||
| expect(config.workspace[1].ui).toBe(true) | ||
| }) | ||
|
|
||
| test('[e2e] CLI options correctly override file workspace options', async () => { | ||
| const vitest = await getCliConfig( | ||
| { | ||
| workspace: [ | ||
| { | ||
| test: { | ||
| name: 'unit', | ||
| }, | ||
| }, | ||
| './vitest.browser.config.ts', | ||
| ], | ||
| }, | ||
| ['--browser.headless=false'], | ||
| { | ||
| 'vitest.browser.config.ts': /* ts */ ` | ||
| export default { | ||
| test: { | ||
| name: 'browser', | ||
| browser: { | ||
| enabled: true, | ||
| headless: true, | ||
| instances: [ | ||
| { | ||
| browser: 'chromium', | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| } | ||
| `, | ||
| }, | ||
| ) | ||
|
|
||
| const config = JSON.parse(vitest.stdout) | ||
|
|
||
| expect(config.workspace).toHaveLength(2) | ||
| expect(config.workspace[0].name).toBe('unit') | ||
| expect(config.workspace[0].browser).toBe(false) | ||
|
|
||
| expect(config.workspace[1].name).toBe('browser (chromium)') | ||
| expect(config.workspace[1].headless).toBe(false) | ||
| expect(config.workspace[1].browser).toBe(true) | ||
| expect(config.workspace[1].ui).toBe(true) | ||
| }) | ||
Oops, something went wrong.
Oops, something went wrong.
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.
The main change is that we ignore all browser CLI flags if workspace doesn't have the
browserconfiguredThere 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.
Note: we throw an error if
--browseris provided, but there are no configurations for it