Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion packages/browser/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const RPC_ID: string
const METHOD = getBrowserState().method
export const ENTRY_URL: string = `${
location.protocol === 'https:' ? 'wss:' : 'ws:'
}//${HOST}/__vitest_browser_api__?type=${PAGE_TYPE}&rpcId=${RPC_ID}&sessionId=${getBrowserState().sessionId}&projectName=${getBrowserState().config.name || ''}&method=${METHOD}&token=${(window as any).VITEST_API_TOKEN || '0'}`
}//${HOST}/__vitest_browser_api__?type=${PAGE_TYPE}&rpcId=${RPC_ID}&sessionId=${getBrowserState().sessionId}&projectName=${encodeURIComponent(getBrowserState().config.name || '')}&method=${METHOD}&token=${(window as any).VITEST_API_TOKEN || '0'}`

const onCancelCallbacks: ((reason: CancelReason) => void)[] = []

Expand Down
50 changes: 50 additions & 0 deletions test/browser/specs/client-url.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is not a good test because it mocks everything. Tests in browser run in Node.js and spawn Vitest process that runs the browser. You can look at other examples. Just add a new test with a project name that would fail previously and test that basic test ran correctly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've changed it to an integration test. Is this similar to what you expected?


describe('Browser Client URL Encoding', () => {
beforeEach(() => {
vi.resetModules()

vi.stubGlobal('window', {
__vitest_browser_runner__: {
type: 'tester',
sessionId: 'session-123',
testerId: 'tester-456',
method: 'run',
config: {
name: 'Components & Hooks',
},
},
VITEST_API_TOKEN: 'token-789',
})

vi.stubGlobal('location', {
protocol: 'http:',
hostname: 'localhost',
port: '5173',
})

vi.stubGlobal('WebSocket', class MockWebSocket {
constructor(public url: string) {}
addEventListener() {}
send() {}
close() {}
})
})

afterEach(() => {
vi.unstubAllGlobals()
})

it('should encode project name with ampersand in ENTRY_URL', async () => {
const { ENTRY_URL } = await import('../../../packages/browser/src/client/client')

const url = new URL(ENTRY_URL)

expect(ENTRY_URL).toContain('projectName=Components%20%26%20Hooks')

expect(url.searchParams.get('projectName')).toBe('Components & Hooks')

expect(url.searchParams.get('type')).toBe('tester')
expect(url.searchParams.get('sessionId')).toBe('session-123')
})
})
Loading