Skip to content

Commit 8c10677

Browse files
committed
test(vite): configure the hub before the https server listens
1 parent e324dd3 commit 8c10677

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

‎packages/vite/test/hub.test.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { DevframeHubContext } from '@devframes/hub/node'
12
import type { IncomingMessage, ServerResponse } from 'node:http'
23
import type { Http2SecureServer } from 'node:http2'
34
import type { Socket } from 'node:net'
@@ -8,7 +9,8 @@ import { createSecureServer } from 'node:http2'
89
import { request } from 'node:https'
910
import { tmpdir } from 'node:os'
1011
import { join } from 'node:path'
11-
import { afterEach, beforeAll, describe, expect, it } from 'vitest'
12+
import { getInternalContext } from 'devframe/node/hub-internals'
13+
import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest'
1214
import { WebSocket } from 'ws'
1315
import { viteDevframeHub } from '../src/hub'
1416

@@ -120,14 +122,16 @@ describe.skipIf(!opensslAvailable)('viteDevframeHub', () => {
120122
it('shares an https (http2) dev server for the WebSocket upgrade', async () => {
121123
const host = '127.0.0.1'
122124
const { server, httpServer, close } = fakeHttpsViteServer(tls)
123-
await new Promise<void>(resolve => httpServer.listen(0, host, resolve))
124-
const { port } = httpServer.address() as { port: number }
125+
let context: DevframeHubContext | undefined
125126

126127
const plugin = viteDevframeHub({
127128
ui: false,
128129
auth: false,
129130
quiet: true,
130131
cwd: mkdtempSync(join(tmpdir(), 'devframe-vite-hub-')),
132+
configure: (ctx) => {
133+
context = ctx
134+
},
131135
})
132136
let ws: WebSocket | undefined
133137
cleanup = async () => {
@@ -136,12 +140,18 @@ describe.skipIf(!opensslAvailable)('viteDevframeHub', () => {
136140
await close()
137141
}
138142
await (plugin.configureServer as (s: ViteDevServer) => Promise<void>)(server as any)
143+
await vi.waitFor(() => expect(httpServer.listenerCount('upgrade')).toBeGreaterThan(0))
144+
await new Promise<void>(resolve => httpServer.listen(0, host, resolve))
145+
const { port } = httpServer.address() as { port: number }
139146

140147
const res = await getInsecure(`https://${host}:${port}/__devframes/__connection.json`)
141148
expect(res.status).toBe(200)
142149
expect((JSON.parse(res.body) as { websocket?: unknown }).websocket).toEqual({ path: '/__devframes/__ws' })
143150

144151
ws = await openWs(`wss://${host}:${port}/__devframes/__ws`)
145152
expect(ws.readyState).toBe(WebSocket.OPEN)
153+
154+
expect(context).toBeDefined()
155+
expect(getInternalContext(context!).wsEndpoint).toEqual({ url: `wss://localhost:${port}/__devframes/__ws` })
146156
})
147157
})

0 commit comments

Comments
 (0)