@@ -20,7 +20,7 @@ test('properties', withServer, async (t, server, got) => {
20
20
21
21
const url = new URL ( server . url ) ;
22
22
23
- const error = ( await t . throwsAsync < HTTPError > ( got ( '' ) ) ) ! ;
23
+ const error = ( await t . throwsAsync < HTTPError < string > > ( got ( '' ) ) ) ! ;
24
24
t . truthy ( error ) ;
25
25
t . truthy ( error . response ) ;
26
26
t . truthy ( error . options ) ;
@@ -30,11 +30,12 @@ test('properties', withServer, async (t, server, got) => {
30
30
t . is ( error . message , 'Response code 404 (Not Found)' ) ;
31
31
t . deepEqual ( error . options . url , url ) ;
32
32
t . is ( error . response . headers . connection , 'keep-alive' ) ;
33
- t . is ( error . response . body , 'not' ) ;
33
+ // Assert is used for type checking
34
+ t . assert ( error . response . body === 'not' ) ;
34
35
} ) ;
35
36
36
37
test ( 'catches dns errors' , async t => {
37
- const error = ( await t . throwsAsync < RequestError > ( got ( 'http://doesntexist' , { retry : { limit : 0 } } ) ) ) ! ;
38
+ const error = ( await t . throwsAsync < RequestError < undefined > > ( got ( 'http://doesntexist' , { retry : { limit : 0 } } ) ) ) ! ;
38
39
t . truthy ( error ) ;
39
40
t . regex ( error . message , / E N O T F O U N D | E A I _ A G A I N / ) ;
40
41
t . is ( ( error . options . url as URL ) . host , 'doesntexist' ) ;
@@ -110,7 +111,27 @@ test('custom body', withServer, async (t, server, got) => {
110
111
message : 'Response code 404 (Not Found)' ,
111
112
} ) ;
112
113
t . is ( error ?. response . statusCode , 404 ) ;
113
- t . is ( error ?. response . body , 'not' ) ;
114
+ // Typecheck for default `any` type
115
+ t . assert ( error ?. response . body === 'not' ) ;
116
+ } ) ;
117
+
118
+ test ( 'custom json body' , withServer , async ( t , server , got ) => {
119
+ server . get ( '/' , ( _request , response ) => {
120
+ response . statusCode = 404 ;
121
+ response . header ( 'content-type' , 'application/json' ) ;
122
+ response . end ( JSON . stringify ( {
123
+ message : 'not found' ,
124
+ } ) ) ;
125
+ } ) ;
126
+
127
+ const error = await t . throwsAsync < HTTPError < { message : string } > > ( got ( '' , { responseType : 'json' } ) ,
128
+ {
129
+ instanceOf : HTTPError ,
130
+ message : 'Response code 404 (Not Found)' ,
131
+ } ) ;
132
+ t . is ( error ?. response . statusCode , 404 ) ;
133
+ // Assert is used for body typecheck
134
+ t . assert ( error ?. response . body . message === 'not found' ) ;
114
135
} ) ;
115
136
116
137
test ( 'contains Got options' , withServer , async ( t , server , got ) => {
0 commit comments