This repository has been archived by the owner on Apr 15, 2020. It is now read-only.
forked from ardatan/graphql-tools
/
testErrors.ts
98 lines (89 loc) · 3.06 KB
/
testErrors.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { assert } from 'chai';
import { GraphQLResolveInfo, GraphQLError } from 'graphql';
import {
relocatedError,
checkResultAndHandleErrors,
getErrorsFromParent,
ERROR_SYMBOL
} from '../stitching/errors';
import 'mocha';
class ErrorWithExtensions extends GraphQLError {
constructor(message: string, code: string) {
super(message, null, null, null, null, null, { code });
}
}
describe('Errors', () => {
describe('relocatedError', () => {
it('should adjust the path of a GraphqlError', () => {
const originalError = new GraphQLError('test', null, null, null, ['test']);
const newError = relocatedError(originalError, null, ['test', 1]);
const expectedError = new GraphQLError('test', null, null, null, ['test', 1]);
assert.deepEqual(newError, expectedError);
});
it('should also locate a non GraphQLError', () => {
const originalError = new Error('test');
const newError = relocatedError(originalError, null, ['test', 1]);
const expectedError = new GraphQLError('test', null, null, null, ['test', 1]);
assert.deepEqual(newError, expectedError);
});
});
describe('getErrorsFromParent', () => {
it('should return all errors including if path is not defined', () => {
const mockErrors = {
responseKey: '',
[ERROR_SYMBOL]: [
{
message: 'Test error without path'
} as GraphQLError
]
};
assert.deepEqual(getErrorsFromParent(mockErrors, 'responseKey'),
[mockErrors[ERROR_SYMBOL][0]]
);
});
});
describe('checkResultAndHandleErrors', () => {
it('persists single error', () => {
const result = {
errors: [new GraphQLError('Test error')]
};
try {
checkResultAndHandleErrors(result, {} as GraphQLResolveInfo, 'responseKey');
} catch (e) {
assert.equal(e.message, 'Test error');
assert.isUndefined(e.originalError.errors);
}
});
it('persists single error with extensions', () => {
const result = {
errors: [new ErrorWithExtensions('Test error', 'UNAUTHENTICATED')]
};
try {
checkResultAndHandleErrors(result, {} as GraphQLResolveInfo, 'responseKey');
} catch (e) {
assert.equal(e.message, 'Test error');
assert.equal(e.extensions && e.extensions.code, 'UNAUTHENTICATED');
assert.isUndefined(e.originalError.errors);
}
});
it('combines errors and persists the original errors', () => {
const result = {
errors: [
new GraphQLError('Error1'),
new GraphQLError('Error2'),
]
};
try {
checkResultAndHandleErrors(result, {} as GraphQLResolveInfo, 'responseKey');
} catch (e) {
assert.equal(e.message, 'Error1\nError2');
assert.isNotEmpty(e.originalError);
assert.isNotEmpty(e.originalError.errors);
assert.lengthOf(e.originalError.errors, result.errors.length);
result.errors.forEach((error, i) => {
assert.deepEqual(e.originalError.errors[i], error);
});
}
});
});
});