Skip to content

Commit 2c505ee

Browse files
committed
test(handler): make sure error's toJSON is respected
1 parent c0eaeb4 commit 2c505ee

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/__tests__/handler.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,34 @@ it('should format errors using the formatter', async () => {
211211
`[GraphQLError: Cannot query field "idontexist" on type "Query".]`,
212212
);
213213
});
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+
});

0 commit comments

Comments
 (0)