File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -211,3 +211,34 @@ it('should format errors using the formatter', async () => {
211
211
`[GraphQLError: Cannot query field "idontexist" on type "Query".]` ,
212
212
) ;
213
213
} ) ;
214
+
215
+ it ( 'should respect plain errors toJSON implementation' , async ( ) => {
216
+ class MyError extends Error {
217
+ constructor ( msg : string ) {
218
+ super ( msg ) ;
219
+ }
220
+ toJSON ( ) {
221
+ return {
222
+ message : this . message ,
223
+ toJSON : 'used' ,
224
+ } ;
225
+ }
226
+ }
227
+ const formatErrorFn = jest . fn ( ( _err ) => new MyError ( 'Custom toJSON' ) ) ;
228
+ const server = startTServer ( {
229
+ formatError : formatErrorFn ,
230
+ } ) ;
231
+ const url = new URL ( server . url ) ;
232
+ url . searchParams . set ( 'query' , '{ idontexist }' ) ;
233
+ const res = await fetch ( url . toString ( ) ) ;
234
+ expect ( res . json ( ) ) . resolves . toMatchInlineSnapshot ( `
235
+ {
236
+ "errors": [
237
+ {
238
+ "message": "Custom toJSON",
239
+ "toJSON": "used",
240
+ },
241
+ ],
242
+ }
243
+ ` ) ;
244
+ } ) ;
You can’t perform that action at this time.
0 commit comments