@@ -313,30 +313,26 @@ describe("usePartySocket", () => {
313313 test ( "attaches onOpen event handler" , async ( ) => {
314314 const onOpen = vitest . fn ( ) ;
315315
316- wss . once ( "connection" , ( _ws ) => {
317- // Connection established
316+ // Set up connection handler before rendering
317+ const connectionPromise = new Promise < void > ( ( resolve ) => {
318+ wss . once ( "connection" , ( _ws : any ) => {
319+ // Connection established
320+ resolve ( ) ;
321+ } ) ;
318322 } ) ;
319323
320- // Start with socket closed to ensure handler is attached before connection
321324 const { result } = renderHook ( ( ) =>
322325 usePartySocket ( {
323326 host : `localhost:${ PORT } ` ,
324327 room : "test-room" ,
325- onOpen,
326- startClosed : true
328+ onOpen
327329 } )
328330 ) ;
329331
330- // Verify socket starts closed
331- expect ( result . current . readyState ) . toBe ( WebSocket . CLOSED ) ;
332-
333- // Give useEffect time to attach event handlers
334- await new Promise ( ( resolve ) => setTimeout ( resolve , 50 ) ) ;
332+ // Wait for connection to be established on server side
333+ await connectionPromise ;
335334
336- // Manually trigger reconnection - handler should be attached by now
337- result . current . reconnect ( ) ;
338-
339- // Wait for connection to be established
335+ // Wait for connection to be established on client side
340336 await waitFor (
341337 ( ) => {
342338 expect ( result . current . readyState ) . toBe ( WebSocket . OPEN ) ;
@@ -354,12 +350,13 @@ describe("usePartySocket", () => {
354350 const onMessage = vitest . fn ( ) ;
355351 const testMessage = "hello from server" ;
356352
357- wss . once ( "connection" , ( ws ) => {
358- // Wait for connection to be fully established before sending
353+ const connectionHandler = ( ws : any ) => {
354+ // Send message after a small delay to ensure connection is fully established
359355 setTimeout ( ( ) => {
360356 ws . send ( testMessage ) ;
361- } , 100 ) ;
362- } ) ;
357+ } , 50 ) ;
358+ } ;
359+ wss . on ( "connection" , connectionHandler ) ;
363360
364361 const { result } = renderHook ( ( ) =>
365362 usePartySocket ( {
@@ -369,15 +366,7 @@ describe("usePartySocket", () => {
369366 } )
370367 ) ;
371368
372- // Wait for connection to be established first
373- await waitFor (
374- ( ) => {
375- expect ( result . current . readyState ) . toBe ( WebSocket . OPEN ) ;
376- } ,
377- { timeout : 3000 }
378- ) ;
379-
380- // Then wait for message
369+ // Wait for message to be received
381370 await waitFor (
382371 ( ) => {
383372 expect ( onMessage ) . toHaveBeenCalled ( ) ;
@@ -387,6 +376,7 @@ describe("usePartySocket", () => {
387376 { timeout : 3000 }
388377 ) ;
389378
379+ wss . off ( "connection" , connectionHandler ) ;
390380 result . current . close ( ) ;
391381 } ) ;
392382
@@ -449,11 +439,12 @@ describe("usePartySocket", () => {
449439 const onMessage1 = vitest . fn ( ) ;
450440 const onMessage2 = vitest . fn ( ) ;
451441
452- wss . once ( "connection" , ( ws ) => {
453- // Wait for connection to be fully established before sending messages
442+ const connectionHandler = ( ws : any ) => {
443+ // Send messages with delays to ensure connection is established
454444 setTimeout ( ( ) => ws . send ( "message1" ) , 100 ) ;
455445 setTimeout ( ( ) => ws . send ( "message2" ) , 200 ) ;
456- } ) ;
446+ } ;
447+ wss . on ( "connection" , connectionHandler ) ;
457448
458449 const { result, rerender } = renderHook (
459450 ( { onMessage } ) =>
@@ -467,14 +458,6 @@ describe("usePartySocket", () => {
467458
468459 const firstSocket = result . current ;
469460
470- // Wait for connection to be established first
471- await waitFor (
472- ( ) => {
473- expect ( result . current . readyState ) . toBe ( WebSocket . OPEN ) ;
474- } ,
475- { timeout : 3000 }
476- ) ;
477-
478461 // Wait for first message
479462 await waitFor (
480463 ( ) => {
@@ -497,6 +480,7 @@ describe("usePartySocket", () => {
497480 { timeout : 3000 }
498481 ) ;
499482
483+ wss . off ( "connection" , connectionHandler ) ;
500484 result . current . close ( ) ;
501485 } ) ;
502486
0 commit comments