@@ -33,7 +33,11 @@ test('Should expose a websocket', async (t) => {
3333 await fastify . listen ( { port : 0 } )
3434
3535 const ws = new WebSocket ( 'ws://localhost:' + fastify . server . address ( ) . port )
36- t . teardown ( ( ) => ws . close ( ) )
36+ t . teardown ( ( ) => {
37+ if ( ws . readyState ) {
38+ ws . close ( )
39+ }
40+ } )
3741
3842 const chunkPromise = once ( ws , 'message' )
3943 await once ( ws , 'open' )
@@ -94,12 +98,18 @@ test('Should run custom errorHandler on wildcard route handler error', async (t)
9498 await fastify . listen ( { port : 0 } )
9599
96100 const ws = new WebSocket ( 'ws://localhost:' + fastify . server . address ( ) . port )
97- t . teardown ( ( ) => ws . close ( ) )
101+ t . teardown ( ( ) => {
102+ if ( ws . readyState ) {
103+ ws . close ( )
104+ }
105+ } )
98106
99107 await p
100108} )
101109
102110test ( 'Should run custom errorHandler on error inside websocket handler' , async ( t ) => {
111+ t . plan ( 1 )
112+
103113 const fastify = Fastify ( )
104114 t . teardown ( ( ) => fastify . close ( ) )
105115
@@ -125,12 +135,19 @@ test('Should run custom errorHandler on error inside websocket handler', async (
125135
126136 await fastify . listen ( { port : 0 } )
127137 const ws = new WebSocket ( 'ws://localhost:' + fastify . server . address ( ) . port )
128- t . teardown ( ( ) => ws . close ( ) )
138+
139+ t . teardown ( ( ) => {
140+ if ( ws . readyState ) {
141+ ws . close ( )
142+ }
143+ } )
129144
130145 await p
131146} )
132147
133148test ( 'Should run custom errorHandler on error inside async websocket handler' , async ( t ) => {
149+ t . plan ( 1 )
150+
134151 const fastify = Fastify ( )
135152 t . teardown ( ( ) => fastify . close ( ) )
136153
@@ -156,7 +173,11 @@ test('Should run custom errorHandler on error inside async websocket handler', a
156173
157174 await fastify . listen ( { port : 0 } )
158175 const ws = new WebSocket ( 'ws://localhost:' + fastify . server . address ( ) . port )
159- t . teardown ( ( ) => ws . close ( ) )
176+ t . teardown ( ( ) => {
177+ if ( ws . readyState ) {
178+ ws . close ( )
179+ }
180+ } )
160181
161182 await p
162183} )
@@ -188,7 +209,11 @@ test('Should be able to pass custom options to ws', async (t) => {
188209 const ws = new WebSocket ( 'ws://localhost:' + fastify . server . address ( ) . port , clientOptions )
189210 const chunkPromise = once ( ws , 'message' )
190211 await once ( ws , 'open' )
191- t . teardown ( ( ) => ws . close ( ) )
212+ t . teardown ( ( ) => {
213+ if ( ws . readyState ) {
214+ ws . close ( )
215+ }
216+ } )
192217
193218 ws . send ( 'hello' )
194219
@@ -228,7 +253,11 @@ test('Should warn if path option is provided to ws', async (t) => {
228253 const ws = new WebSocket ( 'ws://localhost:' + fastify . server . address ( ) . port , clientOptions )
229254 const chunkPromise = once ( ws , 'message' )
230255 await once ( ws , 'open' )
231- t . teardown ( ( ) => ws . close ( ) )
256+ t . teardown ( ( ) => {
257+ if ( ws . readyState ) {
258+ ws . close ( )
259+ }
260+ } )
232261
233262 ws . send ( 'hello' )
234263
@@ -269,7 +298,11 @@ test('Should be able to pass a custom server option to ws', async (t) => {
269298 const ws = new WebSocket ( 'ws://localhost:' + externalServerPort )
270299 const chunkPromise = once ( ws , 'message' )
271300 await once ( ws , 'open' )
272- t . teardown ( ( ) => ws . close ( ) )
301+ t . teardown ( ( ) => {
302+ if ( ws . readyState ) {
303+ ws . close ( )
304+ }
305+ } )
273306
274307 ws . send ( 'hello' )
275308
@@ -330,7 +363,11 @@ test('Should be able to pass preClose option to override default', async (t) =>
330363 await fastify . listen ( { port : 0 } )
331364
332365 const ws = new WebSocket ( 'ws://localhost:' + fastify . server . address ( ) . port )
333- t . teardown ( ( ) => ws . close ( ) )
366+ t . teardown ( ( ) => {
367+ if ( ws . readyState ) {
368+ ws . close ( )
369+ }
370+ } )
334371
335372 const chunkPromise = once ( ws , 'message' )
336373 await once ( ws , 'open' )
0 commit comments