Skip to content

Commit 642eeaf

Browse files
committed
Extract 'formatError' tests into separate file + improve coverage
1 parent 28511bf commit 642eeaf

File tree

2 files changed

+60
-29
lines changed

2 files changed

+60
-29
lines changed

src/error/__tests__/GraphQLError-test.js

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { Kind } from '../../language/kinds';
1010
import { parse } from '../../language/parser';
1111
import { Source } from '../../language/source';
1212

13-
import { formatError } from '../formatError';
1413
import { GraphQLError, printError } from '../GraphQLError';
1514

1615
const source = new Source(dedent`
@@ -132,34 +131,6 @@ describe('GraphQLError', () => {
132131
'{"message":"msg","path":["path",3,"to","field"]}',
133132
);
134133
});
135-
136-
it('default error formatter includes path', () => {
137-
const e = new GraphQLError('msg', null, null, null, [
138-
'path',
139-
3,
140-
'to',
141-
'field',
142-
]);
143-
144-
expect(formatError(e)).to.deep.equal({
145-
message: 'msg',
146-
locations: undefined,
147-
path: ['path', 3, 'to', 'field'],
148-
});
149-
});
150-
151-
it('default error formatter includes extension fields', () => {
152-
const e = new GraphQLError('msg', null, null, null, null, null, {
153-
foo: 'bar',
154-
});
155-
156-
expect(formatError(e)).to.deep.equal({
157-
message: 'msg',
158-
locations: undefined,
159-
path: undefined,
160-
extensions: { foo: 'bar' },
161-
});
162-
});
163134
});
164135

165136
describe('printError', () => {
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// @flow strict
2+
3+
import { expect } from 'chai';
4+
import { describe, it } from 'mocha';
5+
6+
import { formatError } from '../formatError';
7+
import { GraphQLError } from '../GraphQLError';
8+
9+
describe('formatError: default error formatter', () => {
10+
it('uses default message', () => {
11+
// $DisableFlowOnNegativeTest
12+
const e = new GraphQLError();
13+
14+
expect(formatError(e)).to.deep.equal({
15+
message: 'An unknown error occurred.',
16+
path: undefined,
17+
locations: undefined,
18+
});
19+
});
20+
21+
it('includes path', () => {
22+
const e = new GraphQLError('msg', null, null, null, [
23+
'path',
24+
3,
25+
'to',
26+
'field',
27+
]);
28+
29+
expect(formatError(e)).to.deep.equal({
30+
message: 'msg',
31+
locations: undefined,
32+
path: ['path', 3, 'to', 'field'],
33+
});
34+
});
35+
36+
it('includes extension fields', () => {
37+
const e = new GraphQLError('msg', null, null, null, null, null, {
38+
foo: 'bar',
39+
});
40+
41+
expect(formatError(e)).to.deep.equal({
42+
message: 'msg',
43+
locations: undefined,
44+
path: undefined,
45+
extensions: { foo: 'bar' },
46+
});
47+
});
48+
49+
it('rejects null and undefined errors', () => {
50+
// $DisableFlowOnNegativeTest
51+
expect(() => formatError(undefined)).to.throw(
52+
'Received null or undefined error.',
53+
);
54+
55+
// $DisableFlowOnNegativeTest
56+
expect(() => formatError(null)).to.throw(
57+
'Received null or undefined error.',
58+
);
59+
});
60+
});

0 commit comments

Comments
 (0)