1
- import { describe , it , beforeAll , afterAll , expect } from '@jest/globals' ;
1
+ import { afterAll , beforeAll , describe , expect , it } from '@jest/globals' ;
2
2
3
- import fetchMockModule from '../index' ;
4
- const fetchMock = fetchMockModule . default ;
3
+ import fetchMock from '../index' ;
4
+ import { expectTypeOf } from 'expect-type' ;
5
5
6
6
const humanVerbToMethods = [
7
- 'Fetched' ,
8
- 'Got: get' ,
9
- 'Posted: post' ,
10
- 'Put: put' ,
11
- 'Deleted: delete' ,
12
- 'FetchedHead: head' ,
13
- 'Patched: patch' ,
14
- ] ;
7
+ { humanVerb : 'Fetched' , method : 'get' } ,
8
+ { humanVerb : 'Got' , method : ' get' } ,
9
+ { humanVerb : 'Posted' , method : ' post' } ,
10
+ { humanVerb : 'Put' , method : ' put' } ,
11
+ { humanVerb : 'Deleted' , method : ' delete' } ,
12
+ { humanVerb : 'FetchedHead' , method : ' head' } ,
13
+ { humanVerb : 'Patched' , method : ' patch' } ,
14
+ ] as const ;
15
15
16
16
// initialize a mock here so fetch is patched across all tests
17
17
fetchMock . mockGlobal ( ) ;
@@ -20,34 +20,37 @@ describe.each([
20
20
[ 'patched fetch input' , fetch ] ,
21
21
[ 'fetchMock input' , fetchMock ] ,
22
22
] ) ( 'expect extensions %s' , ( _str , expectInput ) => {
23
- humanVerbToMethods . forEach ( ( verbs ) => {
24
- const [ humanVerb , method ] = verbs . split ( ':' ) ;
23
+ humanVerbToMethods . forEach ( ( { humanVerb, method } ) => {
25
24
describe ( `${ humanVerb } expectations` , ( ) => {
26
25
describe ( 'when no calls' , ( ) => {
27
26
beforeAll ( ( ) => {
28
27
fetchMock . mockGlobal ( ) . route ( '*' , 200 , 'my-route' ) ;
29
28
} ) ;
30
29
afterAll ( ( ) => fetchMock . mockReset ( ) ) ;
31
30
it ( `toHave${ humanVerb } should be falsy` , ( ) => {
31
+ expect ( expectInput ) . not [ `toHave${ humanVerb } ` ] ( ) ;
32
32
expect ( expectInput ) . not [ `toHave${ humanVerb } ` ] (
33
33
'http://example.com/path' ,
34
34
) ;
35
35
} ) ;
36
36
37
37
it ( `toHaveLast${ humanVerb } should be falsy` , ( ) => {
38
+ expect ( expectInput ) . not [ `toHaveLast${ humanVerb } ` ] ( ) ;
38
39
expect ( expectInput ) . not [ `toHaveLast${ humanVerb } ` ] (
39
40
'http://example.com/path' ,
40
41
) ;
41
42
} ) ;
42
43
43
44
it ( `toHaveNth${ humanVerb } should be falsy` , ( ) => {
45
+ expect ( expectInput ) . not [ `toHaveNth${ humanVerb } ` ] ( 1 ) ;
44
46
expect ( expectInput ) . not [ `toHaveNth${ humanVerb } ` ] (
45
47
1 ,
46
48
'http://example.com/path' ,
47
49
) ;
48
50
} ) ;
49
51
50
52
it ( `toHave${ humanVerb } Times should be falsy` , ( ) => {
53
+ expect ( expectInput ) . not [ `toHave${ humanVerb } Times` ] ( 1 ) ;
51
54
expect ( expectInput ) . not [ `toHave${ humanVerb } Times` ] (
52
55
1 ,
53
56
'http://example.com/path' ,
@@ -58,20 +61,24 @@ describe.each([
58
61
beforeAll ( ( ) => {
59
62
fetchMock . mockGlobal ( ) . route ( '*' , 200 ) ;
60
63
fetch ( 'http://example.com/path2' , {
61
- method : method || 'get' ,
64
+ method,
62
65
headers : {
63
66
test : 'header' ,
64
67
} ,
65
68
} ) ;
66
69
fetch ( 'http://example.com/path' , {
67
- method : method || 'get' ,
70
+ method,
68
71
headers : {
69
72
test : 'header' ,
70
73
} ,
71
74
} ) ;
72
75
} ) ;
73
76
afterAll ( ( ) => fetchMock . mockReset ( ) ) ;
74
77
78
+ it ( 'matches without any matcher supplied' , ( ) => {
79
+ expect ( expectInput ) [ `toHave${ humanVerb } ` ] ( ) ;
80
+ } ) ;
81
+
75
82
it ( 'matches with just url' , ( ) => {
76
83
expect ( expectInput ) [ `toHave${ humanVerb } ` ] ( 'http://example.com/path' ) ;
77
84
} ) ;
@@ -111,19 +118,30 @@ describe.each([
111
118
} ,
112
119
) ;
113
120
} ) ;
121
+
122
+ it ( 'should not be any' , ( ) => {
123
+ expectTypeOf ( expect ( expectInput ) [ `toHave${ humanVerb } ` ] ) . not . toBeAny ( ) ;
124
+ expectTypeOf (
125
+ expect ( expectInput ) . not [ `toHave${ humanVerb } ` ] ,
126
+ ) . not . toBeAny ( ) ;
127
+ } ) ;
114
128
} ) ;
115
129
describe ( `toHaveLast${ humanVerb } ` , ( ) => {
116
130
beforeAll ( ( ) => {
117
131
fetchMock . mockGlobal ( ) . route ( '*' , 200 ) ;
118
132
fetch ( 'http://example.com/path' , {
119
- method : method || 'get' ,
133
+ method,
120
134
headers : {
121
135
test : 'header' ,
122
136
} ,
123
137
} ) ;
124
138
} ) ;
125
139
afterAll ( ( ) => fetchMock . mockReset ( ) ) ;
126
140
141
+ it ( 'matches without any matcher supplied' , ( ) => {
142
+ expect ( expectInput ) [ `toHaveLast${ humanVerb } ` ] ( ) ;
143
+ } ) ;
144
+
127
145
it ( 'matches with just url' , ( ) => {
128
146
expect ( expectInput ) [ `toHaveLast${ humanVerb } ` ] (
129
147
'http://example.com/path' ,
@@ -168,26 +186,39 @@ describe.each([
168
186
} ,
169
187
) ;
170
188
} ) ;
189
+
190
+ it ( 'should not be any' , ( ) => {
191
+ expectTypeOf (
192
+ expect ( expectInput ) [ `toHaveLast${ humanVerb } ` ] ,
193
+ ) . not . toBeAny ( ) ;
194
+ expectTypeOf (
195
+ expect ( expectInput ) . not [ `toHaveLast${ humanVerb } ` ] ,
196
+ ) . not . toBeAny ( ) ;
197
+ } ) ;
171
198
} ) ;
172
199
173
200
describe ( `toHaveNth${ humanVerb } ` , ( ) => {
174
201
beforeAll ( ( ) => {
175
202
fetchMock . mockGlobal ( ) . route ( '*' , 200 ) ;
176
203
fetch ( 'http://example1.com/path' , {
177
- method : method || 'get' ,
204
+ method,
178
205
headers : {
179
206
test : 'header' ,
180
207
} ,
181
208
} ) ;
182
209
fetch ( 'http://example2.com/path' , {
183
- method : method || 'get' ,
210
+ method,
184
211
headers : {
185
212
test : 'header' ,
186
213
} ,
187
214
} ) ;
188
215
} ) ;
189
216
afterAll ( ( ) => fetchMock . mockReset ( ) ) ;
190
217
218
+ it ( 'matches without any matcher supplied' , ( ) => {
219
+ expect ( expectInput ) [ `toHaveNth${ humanVerb } ` ] ( 2 ) ;
220
+ } ) ;
221
+
191
222
it ( 'matches with just url' , ( ) => {
192
223
expect ( expectInput ) [ `toHaveNth${ humanVerb } ` ] (
193
224
2 ,
@@ -244,26 +275,39 @@ describe.each([
244
275
'http://example2.com/path' ,
245
276
) ;
246
277
} ) ;
278
+
279
+ it ( 'should not be any' , ( ) => {
280
+ expectTypeOf (
281
+ expect ( expectInput ) [ `toHaveNth${ humanVerb } ` ] ,
282
+ ) . not . toBeAny ( ) ;
283
+ expectTypeOf (
284
+ expect ( expectInput ) . not [ `toHaveNth${ humanVerb } ` ] ,
285
+ ) . not . toBeAny ( ) ;
286
+ } ) ;
247
287
} ) ;
248
288
249
289
describe ( `toHave${ humanVerb } Times` , ( ) => {
250
290
beforeAll ( ( ) => {
251
291
fetchMock . mockGlobal ( ) . route ( '*' , 200 ) ;
252
292
fetch ( 'http://example.com/path' , {
253
- method : method || 'get' ,
293
+ method,
254
294
headers : {
255
295
test : 'header' ,
256
296
} ,
257
297
} ) ;
258
298
fetch ( 'http://example.com/path' , {
259
- method : method || 'get' ,
299
+ method,
260
300
headers : {
261
301
test : 'header' ,
262
302
} ,
263
303
} ) ;
264
304
} ) ;
265
305
afterAll ( ( ) => fetchMock . mockReset ( ) ) ;
266
306
307
+ it ( 'matches without any matcher supplied' , ( ) => {
308
+ expect ( expectInput ) [ `toHave${ humanVerb } Times` ] ( 2 ) ;
309
+ } ) ;
310
+
267
311
it ( 'matches with just url' , ( ) => {
268
312
expect ( expectInput ) [ `toHave${ humanVerb } Times` ] (
269
313
2 ,
@@ -327,6 +371,15 @@ describe.each([
327
371
'http://example.com/path' ,
328
372
) ;
329
373
} ) ;
374
+
375
+ it ( 'should not be any' , ( ) => {
376
+ expectTypeOf (
377
+ expect ( expectInput ) [ `toHave${ humanVerb } Times` ] ,
378
+ ) . not . toBeAny ( ) ;
379
+ expectTypeOf (
380
+ expect ( expectInput ) . not [ `toHave${ humanVerb } Times` ] ,
381
+ ) . not . toBeAny ( ) ;
382
+ } ) ;
330
383
} ) ;
331
384
} ) ;
332
385
} ) ;
@@ -359,12 +412,16 @@ describe.each([
359
412
it ( "doesn't match if too few calls" , ( ) => {
360
413
expect ( expectInput ) . not . toBeDone ( 'route2' ) ;
361
414
} ) ;
415
+
416
+ it ( 'should not be any' , ( ) => {
417
+ expectTypeOf ( expect ( expectInput ) . toBeDone ) . not . toBeAny ( ) ;
418
+ expectTypeOf ( expect ( expectInput ) . not . toBeDone ) . not . toBeAny ( ) ;
419
+ } ) ;
362
420
} ) ;
363
421
} ) ;
364
422
365
423
describe ( 'expect extensions: bad inputs' , ( ) => {
366
- humanVerbToMethods . forEach ( ( verbs ) => {
367
- const [ humanVerb ] = verbs . split ( ':' ) ;
424
+ humanVerbToMethods . forEach ( ( { humanVerb } ) => {
368
425
it ( `${ humanVerb } - throws an error if we the input is not patched with fetchMock` , ( ) => {
369
426
expect ( ( ) => {
370
427
// This simulates a "fetch" implementation that doesn't have fetchMock
0 commit comments