@@ -15,8 +15,6 @@ import http from 'http';
15
15
import { SubscriptionServer } from 'subscriptions-transport-ws' ;
16
16
17
17
18
- const SHOULD_NOT_GET_HERE_ERROR = 'SHOULD_NOT_GET_HERE' ;
19
-
20
18
describe ( 'Schema URL Loader' , ( ) => {
21
19
const loader = new UrlLoader ( ) ;
22
20
@@ -101,43 +99,44 @@ input TestInput {
101
99
102
100
const testHost = `http://localhost:3000` ;
103
101
const testPath = '/graphql' ;
104
- const testPathChecker = ( path : string ) => {
105
- return path . startsWith ( testPath ) ;
106
- } ;
102
+ const testPathChecker = ( path : string ) => path . startsWith ( testPath ) ;
107
103
const testUrl = `${ testHost } ${ testPath } ` ;
108
104
109
105
describe ( 'handle' , ( ) => {
110
106
107
+ let scope : nock . Scope ;
108
+ afterEach ( ( ) => {
109
+ if ( ! scope ?. isDone ( ) ) {
110
+ scope ?. done ( ) ;
111
+ }
112
+ } )
113
+
111
114
it ( 'Should throw an error when introspection is not valid' , async ( ) => {
112
115
const brokenData = { data : { } } ;
113
- const scope = nock ( testHost ) . post ( testPathChecker ) . reply ( 200 , brokenData ) ;
116
+ scope = nock ( testHost ) . post ( testPathChecker ) . reply ( 200 , brokenData ) ;
117
+
118
+ expect . assertions ( 1 ) ;
114
119
115
120
try {
116
121
await loader . load ( testUrl , { } ) ;
117
- throw new Error ( SHOULD_NOT_GET_HERE_ERROR ) ;
118
- } catch ( e ) {
119
- expect ( e . message ) . not . toBe ( SHOULD_NOT_GET_HERE_ERROR ) ;
120
- expect ( e . message ) . toBe ( 'Could not obtain introspection result, received: ' + JSON . stringify ( brokenData ) ) ;
122
+ } catch ( e ) {
123
+ expect ( e . message ) . toBe ( 'Could not obtain introspection result, received: ' + JSON . stringify ( brokenData ) )
121
124
}
122
-
123
- scope . done ( ) ;
124
125
} ) ;
125
126
126
127
it ( 'Should return a valid schema when request is valid' , async ( ) => {
127
- const server = mockGraphQLServer ( { schema : testSchema , host : testHost , path : testPathChecker } ) ;
128
+ scope = mockGraphQLServer ( { schema : testSchema , host : testHost , path : testPathChecker } ) ;
128
129
129
130
const schema = await loader . load ( testUrl , { } ) ;
130
131
131
- server . done ( ) ;
132
-
133
132
expect ( schema . schema ) . toBeDefined ( ) ;
134
133
expect ( printSchemaWithDirectives ( schema . schema ) ) . toBeSimilarGqlDoc ( testTypeDefs ) ;
135
134
} ) ;
136
135
137
136
it ( 'Should pass default headers' , async ( ) => {
138
137
let headers : Record < string , string | string [ ] > = { } ;
139
138
140
- const server = mockGraphQLServer ( {
139
+ scope = mockGraphQLServer ( {
141
140
schema : testSchema ,
142
141
host : testHost ,
143
142
path : testPathChecker ,
@@ -148,8 +147,6 @@ input TestInput {
148
147
149
148
const schema = await loader . load ( testUrl , { } ) ;
150
149
151
- server . done ( ) ;
152
-
153
150
expect ( schema ) . toBeDefined ( ) ;
154
151
expect ( schema . schema ) . toBeDefined ( ) ;
155
152
expect ( printSchemaWithDirectives ( schema . schema ) ) . toBeSimilarGqlDoc ( testTypeDefs ) ;
@@ -160,7 +157,7 @@ input TestInput {
160
157
161
158
it ( 'Should pass extra headers when they are specified as object' , async ( ) => {
162
159
let headers : Record < string , string | string [ ] > = { } ;
163
- const server = mockGraphQLServer ( {
160
+ scope = mockGraphQLServer ( {
164
161
schema : testSchema ,
165
162
host : testHost ,
166
163
path : testPathChecker ,
@@ -171,8 +168,6 @@ input TestInput {
171
168
172
169
const schema = await loader . load ( testUrl , { headers : { Auth : '1' } } ) ;
173
170
174
- server . done ( ) ;
175
-
176
171
expect ( schema ) . toBeDefined ( ) ;
177
172
expect ( schema . schema ) . toBeDefined ( ) ;
178
173
expect ( printSchemaWithDirectives ( schema . schema ) ) . toBeSimilarGqlDoc ( testTypeDefs ) ;
@@ -184,7 +179,7 @@ input TestInput {
184
179
185
180
it ( 'Should pass extra headers when they are specified as array' , async ( ) => {
186
181
let headers : Record < string , string | string [ ] > = { } ;
187
- const server = mockGraphQLServer ( {
182
+ scope = mockGraphQLServer ( {
188
183
schema : testSchema ,
189
184
host : testHost ,
190
185
path : testPathChecker ,
@@ -194,8 +189,6 @@ input TestInput {
194
189
} ) ;
195
190
const schema = await loader . load ( testUrl , { headers : [ { A : '1' } , { B : '2' , C : '3' } ] } ) ;
196
191
197
- server . done ( ) ;
198
-
199
192
expect ( schema ) . toBeDefined ( ) ;
200
193
expect ( schema . schema ) . toBeDefined ( ) ;
201
194
expect ( printSchemaWithDirectives ( schema . schema ) ) . toBeSimilarGqlDoc ( testTypeDefs ) ;
@@ -208,11 +201,9 @@ input TestInput {
208
201
} ) ;
209
202
210
203
it ( 'Should utilize extra introspection options' , async ( ) => {
211
- const server = mockGraphQLServer ( { schema : testSchema , host : testHost , path : testPathChecker } ) ;
204
+ scope = mockGraphQLServer ( { schema : testSchema , host : testHost , path : testPathChecker } ) ;
212
205
const source = await loader . load ( testUrl , { descriptions : false } ) ;
213
206
214
- server . done ( ) ;
215
-
216
207
expect ( source ) . toBeDefined ( ) ;
217
208
expect ( source . schema . getQueryType ( ) . description ) . toBeUndefined ( ) ;
218
209
} ) ;
@@ -221,18 +212,18 @@ input TestInput {
221
212
expect ( await loader . canLoad ( cwd ( ) , { } ) ) . toBeFalsy ( ) ;
222
213
} ) ;
223
214
it ( 'should handle useGETForQueries correctly' , async ( ) => {
224
- const server = mockGraphQLServer ( { schema : testSchema , host : testHost , path : testPathChecker , method : 'GET' } ) ;
215
+ scope = mockGraphQLServer ( { schema : testSchema , host : testHost , path : testPathChecker , method : 'GET' } ) ;
225
216
226
217
const source = await loader . load ( testUrl , {
227
218
descriptions : false ,
228
219
useGETForQueries : true ,
229
220
} ) ;
230
221
231
- server . done ( ) ;
232
-
233
222
const testVariableValue = 'A' ;
234
223
235
- const server2 = mockGraphQLServer ( { schema : testSchema , host : testHost , path : testPathChecker , method : 'GET' } ) ;
224
+ scope . done ( ) ;
225
+
226
+ scope = mockGraphQLServer ( { schema : testSchema , host : testHost , path : testPathChecker , method : 'GET' } ) ;
236
227
237
228
const result = await execute ( {
238
229
schema : source . schema ,
@@ -246,7 +237,7 @@ input TestInput {
246
237
} ,
247
238
} ) ;
248
239
249
- server2 . done ( ) ;
240
+ scope . done ( ) ;
250
241
251
242
expect ( result ?. errors ) . toBeFalsy ( ) ;
252
243
@@ -259,11 +250,9 @@ input TestInput {
259
250
path : '/graphql' ,
260
251
} ;
261
252
const url = address . host + address . path ;
262
- const server = mockGraphQLServer ( { schema : testSchema , host : address . host , path : address . path } ) ;
253
+ scope = mockGraphQLServer ( { schema : testSchema , host : address . host , path : address . path } ) ;
263
254
const result = await loader . load ( url , { } ) ;
264
255
265
- server . done ( ) ;
266
-
267
256
expect ( result . schema ) . toBeDefined ( ) ;
268
257
expect ( printSchemaWithDirectives ( result . schema ) ) . toBeSimilarGqlDoc ( testTypeDefs ) ;
269
258
} ) ;
@@ -274,15 +263,13 @@ input TestInput {
274
263
path : '/graphql' ,
275
264
} ;
276
265
const url = address . host + address . path ;
277
- const server = mockGraphQLServer ( {
266
+ scope = mockGraphQLServer ( {
278
267
schema : testSchema ,
279
268
host : address . host . replace ( 'ws' , 'http' ) ,
280
269
path : address . path ,
281
270
} ) ;
282
271
const result = await loader . load ( url , { } ) ;
283
272
284
- server . done ( ) ;
285
-
286
273
expect ( result . schema ) . toBeDefined ( ) ;
287
274
expect ( printSchemaWithDirectives ( result . schema ) ) . toBeSimilarGqlDoc ( testTypeDefs ) ;
288
275
} ) ;
@@ -293,26 +280,22 @@ input TestInput {
293
280
path : '/graphql' ,
294
281
} ;
295
282
const url = address . host + address . path ;
296
- const server = mockGraphQLServer ( {
283
+ scope = mockGraphQLServer ( {
297
284
schema : testSchema ,
298
285
host : address . host . replace ( 'wss' , 'https' ) ,
299
286
path : address . path ,
300
287
} ) ;
301
288
const result = await loader . load ( url , { } ) ;
302
289
303
- server . done ( ) ;
304
-
305
290
expect ( result . schema ) . toBeDefined ( ) ;
306
291
expect ( printSchemaWithDirectives ( result . schema ) ) . toBeSimilarGqlDoc ( testTypeDefs ) ;
307
292
} ) ;
308
293
it ( 'should handle .graphql files' , async ( ) => {
309
294
const testHost = 'http://localhost:3000' ;
310
295
const testPath = '/schema.graphql' ;
311
- const server = nock ( testHost ) . get ( testPath ) . reply ( 200 , testTypeDefs ) ;
296
+ scope = nock ( testHost ) . get ( testPath ) . reply ( 200 , testTypeDefs ) ;
312
297
const result = await loader . load ( testHost + testPath , { } ) ;
313
298
314
- server . done ( ) ;
315
-
316
299
expect ( result . schema ) . toBeDefined ( ) ;
317
300
expect ( printSchemaWithDirectives ( result . schema ) ) . toBeSimilarGqlDoc ( testTypeDefs ) ;
318
301
@@ -322,13 +305,11 @@ input TestInput {
322
305
it ( 'should handle results with handleAsSDL option even if it doesn\'t end with .graphql' , async ( ) => {
323
306
const testHost = 'http://localhost:3000' ;
324
307
const testPath = '/sdl' ;
325
- const server = nock ( testHost ) . get ( testPath ) . reply ( 200 , testTypeDefs ) ;
308
+ scope = nock ( testHost ) . get ( testPath ) . reply ( 200 , testTypeDefs ) ;
326
309
const result = await loader . load ( testHost + testPath , {
327
310
handleAsSDL : true ,
328
311
} ) ;
329
312
330
- server . done ( ) ;
331
-
332
313
expect ( result . schema ) . toBeDefined ( ) ;
333
314
expect ( printSchemaWithDirectives ( result . schema ) ) . toBeSimilarGqlDoc ( testTypeDefs ) ;
334
315
@@ -469,15 +450,15 @@ input TestInput {
469
450
httpServer . close ( done ) ;
470
451
} ) ;
471
452
it ( 'should handle multipart requests' , async ( ) => {
472
- let server = mockGraphQLServer ( { schema : testSchema , host : testHost , path : testPathChecker , method : 'POST' } ) ;
453
+ scope = mockGraphQLServer ( { schema : testSchema , host : testHost , path : testPathChecker , method : 'POST' } ) ;
473
454
474
455
const { schema } = await loader . load ( testUrl , {
475
456
multipart : true ,
476
457
} ) ;
477
458
478
- server . done ( ) ;
459
+ scope . done ( ) ;
479
460
480
- server = mockGraphQLServer ( { schema : testSchema , host : testHost , path : testPathChecker , method : 'POST' } )
461
+ scope = mockGraphQLServer ( { schema : testSchema , host : testHost , path : testPathChecker , method : 'POST' } )
481
462
482
463
const fileName = 'testfile.txt' ;
483
464
@@ -500,8 +481,6 @@ input TestInput {
500
481
} ,
501
482
} )
502
483
503
- server . done ( ) ;
504
-
505
484
const content = readFileSync ( absoluteFilePath , 'utf8' )
506
485
507
486
expect ( result . errors ) . toBeFalsy ( ) ;
0 commit comments