Skip to content

Commit 5996bce

Browse files
committed
Include tests for graphqlSync
1 parent 810a05d commit 5996bce

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

src/execution/__tests__/sync-test.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import { expect } from 'chai';
99
import { describe, it } from 'mocha';
10+
import { graphqlSync } from '../../graphql';
1011
import { execute } from '../execute';
1112
import { parse } from '../../language';
1213
import { GraphQLSchema, GraphQLObjectType, GraphQLString } from '../../type';
@@ -72,4 +73,64 @@ describe('Execute: synchronously when possible', () => {
7273
data: { syncField: 'rootValue', asyncField: 'rootValue' },
7374
});
7475
});
76+
77+
describe('graphqlSync', () => {
78+
it('does not return a Promise for syntax errors', () => {
79+
const doc = 'fragment Example on Query { { { syncField }';
80+
const result = graphqlSync({
81+
schema,
82+
source: doc,
83+
});
84+
expect(result).to.containSubset({
85+
errors: [
86+
{
87+
message:
88+
'Syntax Error GraphQL request (1:29) Expected Name, found {\n\n' +
89+
'1: fragment Example on Query { { { syncField }\n' +
90+
' ^\n',
91+
locations: [{ line: 1, column: 29 }],
92+
},
93+
],
94+
});
95+
});
96+
97+
it('does not return a Promise for validation errors', () => {
98+
const doc = 'fragment Example on Query { unknownField }';
99+
const result = graphqlSync({
100+
schema,
101+
source: doc,
102+
});
103+
expect(result).to.containSubset({
104+
errors: [
105+
{
106+
message:
107+
'Cannot query field "unknownField" on type "Query". Did you ' +
108+
'mean "syncField" or "asyncField"?',
109+
locations: [{ line: 1, column: 29 }],
110+
},
111+
],
112+
});
113+
});
114+
115+
it('does not return a Promise for sync execution', () => {
116+
const doc = 'query Example { syncField }';
117+
const result = graphqlSync({
118+
schema,
119+
source: doc,
120+
rootValue: 'rootValue',
121+
});
122+
expect(result).to.deep.equal({ data: { syncField: 'rootValue' } });
123+
});
124+
125+
it('throws if encountering async execution', () => {
126+
const doc = 'query Example { syncField, asyncField }';
127+
expect(() => {
128+
graphqlSync({
129+
schema,
130+
source: doc,
131+
rootValue: 'rootValue',
132+
});
133+
}).to.throw('GraphQL execution failed to complete synchronously.');
134+
});
135+
});
75136
});

0 commit comments

Comments
 (0)