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

test: fix flaky browser-multiple.test.ts #6977

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
14 changes: 7 additions & 7 deletions test/cli/test/browser-multiple.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import type { Vitest } from 'vitest/node'
import { resolve } from 'pathe'

import { expect, it, onTestFinished, vi } from 'vitest'
import { expect, it } 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')
Copy link
Member

@sheremet-va sheremet-va Dec 6, 2024

Choose a reason for hiding this comment

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

We don't intercept Vite's logger with stdout, it's logged with console.log

with this change, if the behaviour breaks, this test won't fail

onTestFinished(() => spy.mockRestore())
let ctx: Vitest
let urls: (string | undefined)[] = []
const { stderr } = await runVitest({
const { stdout, stderr } = await runVitest({
root,
workspace,
dir: root,
Expand All @@ -20,6 +18,8 @@ it('automatically assigns the port', async () => {
{
onInit(ctx_) {
ctx = ctx_
// patch _browserLastPort to conflicting default 63315 with other tests
ctx._browserLastPort = 33445
},
onFinished() {
urls = ctx.projects.map(p => p.browser?.vite.resolvedUrls?.local[0])
Expand All @@ -28,8 +28,8 @@ it('automatically assigns the port', async () => {
],
})

expect(spy).not.toHaveBeenCalled()
expect(stdout).not.toContain('is in use, trying another one...')
expect(stderr).not.toContain('is in use, trying another one...')
expect(urls).toContain('http://localhost:63315/')
expect(urls).toContain('http://localhost:63316/')
expect(urls).toContain('http://localhost:33445/')
expect(urls).toContain('http://localhost:33446/')
})