@@ -33,7 +33,11 @@ test('Should expose a websocket', async (t) => {
33
33
await fastify . listen ( { port : 0 } )
34
34
35
35
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
+ } )
37
41
38
42
const chunkPromise = once ( ws , 'message' )
39
43
await once ( ws , 'open' )
@@ -94,12 +98,18 @@ test('Should run custom errorHandler on wildcard route handler error', async (t)
94
98
await fastify . listen ( { port : 0 } )
95
99
96
100
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
+ } )
98
106
99
107
await p
100
108
} )
101
109
102
110
test ( 'Should run custom errorHandler on error inside websocket handler' , async ( t ) => {
111
+ t . plan ( 1 )
112
+
103
113
const fastify = Fastify ( )
104
114
t . teardown ( ( ) => fastify . close ( ) )
105
115
@@ -125,12 +135,19 @@ test('Should run custom errorHandler on error inside websocket handler', async (
125
135
126
136
await fastify . listen ( { port : 0 } )
127
137
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
+ } )
129
144
130
145
await p
131
146
} )
132
147
133
148
test ( 'Should run custom errorHandler on error inside async websocket handler' , async ( t ) => {
149
+ t . plan ( 1 )
150
+
134
151
const fastify = Fastify ( )
135
152
t . teardown ( ( ) => fastify . close ( ) )
136
153
@@ -156,7 +173,11 @@ test('Should run custom errorHandler on error inside async websocket handler', a
156
173
157
174
await fastify . listen ( { port : 0 } )
158
175
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
+ } )
160
181
161
182
await p
162
183
} )
@@ -188,7 +209,11 @@ test('Should be able to pass custom options to ws', async (t) => {
188
209
const ws = new WebSocket ( 'ws://localhost:' + fastify . server . address ( ) . port , clientOptions )
189
210
const chunkPromise = once ( ws , 'message' )
190
211
await once ( ws , 'open' )
191
- t . teardown ( ( ) => ws . close ( ) )
212
+ t . teardown ( ( ) => {
213
+ if ( ws . readyState ) {
214
+ ws . close ( )
215
+ }
216
+ } )
192
217
193
218
ws . send ( 'hello' )
194
219
@@ -228,7 +253,11 @@ test('Should warn if path option is provided to ws', async (t) => {
228
253
const ws = new WebSocket ( 'ws://localhost:' + fastify . server . address ( ) . port , clientOptions )
229
254
const chunkPromise = once ( ws , 'message' )
230
255
await once ( ws , 'open' )
231
- t . teardown ( ( ) => ws . close ( ) )
256
+ t . teardown ( ( ) => {
257
+ if ( ws . readyState ) {
258
+ ws . close ( )
259
+ }
260
+ } )
232
261
233
262
ws . send ( 'hello' )
234
263
@@ -269,7 +298,11 @@ test('Should be able to pass a custom server option to ws', async (t) => {
269
298
const ws = new WebSocket ( 'ws://localhost:' + externalServerPort )
270
299
const chunkPromise = once ( ws , 'message' )
271
300
await once ( ws , 'open' )
272
- t . teardown ( ( ) => ws . close ( ) )
301
+ t . teardown ( ( ) => {
302
+ if ( ws . readyState ) {
303
+ ws . close ( )
304
+ }
305
+ } )
273
306
274
307
ws . send ( 'hello' )
275
308
@@ -330,7 +363,11 @@ test('Should be able to pass preClose option to override default', async (t) =>
330
363
await fastify . listen ( { port : 0 } )
331
364
332
365
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
+ } )
334
371
335
372
const chunkPromise = once ( ws , 'message' )
336
373
await once ( ws , 'open' )
0 commit comments