Skip to content

Commit

Permalink
fix(browser): add instance validation to resolve coverage error (#7231)
Browse files Browse the repository at this point in the history
Co-authored-by: Ari Perkkiö <ari.perkkio@gmail.com>
  • Loading branch information
DevJoaoLopes and AriPerkkio authored Jan 15, 2025
1 parent 0ad2860 commit 1e7915b
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 12 deletions.
2 changes: 1 addition & 1 deletion docs/guide/debugging.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ export default defineConfig({
inspectBrk: true,
fileParallelism: false,
browser: {
name: 'chromium',
provider: 'playwright',
instances: [{ browser: 'chromium' }]
},
},
})
Expand Down
16 changes: 12 additions & 4 deletions packages/vitest/src/node/config/resolveConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,22 @@ export function resolveConfig(
}
}

const playwrightChromiumOnly = browser.provider === 'playwright' && (browser.name === 'chromium' || browser.instances?.every(i => i.browser === 'chromium'))

// Browser-mode "Playwright + Chromium" only features:
if (browser.enabled && !(browser.provider === 'playwright' && browser.name === 'chromium')) {
const browserConfig = { browser: { provider: browser.provider, name: browser.name } }
if (browser.enabled && !playwrightChromiumOnly) {
const browserConfig = {
browser: {
provider: browser.provider,
name: browser.name,
instances: browser.instances,
},
}

if (resolved.coverage.enabled && resolved.coverage.provider === 'v8') {
throw new Error(
`@vitest/coverage-v8 does not work with\n${JSON.stringify(browserConfig, null, 2)}\n`
+ `\nUse either:\n${JSON.stringify({ browser: { provider: 'playwright', name: 'chromium' } }, null, 2)}`
+ `\nUse either:\n${JSON.stringify({ browser: { provider: 'playwright', instances: [{ browser: 'chromium' }] } }, null, 2)}`
+ `\n\n...or change your coverage provider to:\n${JSON.stringify({ coverage: { provider: 'istanbul' } }, null, 2)}\n`,
)
}
Expand All @@ -268,7 +276,7 @@ export function resolveConfig(

throw new Error(
`${inspectOption} does not work with\n${JSON.stringify(browserConfig, null, 2)}\n`
+ `\nUse either:\n${JSON.stringify({ browser: { provider: 'playwright', name: 'chromium' } }, null, 2)}`
+ `\nUse either:\n${JSON.stringify({ browser: { provider: 'playwright', instances: [{ browser: 'chromium' }] } }, null, 2)}`
+ `\n\n...or disable ${inspectOption}\n`,
)
}
Expand Down
4 changes: 2 additions & 2 deletions packages/vitest/src/node/types/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export interface BrowserConfigOptions {

/**
* Name of the browser
* @deprecated use `configs` instead. if both are defined, this will filter `configs` by name.
* @deprecated use `instances` instead. if both are defined, this will filter `instances` by name.
*/
name?: string

Expand All @@ -118,7 +118,7 @@ export interface BrowserConfigOptions {
*
* @example
* { playwright: { launch: { devtools: true } }
* @deprecated use `configs` instead
* @deprecated use `instances` instead
*/
providerOptions?: BrowserProviderOptions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default defineWorkspace([
browser: {
enabled: true,
provider: 'webdriverio',
name: 'chrome',
instances: [{ browser: 'chrome' }]
},
}
}
Expand Down
130 changes: 126 additions & 4 deletions test/config/test/failures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ Use either:
{
"browser": {
"provider": "playwright",
"name": "chromium"
"instances": [
{
"browser": "chromium"
}
]
}
}
Expand All @@ -122,7 +126,7 @@ Use either:
}
})

test('v8 coverage provider throws when not playwright + chromium', async () => {
test('v8 coverage provider throws when not playwright + chromium (browser.name)', async () => {
for (const { provider, name } of browsers) {
if (provider === 'playwright' && name === 'chromium') {
continue
Expand Down Expand Up @@ -152,7 +156,64 @@ Use either:
{
"browser": {
"provider": "playwright",
"name": "chromium"
"instances": [
{
"browser": "chromium"
}
]
}
}
...or change your coverage provider to:
{
"coverage": {
"provider": "istanbul"
}
}
`,
)
}
})

test('v8 coverage provider throws when not playwright + chromium (browser.instances)', async () => {
for (const { provider, name } of browsers) {
if (provider === 'playwright' && name === 'chromium') {
continue
}

const { stderr } = await runVitest({
coverage: {
enabled: true,
},
browser: {
enabled: true,
provider,
instances: [{ browser: name }],
},
})

expect(stderr).toMatch(
`Error: @vitest/coverage-v8 does not work with
{
"browser": {
"provider": "${provider}",
"instances": [
{
"browser": "${name}"
}
]
}
}
Use either:
{
"browser": {
"provider": "playwright",
"instances": [
{
"browser": "chromium"
}
]
}
}
Expand All @@ -167,14 +228,75 @@ Use either:
}
})

test('v8 coverage provider throws when using chromium and other non-chromium browser', async () => {
const { stderr } = await runVitest({
coverage: {
enabled: true,
},
browser: {
enabled: true,
headless: true,
provider: 'playwright',
instances: [
{ browser: 'chromium' },
{ browser: 'firefox' },
{ browser: 'webkit' },
],
},
})

expect(stderr).toMatch(
`Error: @vitest/coverage-v8 does not work with
{
"browser": {
"provider": "playwright",
"instances": [
{
"browser": "chromium"
},
{
"browser": "firefox"
},
{
"browser": "webkit"
}
]
}
}
Use either:
{
"browser": {
"provider": "playwright",
"instances": [
{
"browser": "chromium"
}
]
}
}
...or change your coverage provider to:
{
"coverage": {
"provider": "istanbul"
}
}`,
)
})

test('v8 coverage provider cannot be used in workspace without playwright + chromium', async () => {
const { stderr } = await runVitest({ coverage: { enabled: true }, workspace: './fixtures/workspace/browser/workspace-with-browser.ts' })
expect(stderr).toMatch(
`Error: @vitest/coverage-v8 does not work with
{
"browser": {
"provider": "webdriverio",
"name": "chrome"
"instances": [
{
"browser": "chrome"
}
]
}
}`,
)
Expand Down

0 comments on commit 1e7915b

Please sign in to comment.