@@ -2,7 +2,7 @@ import { ApolloError, AuthenticationError, ForbiddenError } from 'apollo-server-
22import anyTest , { TestInterface } from 'ava'
33import { uid } from 'uid'
44import nock from 'nock'
5- import { CancelError , HTTPDataSource , TimeoutError , RequestOptions } from '../src'
5+ import { CancelError , HTTPDataSource , TimeoutError , RequestOptions , RequestError , Request } from '../src'
66import { DataSourceConfig } from 'apollo-datasource'
77
88const test = anyTest as TestInterface < { path : string } >
@@ -126,6 +126,8 @@ test('Should cache subsequent GET calls to the same endpoint', async (t) => {
126126} )
127127
128128test ( 'Should be able to define a custom cache key for request memoization' , async ( t ) => {
129+ t . plan ( 5 )
130+
129131 const baseURL = 'https://api.example.com'
130132 const { path } = t . context
131133 const scope = nock ( baseURL ) . get ( path ) . times ( 1 ) . reply ( 200 , { name : 'foo' } )
@@ -134,6 +136,7 @@ test('Should be able to define a custom cache key for request memoization', asyn
134136 baseURL = baseURL
135137
136138 onCacheKeyCalculation ( _requestOptions : RequestOptions ) {
139+ t . pass ( 'onCacheKeyCalculation' ) ;
137140 return 'foo'
138141 }
139142
@@ -188,6 +191,39 @@ test('Should timeout', async (t) => {
188191 t . is ( scope . isDone ( ) , true )
189192} )
190193
194+ test ( 'Should call onRequestError on request error' , async ( t ) => {
195+ t . plan ( 5 )
196+
197+ const baseURL = 'https://api.example.com'
198+ const { path } = t . context
199+ const scope = nock ( baseURL ) . get ( path ) . reply ( 500 )
200+
201+ const dataSource = new ( class extends HTTPDataSource {
202+ baseURL = baseURL
203+
204+ async onRequestError ( error : Error , request ?: Request ) {
205+ t . true ( error instanceof RequestError ) ;
206+ t . truthy ( request ) ;
207+ t . pass ( 'onRequestError' ) ;
208+ }
209+
210+ async getFoo ( ) {
211+ return await this . get ( path )
212+ }
213+ } ) ( )
214+
215+ await t . throwsAsync (
216+ dataSource . getFoo ( ) ,
217+ {
218+ instanceOf : ApolloError ,
219+ message : "Response code 500 (Internal Server Error)" ,
220+ } ,
221+ 'Server error' ,
222+ )
223+
224+ t . is ( scope . isDone ( ) , true )
225+ } )
226+
191227test . cb ( 'Should abort request' , ( t ) => {
192228 t . plan ( 2 )
193229
0 commit comments