Skip to content

Commit a745361

Browse files
GraphQLError: Add test to check order of fields in JSON.stringify (#3336)
1 parent dc1f7a5 commit a745361

File tree

1 file changed

+38
-20
lines changed

1 file changed

+38
-20
lines changed

src/error/__tests__/GraphQLError-test.ts

+38-20
Original file line numberDiff line numberDiff line change
@@ -144,29 +144,47 @@ describe('GraphQLError', () => {
144144
});
145145
});
146146

147-
it('serializes to include message', () => {
148-
const e = new GraphQLError('msg');
149-
expect(JSON.stringify(e)).to.equal('{"message":"msg"}');
150-
});
147+
it('serializes to include all standard fields', () => {
148+
const eShort = new GraphQLError('msg');
149+
expect(JSON.stringify(eShort, null, 2)).to.equal(dedent`
150+
{
151+
"message": "msg"
152+
}
153+
`);
151154

152-
it('serializes to include message and locations', () => {
153-
const e = new GraphQLError('msg', fieldNode);
154-
expect(JSON.stringify(e)).to.equal(
155-
'{"message":"msg","locations":[{"line":2,"column":3}]}',
155+
const path = ['path', 2, 'field'];
156+
const extensions = { foo: 'bar' };
157+
const eFull = new GraphQLError(
158+
'msg',
159+
fieldNode,
160+
undefined,
161+
undefined,
162+
path,
163+
undefined,
164+
extensions,
156165
);
157-
});
158166

159-
it('serializes to include path', () => {
160-
const e = new GraphQLError('msg', null, null, null, [
161-
'path',
162-
3,
163-
'to',
164-
'field',
165-
]);
166-
expect(e).to.have.deep.property('path', ['path', 3, 'to', 'field']);
167-
expect(JSON.stringify(e)).to.equal(
168-
'{"message":"msg","path":["path",3,"to","field"]}',
169-
);
167+
// We should try to keep order of fields stable
168+
// Changing it wouldn't be breaking change but will fail some tests in other libraries.
169+
expect(JSON.stringify(eFull, null, 2)).to.equal(dedent`
170+
{
171+
"message": "msg",
172+
"locations": [
173+
{
174+
"line": 2,
175+
"column": 3
176+
}
177+
],
178+
"path": [
179+
"path",
180+
2,
181+
"field"
182+
],
183+
"extensions": {
184+
"foo": "bar"
185+
}
186+
}
187+
`);
170188
});
171189
});
172190

0 commit comments

Comments
 (0)