Skip to content

Commit f0ae3f4

Browse files
Enable Flow typings on errors tests + Fix typing for Error constructor (#1582)
1 parent 5384d21 commit f0ae3f4

File tree

5 files changed

+48
-47
lines changed

5 files changed

+48
-47
lines changed

src/error/GraphQLError.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import type { Source } from '../language/source';
2222
declare class GraphQLError extends Error {
2323
constructor(
2424
message: string,
25-
nodes?: $ReadOnlyArray<ASTNode> | ASTNode | void,
25+
nodes?: $ReadOnlyArray<ASTNode> | ASTNode | void | null,
2626
source?: ?Source,
2727
positions?: ?$ReadOnlyArray<number>,
2828
path?: ?$ReadOnlyArray<string | number>,

src/error/__tests__/GraphQLError-test.js

+27-33
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,31 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @noflow
7+
* @flow strict
88
*/
99

1010
import { expect } from 'chai';
1111
import { describe, it } from 'mocha';
1212

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);
1427

1528
describe('GraphQLError', () => {
1629
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);
1932
});
2033

2134
it('has a name, message, and stack trace', () => {
@@ -42,7 +55,7 @@ describe('GraphQLError', () => {
4255
});
4356

4457
it('creates new stack if original error has no stack', () => {
45-
const original = { message: 'original' };
58+
const original = new Error('original');
4659
const e = new GraphQLError('msg', null, null, null, null, original);
4760
expect(e.name).to.equal('GraphQLError');
4861
expect(e.stack).to.be.a('string');
@@ -51,37 +64,22 @@ describe('GraphQLError', () => {
5164
});
5265

5366
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];
5967
const e = new GraphQLError('msg', [fieldNode]);
6068
expect(e.nodes).to.deep.equal([fieldNode]);
6169
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 }]);
6472
});
6573

6674
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];
7275
const e = new GraphQLError('msg', fieldNode); // Non-array value.
7376
expect(e.nodes).to.deep.equal([fieldNode]);
7477
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 }]);
7780
});
7881

7982
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];
8583
const e = new GraphQLError('msg', [operationNode]);
8684
expect(e.nodes).to.deep.equal([operationNode]);
8785
expect(e.source).to.equal(source);
@@ -90,14 +88,11 @@ describe('GraphQLError', () => {
9088
});
9189

9290
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]);
9792
expect(e.nodes).to.equal(undefined);
9893
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 }]);
10196
});
10297

10398
it('serializes to include message', () => {
@@ -106,10 +101,9 @@ describe('GraphQLError', () => {
106101
});
107102

108103
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]);
111105
expect(JSON.stringify(e)).to.equal(
112-
'{"message":"msg","locations":[{"line":1,"column":3}]}',
106+
'{"message":"msg","locations":[{"line":2,"column":3}]}',
113107
);
114108
});
115109

src/error/__tests__/locatedError-test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @noflow
7+
* @flow strict
88
*/
99

1010
import { expect } from 'chai';
@@ -26,7 +26,7 @@ describe('locatedError', () => {
2626
});
2727

2828
it('passes GraphQLError-ish through', () => {
29-
const e = new Error('I have a different prototype chain');
29+
const e: any = new Error('I have a different prototype chain');
3030
e.locations = [];
3131
e.path = [];
3232
e.nodes = [];
@@ -38,7 +38,7 @@ describe('locatedError', () => {
3838
});
3939

4040
it('does not pass through elasticsearch-like errors', () => {
41-
const e = new Error('I am from elasticsearch');
41+
const e: any = new Error('I am from elasticsearch');
4242
e.path = '/something/feed/_search';
4343

4444
expect(locatedError(e, [], [])).to.not.deep.equal(e);

src/error/__tests__/printError-test.js

+15-9
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @noflow
7+
* @flow strict
88
*/
99

1010
import { expect } from 'chai';
1111
import { describe, it } from 'mocha';
1212

13+
import dedent from '../../jsutils/dedent';
14+
import invariant from '../../jsutils/invariant';
1315
import { GraphQLError } from '../GraphQLError';
1416
import { printError } from '../printError';
15-
import { parse, Source } from '../../language';
16-
import dedent from '../../jsutils/dedent';
17+
import { Kind, parse, Source } from '../../language';
1718

1819
describe('printError', () => {
1920
it('prints an line numbers with correct padding', () => {
@@ -48,7 +49,7 @@ describe('printError', () => {
4849
});
4950

5051
it('prints an error with nodes from different sources', () => {
51-
const sourceA = parse(
52+
const docA = parse(
5253
new Source(
5354
dedent`
5455
type Foo {
@@ -58,9 +59,11 @@ describe('printError', () => {
5859
'SourceA',
5960
),
6061
);
61-
const fieldTypeA = sourceA.definitions[0].fields[0].type;
62+
const opA = docA.definitions[0];
63+
invariant(opA && opA.kind === Kind.OBJECT_TYPE_DEFINITION && opA.fields);
64+
const fieldA = opA.fields[0];
6265

63-
const sourceB = parse(
66+
const docB = parse(
6467
new Source(
6568
dedent`
6669
type Foo {
@@ -70,11 +73,14 @@ describe('printError', () => {
7073
'SourceB',
7174
),
7275
);
73-
const fieldTypeB = sourceB.definitions[0].fields[0].type;
76+
const opB = docB.definitions[0];
77+
invariant(opB && opB.kind === Kind.OBJECT_TYPE_DEFINITION && opB.fields);
78+
const fieldB = opB.fields[0];
7479

80+
invariant(fieldA && fieldB);
7581
const error = new GraphQLError('Example error with two nodes', [
76-
fieldTypeA,
77-
fieldTypeB,
82+
fieldA.type,
83+
fieldB.type,
7884
]);
7985

8086
expect(printError(error)).to.equal(dedent`

src/jsutils/__tests__/inspect-test.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @noflow
7+
* @flow strict
88
*/
99

1010
import { expect } from 'chai';
@@ -28,6 +28,7 @@ describe('inspect', () => {
2828
it('string', () => {
2929
expect(inspect('')).to.equal('""');
3030
expect(inspect('abc')).to.equal('"abc"');
31+
// $FlowFixMe
3132
expect(inspect('"')).to.equal(String.raw`"\""`);
3233
});
3334

0 commit comments

Comments
 (0)