@@ -77,7 +77,7 @@ describe("Test Service", () => {
7777 await stop ( ) ;
7878 } ) ;
7979
80- it ( "should call sendError if error occured " , async ( ) => {
80+ it ( "should call sendError if error occurs when preparing graphql schema " , async ( ) => {
8181 const { svc, stop } = await startService ( ) ;
8282
8383 const err = new Error ( "Something happened" ) ;
@@ -99,6 +99,29 @@ describe("Test Service", () => {
9999
100100 await stop ( ) ;
101101 } ) ;
102+
103+ it ( "should call sendError if error occurs when handling graphql request" , async ( ) => {
104+ const { svc, stop } = await startService ( ) ;
105+
106+ const err = new Error ( "Something happened" ) ;
107+ svc . sendError = jest . fn ( ) ;
108+ svc . prepareGraphQLSchema = jest . fn ( ) ;
109+ svc . graphqlHandler = jest . fn ( ( ) => {
110+ throw err ;
111+ } ) ;
112+ const fakeReq = { req : 1 } ;
113+ const fakeRes = { res : 1 } ;
114+
115+ const res = await svc . settings . routes [ 0 ] . aliases [ "/" ] . call ( svc , fakeReq , fakeRes ) ;
116+
117+ expect ( res ) . toBeUndefined ( ) ;
118+ expect ( svc . prepareGraphQLSchema ) . toBeCalledTimes ( 1 ) ;
119+ expect ( svc . graphqlHandler ) . toBeCalledTimes ( 1 ) ;
120+ expect ( svc . sendError ) . toBeCalledTimes ( 1 ) ;
121+ expect ( svc . sendError ) . toBeCalledWith ( fakeReq , fakeRes , err ) ;
122+
123+ await stop ( ) ;
124+ } ) ;
102125 } ) ;
103126
104127 describe ( "Test `GET /.well-known/apollo/server-health` route handler" , ( ) => {
@@ -123,7 +146,7 @@ describe("Test Service", () => {
123146 await stop ( ) ;
124147 } ) ;
125148
126- it ( "should call sendError if error occured " , async ( ) => {
149+ it ( "should call sendError if error occurs when preparing graphql schema " , async ( ) => {
127150 const { svc, stop } = await startService ( ) ;
128151
129152 const err = new Error ( "Something happened" ) ;
@@ -146,13 +169,38 @@ describe("Test Service", () => {
146169 expect ( svc . sendResponse ) . toBeCalledWith (
147170 fakeReq ,
148171 fakeRes ,
149- {
150- status : "fail" ,
151- schema : false ,
152- } ,
153- {
154- responseType : "application/health+json" ,
155- }
172+ { status : "fail" , schema : false } ,
173+ { responseType : "application/health+json" }
174+ ) ;
175+
176+ await stop ( ) ;
177+ } ) ;
178+
179+ it ( "should call sendError if error occurs when handling graphql request" , async ( ) => {
180+ const { svc, stop } = await startService ( ) ;
181+
182+ const err = new Error ( "Something happened" ) ;
183+ svc . sendResponse = jest . fn ( ) ;
184+ svc . prepareGraphQLSchema = jest . fn ( ) ;
185+ svc . graphqlHandler = jest . fn ( ( ) => {
186+ throw err ;
187+ } ) ;
188+ const fakeReq = { req : 1 } ;
189+ const fakeRes = { res : 1 } ;
190+
191+ const res = await svc . settings . routes [ 0 ] . aliases [
192+ "GET /.well-known/apollo/server-health"
193+ ] . call ( svc , fakeReq , fakeRes ) ;
194+
195+ expect ( res ) . toBeUndefined ( ) ;
196+ expect ( svc . prepareGraphQLSchema ) . toBeCalledTimes ( 1 ) ;
197+ expect ( svc . graphqlHandler ) . toBeCalledTimes ( 1 ) ;
198+ expect ( svc . sendResponse ) . toBeCalledTimes ( 1 ) ;
199+ expect ( svc . sendResponse ) . toBeCalledWith (
200+ fakeReq ,
201+ fakeRes ,
202+ { status : "fail" , schema : false } ,
203+ { responseType : "application/health+json" }
156204 ) ;
157205
158206 await stop ( ) ;
0 commit comments