Skip to content

Commit 6662869

Browse files
committed
use expectJSON and other usual testing shorthands
1 parent 77ffcf2 commit 6662869

File tree

1 file changed

+74
-128
lines changed

1 file changed

+74
-128
lines changed

src/execution/__tests__/semantic-nullability-test.ts

+74-128
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { expect } from 'chai';
21
import { describe, it } from 'mocha';
32

4-
import { GraphQLError } from '../../error/GraphQLError';
3+
import { expectJSON } from '../../__testUtils__/expectJSON';
4+
5+
import type { ObjMap } from '../../jsutils/ObjMap';
56

6-
import type { ExecutableDefinitionNode, FieldNode } from '../../language/ast';
77
import { parse } from '../../language/parser';
88

99
import {
@@ -34,169 +34,124 @@ describe('Execute: Handles Semantic Nullability', () => {
3434
}),
3535
});
3636

37+
const schema = new GraphQLSchema({
38+
useSemanticNullability: true,
39+
query: DataType,
40+
});
41+
42+
function executeWithSemanticNullability(
43+
query: string,
44+
rootValue: ObjMap<unknown>,
45+
) {
46+
return execute({
47+
schema,
48+
document: parse(query),
49+
rootValue,
50+
});
51+
}
52+
3753
it('SemanticNonNull throws error on null without error', async () => {
3854
const data = {
39-
a: () => 'Apple',
4055
b: () => null,
41-
c: () => 'Cookie',
4256
};
4357

44-
const document = parse(`
45-
query {
46-
b
47-
}
48-
`);
49-
50-
const result = await execute({
51-
schema: new GraphQLSchema({
52-
useSemanticNullability: true,
53-
query: DataType,
54-
}),
55-
document,
56-
rootValue: data,
57-
});
58+
const query = `
59+
query {
60+
b
61+
}
62+
`;
5863

59-
const executable = document.definitions?.values().next()
60-
.value as ExecutableDefinitionNode;
61-
const selectionSet = executable.selectionSet.selections
62-
.values()
63-
.next().value;
64+
const result = await executeWithSemanticNullability(query, data);
6465

65-
expect(result).to.deep.equal({
66+
expectJSON(result).toDeepEqual({
6667
data: {
6768
b: null,
6869
},
6970
errors: [
70-
new GraphQLError(
71-
'Cannot return null for semantic-non-nullable field DataType.b.',
72-
{
73-
nodes: selectionSet,
74-
path: ['b'],
75-
},
76-
),
71+
{
72+
message:
73+
'Cannot return null for semantic-non-nullable field DataType.b.',
74+
path: ['b'],
75+
locations: [{ line: 3, column: 9 }],
76+
},
7777
],
7878
});
7979
});
8080

8181
it('SemanticNonNull succeeds on null with error', async () => {
8282
const data = {
83-
a: () => 'Apple',
8483
b: () => {
8584
throw new Error('Something went wrong');
8685
},
87-
c: () => 'Cookie',
8886
};
8987

90-
const document = parse(`
91-
query {
92-
b
93-
}
94-
`);
95-
96-
const executable = document.definitions?.values().next()
97-
.value as ExecutableDefinitionNode;
98-
const selectionSet = executable.selectionSet.selections
99-
.values()
100-
.next().value;
101-
102-
const result = await execute({
103-
schema: new GraphQLSchema({
104-
useSemanticNullability: true,
105-
query: DataType,
106-
}),
107-
document,
108-
rootValue: data,
109-
});
88+
const query = `
89+
query {
90+
b
91+
}
92+
`;
93+
94+
const result = await executeWithSemanticNullability(query, data);
11095

111-
expect(result).to.deep.equal({
96+
expectJSON(result).toDeepEqual({
11297
data: {
11398
b: null,
11499
},
115100
errors: [
116-
new GraphQLError('Something went wrong', {
117-
nodes: selectionSet,
101+
{
102+
message: 'Something went wrong',
118103
path: ['b'],
119-
}),
104+
locations: [{ line: 3, column: 9 }],
105+
},
120106
],
121107
});
122108
});
123109

124110
it('SemanticNonNull halts null propagation', async () => {
125-
const deepData = {
126-
f: () => null,
127-
};
128-
129111
const data = {
130-
a: () => 'Apple',
131-
b: () => null,
132-
c: () => 'Cookie',
133-
d: () => deepData,
112+
d: () => ({
113+
f: () => null,
114+
}),
134115
};
135116

136-
const document = parse(`
137-
query {
138-
d {
139-
f
140-
}
117+
const query = `
118+
query {
119+
d {
120+
f
141121
}
142-
`);
143-
144-
const result = await execute({
145-
schema: new GraphQLSchema({
146-
useSemanticNullability: true,
147-
query: DataType,
148-
}),
149-
document,
150-
rootValue: data,
151-
});
122+
}
123+
`;
152124

153-
const executable = document.definitions?.values().next()
154-
.value as ExecutableDefinitionNode;
155-
const dSelectionSet = executable.selectionSet.selections.values().next()
156-
.value as FieldNode;
157-
const fSelectionSet = dSelectionSet.selectionSet?.selections
158-
.values()
159-
.next().value;
125+
const result = await executeWithSemanticNullability(query, data);
160126

161-
expect(result).to.deep.equal({
127+
expectJSON(result).toDeepEqual({
162128
data: {
163129
d: null,
164130
},
165131
errors: [
166-
new GraphQLError(
167-
'Cannot return null for non-nullable field DeepDataType.f.',
168-
{
169-
nodes: fSelectionSet,
170-
path: ['d', 'f'],
171-
},
172-
),
132+
{
133+
message: 'Cannot return null for non-nullable field DeepDataType.f.',
134+
path: ['d', 'f'],
135+
locations: [{ line: 4, column: 11 }],
136+
},
173137
],
174138
});
175139
});
176140

177141
it('SemanticNullable allows null values', async () => {
178142
const data = {
179143
a: () => null,
180-
b: () => null,
181-
c: () => 'Cookie',
182144
};
183145

184-
const document = parse(`
185-
query {
186-
a
187-
}
188-
`);
146+
const query = `
147+
query {
148+
a
149+
}
150+
`;
189151

190-
const result = await execute({
191-
schema: new GraphQLSchema({
192-
useSemanticNullability: true,
193-
query: DataType,
194-
}),
195-
document,
196-
rootValue: data,
197-
});
152+
const result = await executeWithSemanticNullability(query, data);
198153

199-
expect(result).to.deep.equal({
154+
expectJSON(result).toDeepEqual({
200155
data: {
201156
a: null,
202157
},
@@ -206,26 +161,17 @@ describe('Execute: Handles Semantic Nullability', () => {
206161
it('SemanticNullable allows non-null values', async () => {
207162
const data = {
208163
a: () => 'Apple',
209-
b: () => null,
210-
c: () => 'Cookie',
211164
};
212165

213-
const document = parse(`
214-
query {
215-
a
216-
}
217-
`);
166+
const query = `
167+
query {
168+
a
169+
}
170+
`;
218171

219-
const result = await execute({
220-
schema: new GraphQLSchema({
221-
useSemanticNullability: true,
222-
query: DataType,
223-
}),
224-
document,
225-
rootValue: data,
226-
});
172+
const result = await executeWithSemanticNullability(query, data);
227173

228-
expect(result).to.deep.equal({
174+
expectJSON(result).toDeepEqual({
229175
data: {
230176
a: 'Apple',
231177
},

0 commit comments

Comments
 (0)