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(browser): increment browser port automatically if there are several projects with browser.enabled #6717

Merged
merged 3 commits into from
Oct 16, 2024
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ docs/.vitepress/cache/
!test/cli/fixtures/dotted-files/**/.cache
test/**/__screenshots__/**/*
test/browser/fixtures/update-snapshot/basic.test.ts
test/cli/fixtures/browser-multiple/basic-*
.vitest-reports
7 changes: 4 additions & 3 deletions packages/browser/src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import type { WorkspaceProject } from 'vitest/node'
import { getFilePoolName, resolveApiServerConfig, resolveFsAllow, distDir as vitestDist } from 'vitest/node'
import { type Plugin, coverageConfigDefaults } from 'vitest/config'
import { toArray } from '@vitest/utils'
import { defaultBrowserPort } from 'vitest/config'
import { dynamicImportPlugin } from '@vitest/mocker/node'
import MagicString from 'magic-string'
import BrowserContext from './plugins/pluginContext'
Expand Down Expand Up @@ -329,14 +328,16 @@ export default (browserServer: BrowserServer, base = '/'): Plugin[] => {
viteConfig.esbuild.legalComments = 'inline'
}

const defaultPort = project.ctx._browserLastPort++

const api = resolveApiServerConfig(
viteConfig.test?.browser || {},
defaultBrowserPort,
defaultPort,
)

viteConfig.server = {
...viteConfig.server,
port: defaultBrowserPort,
port: defaultPort,
...api,
middlewareMode: false,
open: false,
Expand Down
6 changes: 5 additions & 1 deletion packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { noop, slash, toArray } from '@vitest/utils'
import { getTasks, hasFailed } from '@vitest/runner/utils'
import { version } from '../../package.json' with { type: 'json' }
import { getCoverageProvider } from '../integrations/coverage'
import { workspacesFiles as workspaceFiles } from '../constants'
import { defaultBrowserPort, workspacesFiles as workspaceFiles } from '../constants'
import { WebSocketReporter } from '../api/setup'
import type { SerializedCoverageConfig } from '../runtime/config'
import type { ArgumentsType, OnServerRestartHandler, ProvidedContext, UserConsoleLog } from '../types/general'
Expand Down Expand Up @@ -87,6 +87,9 @@ export class Vitest {
/** @deprecated use `_cachedSpecs` */
projectTestFiles = this._cachedSpecs

/** @private */
public _browserLastPort = defaultBrowserPort

constructor(
public readonly mode: VitestRunMode,
options: VitestOptions = {},
Expand All @@ -104,6 +107,7 @@ export class Vitest {
this.unregisterWatcher?.()
clearTimeout(this._rerunTimer)
this.restartsCount += 1
this._browserLastPort = defaultBrowserPort
this.pool?.close?.()
this.pool = undefined
this.coverageProvider = undefined
Expand Down
3 changes: 3 additions & 0 deletions test/cli/fixtures/browser-multiple/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "browser-multiple"
}
31 changes: 31 additions & 0 deletions test/cli/fixtures/browser-multiple/vitest.workspace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { resolve } from 'pathe';
import { defineWorkspace } from 'vitest/config';

export default defineWorkspace([
{
cacheDir: resolve(import.meta.dirname, 'basic-1'),
test: {
name: 'basic-1',
include: ['none'],
browser: {
enabled: true,
name: 'chromium',
provider: 'playwright',
headless: true,
}
}
},
{
cacheDir: resolve(import.meta.dirname, 'basic-2'),
test: {
name: 'basic-2',
include: ['none'],
browser: {
enabled: true,
name: 'chromium',
provider: 'playwright',
headless: true,
}
}
},
])
22 changes: 22 additions & 0 deletions test/cli/test/browser-multiple.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { resolve } from 'pathe'
import { expect, it, onTestFinished, vi } from 'vitest'

import { runVitest } from '../../test-utils'

it('automatically assigns the port', async () => {
const root = resolve(import.meta.dirname, '../fixtures/browser-multiple')
const workspace = resolve(import.meta.dirname, '../fixtures/browser-multiple/vitest.workspace.ts')
const spy = vi.spyOn(console, 'log')
onTestFinished(() => spy.mockRestore())
const { stderr, stdout } = await runVitest({
root,
workspace,
dir: root,
watch: false,
})

expect(spy).not.toHaveBeenCalled()
expect(stderr).not.toContain('is in use, trying another one...')
expect(stdout).toContain('Browser runner started by playwright at http://localhost:63315/')
expect(stdout).toContain('Browser runner started by playwright at http://localhost:63316/')
})
7 changes: 7 additions & 0 deletions test/cli/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@ export default defineConfig({
truncateThreshold: 999,
},
},
server: {
watch: {
ignored: [
'**/fixtures/browser-multiple/**/*',
],
},
},
})
4 changes: 2 additions & 2 deletions test/test-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Readable, Writable } from 'node:stream'
import fs from 'node:fs'
import { fileURLToPath } from 'node:url'
import type { UserConfig as ViteUserConfig } from 'vite'
import { type UserConfig, type VitestRunMode, type WorkerGlobalState, afterEach, onTestFinished } from 'vitest'
import type { Vitest } from 'vitest/node'
import { type WorkerGlobalState, afterEach, onTestFinished } from 'vitest'
import type { UserConfig, Vitest, VitestRunMode } from 'vitest/node'
import { startVitest } from 'vitest/node'
import type { Options } from 'tinyexec'
import { x } from 'tinyexec'
Expand Down