4
4
* This source code is licensed under the MIT license found in the
5
5
* LICENSE file in the root directory of this source tree.
6
6
*
7
- * @noflow
7
+ * @flow strict
8
8
*/
9
9
10
10
import { expect } from 'chai' ;
11
11
import { describe , it } from 'mocha' ;
12
12
13
- import { parse , Source , GraphQLError , formatError } from '../../' ;
13
+ import dedent from '../../jsutils/dedent' ;
14
+ import invariant from '../../jsutils/invariant' ;
15
+ import { Kind , parse , Source , GraphQLError , formatError } from '../../' ;
16
+
17
+ const source = new Source ( dedent `
18
+ {
19
+ field
20
+ }
21
+ ` ) ;
22
+ const ast = parse ( source ) ;
23
+ const operationNode = ast . definitions [ 0 ] ;
24
+ invariant ( operationNode && operationNode . kind === Kind . OPERATION_DEFINITION ) ;
25
+ const fieldNode = operationNode . selectionSet . selections [ 0 ] ;
26
+ invariant ( fieldNode ) ;
14
27
15
28
describe ( 'GraphQLError' , ( ) => {
16
29
it ( 'is a class and is a subclass of Error' , ( ) => {
17
- expect ( new GraphQLError ( ) ) . to . be . instanceof ( Error ) ;
18
- expect ( new GraphQLError ( ) ) . to . be . instanceof ( GraphQLError ) ;
30
+ expect ( new GraphQLError ( 'str' ) ) . to . be . instanceof ( Error ) ;
31
+ expect ( new GraphQLError ( 'str' ) ) . to . be . instanceof ( GraphQLError ) ;
19
32
} ) ;
20
33
21
34
it ( 'has a name, message, and stack trace' , ( ) => {
@@ -42,7 +55,7 @@ describe('GraphQLError', () => {
42
55
} ) ;
43
56
44
57
it ( 'creates new stack if original error has no stack' , ( ) => {
45
- const original = { message : 'original' } ;
58
+ const original = new Error ( 'original' ) ;
46
59
const e = new GraphQLError ( 'msg' , null , null , null , null , original ) ;
47
60
expect ( e . name ) . to . equal ( 'GraphQLError' ) ;
48
61
expect ( e . stack ) . to . be . a ( 'string' ) ;
@@ -51,37 +64,22 @@ describe('GraphQLError', () => {
51
64
} ) ;
52
65
53
66
it ( 'converts nodes to positions and locations' , ( ) => {
54
- const source = new Source ( `{
55
- field
56
- }` ) ;
57
- const ast = parse ( source ) ;
58
- const fieldNode = ast . definitions [ 0 ] . selectionSet . selections [ 0 ] ;
59
67
const e = new GraphQLError ( 'msg' , [ fieldNode ] ) ;
60
68
expect ( e . nodes ) . to . deep . equal ( [ fieldNode ] ) ;
61
69
expect ( e . source ) . to . equal ( source ) ;
62
- expect ( e . positions ) . to . deep . equal ( [ 8 ] ) ;
63
- expect ( e . locations ) . to . deep . equal ( [ { line : 2 , column : 7 } ] ) ;
70
+ expect ( e . positions ) . to . deep . equal ( [ 4 ] ) ;
71
+ expect ( e . locations ) . to . deep . equal ( [ { line : 2 , column : 3 } ] ) ;
64
72
} ) ;
65
73
66
74
it ( 'converts single node to positions and locations' , ( ) => {
67
- const source = new Source ( `{
68
- field
69
- }` ) ;
70
- const ast = parse ( source ) ;
71
- const fieldNode = ast . definitions [ 0 ] . selectionSet . selections [ 0 ] ;
72
75
const e = new GraphQLError ( 'msg' , fieldNode ) ; // Non-array value.
73
76
expect ( e . nodes ) . to . deep . equal ( [ fieldNode ] ) ;
74
77
expect ( e . source ) . to . equal ( source ) ;
75
- expect ( e . positions ) . to . deep . equal ( [ 8 ] ) ;
76
- expect ( e . locations ) . to . deep . equal ( [ { line : 2 , column : 7 } ] ) ;
78
+ expect ( e . positions ) . to . deep . equal ( [ 4 ] ) ;
79
+ expect ( e . locations ) . to . deep . equal ( [ { line : 2 , column : 3 } ] ) ;
77
80
} ) ;
78
81
79
82
it ( 'converts node with loc.start === 0 to positions and locations' , ( ) => {
80
- const source = new Source ( `{
81
- field
82
- }` ) ;
83
- const ast = parse ( source ) ;
84
- const operationNode = ast . definitions [ 0 ] ;
85
83
const e = new GraphQLError ( 'msg' , [ operationNode ] ) ;
86
84
expect ( e . nodes ) . to . deep . equal ( [ operationNode ] ) ;
87
85
expect ( e . source ) . to . equal ( source ) ;
@@ -90,14 +88,11 @@ describe('GraphQLError', () => {
90
88
} ) ;
91
89
92
90
it ( 'converts source and positions to locations' , ( ) => {
93
- const source = new Source ( `{
94
- field
95
- }` ) ;
96
- const e = new GraphQLError ( 'msg' , null , source , [ 10 ] ) ;
91
+ const e = new GraphQLError ( 'msg' , null , source , [ 6 ] ) ;
97
92
expect ( e . nodes ) . to . equal ( undefined ) ;
98
93
expect ( e . source ) . to . equal ( source ) ;
99
- expect ( e . positions ) . to . deep . equal ( [ 10 ] ) ;
100
- expect ( e . locations ) . to . deep . equal ( [ { line : 2 , column : 9 } ] ) ;
94
+ expect ( e . positions ) . to . deep . equal ( [ 6 ] ) ;
95
+ expect ( e . locations ) . to . deep . equal ( [ { line : 2 , column : 5 } ] ) ;
101
96
} ) ;
102
97
103
98
it ( 'serializes to include message' , ( ) => {
@@ -106,10 +101,9 @@ describe('GraphQLError', () => {
106
101
} ) ;
107
102
108
103
it ( 'serializes to include message and locations' , ( ) => {
109
- const node = parse ( '{ field }' ) . definitions [ 0 ] . selectionSet . selections [ 0 ] ;
110
- const e = new GraphQLError ( 'msg' , [ node ] ) ;
104
+ const e = new GraphQLError ( 'msg' , [ fieldNode ] ) ;
111
105
expect ( JSON . stringify ( e ) ) . to . equal (
112
- '{"message":"msg","locations":[{"line":1 ,"column":3}]}' ,
106
+ '{"message":"msg","locations":[{"line":2 ,"column":3}]}' ,
113
107
) ;
114
108
} ) ;
115
109
0 commit comments