@@ -244,7 +244,7 @@ describe('game-socket', () => {
244
244
expect ( mockSocket . writeSpy ) . toHaveBeenNthCalledWith ( 3 , `\n\n` ) ;
245
245
} ) ;
246
246
247
- it ( 'throws error if socket is destroyed during connect' , async ( ) => {
247
+ it ( 'throws error if socket times out during connect' , async ( ) => {
248
248
const socket = new GameSocketImpl ( { credentials } ) ;
249
249
250
250
// ---
@@ -267,6 +267,38 @@ describe('game-socket', () => {
267
267
expect ( error ) . toEqual ( new Error ( '[GAME:SOCKET:CONNECT:TIMEOUT] 5000' ) ) ;
268
268
}
269
269
} ) ;
270
+
271
+ it ( 'throws error if socket is destroyed during connect' , async ( ) => {
272
+ const socket = new GameSocketImpl ( { credentials } ) ;
273
+
274
+ // ---
275
+
276
+ try {
277
+ // Connect to socket and begin listening for data.
278
+ const socketDataPromise = socket . connect ( ) ;
279
+
280
+ // Before the connect logic has a chance to know if the
281
+ // socket has connected, issue a disconnect to flag it as destroyed.
282
+ await socket . disconnect ( ) ;
283
+
284
+ // Run timer so that the "wait until" logic runs.
285
+ // Note, we don't run them async because otherwise vitest
286
+ // treats the error as an unhandled promise rejection
287
+ // when instead we want to actually catch it in this test.
288
+ vi . runAllTimers ( ) ;
289
+
290
+ // Now await the connect promise, which will reject due to destroyed.
291
+ await socketDataPromise ;
292
+
293
+ expect . unreachable ( 'it should throw an error' ) ;
294
+ } catch ( error ) {
295
+ expect ( error ) . toEqual (
296
+ new Error (
297
+ '[GAME:SOCKET:STATUS:DESTROYED] failed to connect to game server'
298
+ )
299
+ ) ;
300
+ }
301
+ } ) ;
270
302
} ) ;
271
303
272
304
describe ( '#disconnect' , ( ) => {
0 commit comments