Mirror: The spec-compliant minimum of client-side GraphQL.
1import { describe, it, expect } from 'vitest';
2
3import { Kind } from '../kind';
4import { GraphQLError as graphql_GraphQLError } from 'graphql';
5import { GraphQLError } from '../error';
6
7describe('GraphQLError', () => {
8 it('sorts input arguments into properties', () => {
9 const inputs = ['message', [], { body: '' }, [], [], new Error(), {}] as const;
10
11 const error = new GraphQLError(...inputs);
12 expect(error).toMatchInlineSnapshot('[GraphQLError: message]');
13 expect(error).toEqual(new (graphql_GraphQLError as any)(...inputs));
14 });
15
16 it('normalizes incoming nodes to arrays', () => {
17 const error = new GraphQLError('message', { kind: Kind.NULL });
18 expect(error.nodes).toEqual([{ kind: Kind.NULL }]);
19 });
20
21 it('allows toJSON and toString calls', () => {
22 const error = new GraphQLError('message');
23 expect(error.toString()).toEqual('message');
24 expect(error.toJSON()).toEqual({
25 name: 'GraphQLError',
26 message: 'message',
27 extensions: {},
28 locations: undefined,
29 nodes: undefined,
30 originalError: undefined,
31 path: undefined,
32 positions: undefined,
33 source: undefined,
34 });
35 });
36
37 it('normalizes extensions as expected', () => {
38 const inputs = (extensions: any, originalError = new Error()) =>
39 ['message', [], { body: '' }, [], [], originalError, extensions] as const;
40
41 expect(new GraphQLError(...inputs(undefined)).extensions).toEqual({});
42 expect(new GraphQLError(...inputs({ test: true })).extensions).toEqual({ test: true });
43
44 expect(
45 new GraphQLError(...inputs({ test: true }, { extensions: { override: true } } as any))
46 .extensions
47 ).toEqual({ test: true });
48
49 expect(
50 new GraphQLError(...inputs(undefined, { extensions: { override: true } } as any)).extensions
51 ).toEqual({ override: true });
52 });
53});