1import { describe, it, expect } from 'vitest';
2import { CombinedError } from './error';
3
4describe('CombinedError', () => {
5 it('can be instantiated with graphQLErrors', () => {
6 const err = new CombinedError({
7 graphQLErrors: [],
8 });
9
10 expect(err.name).toBe('CombinedError');
11 });
12
13 it('behaves like a normal Error', () => {
14 const err = new CombinedError({
15 networkError: new Error('test'),
16 });
17
18 expect(err).toBeInstanceOf(CombinedError);
19 expect(err).toBeInstanceOf(Error);
20 expect('' + err).toMatchSnapshot();
21 });
22
23 it('accepts graphQLError messages and generates a single message from them', () => {
24 const graphQLErrors = ['Error Message A', 'Error Message B'];
25
26 const err = new CombinedError({ graphQLErrors });
27
28 expect(err.message).toBe(
29 `
30[GraphQL] Error Message A
31[GraphQL] Error Message B
32 `.trim()
33 );
34
35 expect(err.graphQLErrors).toEqual(graphQLErrors.map(x => new Error(x)));
36 });
37
38 it('accepts a network error and generates a message from it', () => {
39 const networkError = new Error('Network Shenanigans');
40 const err = new CombinedError({ networkError });
41
42 expect(err.message).toBe(`[Network] ${networkError.message}`);
43 });
44
45 it('accepts actual errors for graphQLError', () => {
46 const graphQLErrors = [
47 new Error('Error Message A'),
48 new Error('Error Message B'),
49 ];
50
51 const err = new CombinedError({ graphQLErrors });
52
53 expect(err.message).toBe(
54 `
55[GraphQL] Error Message A
56[GraphQL] Error Message B
57 `.trim()
58 );
59
60 expect(err.graphQLErrors).toEqual(graphQLErrors);
61 });
62
63 it('accepts empty string errors for graphQLError', () => {
64 const graphQLErrors = [new Error('')];
65
66 const err = new CombinedError({ graphQLErrors });
67
68 expect(err.message).toBe('[GraphQL] ');
69
70 expect(err.graphQLErrors).toEqual(graphQLErrors);
71 });
72
73 it('accepts a response that is attached to the resulting error', () => {
74 const response = {};
75 const err = new CombinedError({
76 graphQLErrors: [],
77 response,
78 });
79
80 expect(err.response).toBe(response);
81 });
82});