1
1
import { RequestInit , Headers } from 'apollo-server-env' ;
2
+ type RequestInitWithJSONBody = Omit < RequestInit , 'body' > & { body ?: object }
2
3
3
4
// Make this file a module
4
5
// See: https://github.com/microsoft/TypeScript/issues/17736
5
6
export { } ;
6
7
declare global {
7
8
namespace jest {
8
- interface Matchers < R , T > {
9
- toHaveFetched ( spy : SpyInstance ) : R ;
9
+ interface Matchers < R > {
10
+ toHaveFetched ( requestUrl : string , requestOpts ?: RequestInitWithJSONBody ) : R ;
11
+ toHaveFetchedNth ( nthCall : number , requestUrl : string , requestOpts ?: RequestInitWithJSONBody ) : R ;
10
12
}
11
13
}
12
14
}
13
15
14
- function prepareHttpOptions ( requestUrl : string , requestOpts : RequestInit ) : RequestInit {
15
- const headers = new Headers ( ) ;
16
+ function prepareHttpOptions ( requestUrl : string , requestOpts : RequestInitWithJSONBody ) : RequestInit {
17
+ const headers = new Headers ( requestOpts . headers ) ;
16
18
headers . set ( 'Content-Type' , 'application/json' ) ;
17
- if ( requestOpts . headers ) {
18
- for ( let name in requestOpts . headers ) {
19
- headers . set ( name , requestOpts . headers [ name ] ) ;
20
- }
21
- }
22
19
23
20
const requestHttp = {
24
21
method : 'POST' ,
@@ -37,7 +34,7 @@ function toHaveFetched(
37
34
this : jest . MatcherUtils ,
38
35
fetch : jest . SpyInstance ,
39
36
requestUrl : string ,
40
- requestOpts : RequestInit
37
+ requestOpts : RequestInitWithJSONBody = { }
41
38
) : { message ( ) : string ; pass : boolean } {
42
39
const httpOptions = prepareHttpOptions ( requestUrl , requestOpts ) ;
43
40
let pass = false ;
@@ -60,7 +57,7 @@ function toHaveFetchedNth(
60
57
fetch : jest . SpyInstance ,
61
58
nthCall : number ,
62
59
requestUrl : string ,
63
- requestOpts : RequestInit
60
+ requestOpts : RequestInitWithJSONBody = { }
64
61
) : { message ( ) : string ; pass : boolean } {
65
62
const httpOptions = prepareHttpOptions ( requestUrl , requestOpts ) ;
66
63
let pass = false ;
0 commit comments