Skip to content

Commit 0762193

Browse files
ValuesOfCorrectType: improve coverage (#2351)
1 parent 34c5580 commit 0762193

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

src/validation/__tests__/ValuesOfCorrectType-test.js

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,29 @@
22

33
import { describe, it } from 'mocha';
44

5+
import { GraphQLSchema } from '../../type/schema';
6+
import { GraphQLString } from '../../type/scalars';
7+
import { GraphQLScalarType, GraphQLObjectType } from '../../type/definition';
8+
59
import { ValuesOfCorrectType } from '../rules/ValuesOfCorrectType';
610

7-
import { expectValidationErrors } from './harness';
11+
import {
12+
expectValidationErrors,
13+
expectValidationErrorsWithSchema,
14+
} from './harness';
815

916
function expectErrors(queryStr) {
1017
return expectValidationErrors(ValuesOfCorrectType, queryStr);
1118
}
1219

20+
function expectErrorsWithSchema(schema, queryStr) {
21+
return expectValidationErrorsWithSchema(
22+
schema,
23+
ValuesOfCorrectType,
24+
queryStr,
25+
);
26+
}
27+
1328
function expectValid(queryStr) {
1429
expectErrors(queryStr).to.deep.equal([]);
1530
}
@@ -942,6 +957,45 @@ describe('Validate: Values of correct type', () => {
942957
);
943958
});
944959

960+
it('reports error for custom scalar that returns undefined', () => {
961+
const customScalar = new GraphQLScalarType({
962+
name: 'CustomScalar',
963+
parseValue() {
964+
return undefined;
965+
},
966+
});
967+
968+
const schema = new GraphQLSchema({
969+
query: new GraphQLObjectType({
970+
name: 'Query',
971+
fields: {
972+
invalidArg: {
973+
type: GraphQLString,
974+
args: {
975+
arg: { type: customScalar },
976+
},
977+
},
978+
},
979+
}),
980+
});
981+
982+
const expectedErrors = expectErrorsWithSchema(
983+
schema,
984+
`
985+
{
986+
invalidArg(arg: 123)
987+
}
988+
`,
989+
);
990+
991+
expectedErrors.to.deep.equal([
992+
{
993+
message: 'Expected value of type "CustomScalar", found 123.',
994+
locations: [{ line: 3, column: 27 }],
995+
},
996+
]);
997+
});
998+
945999
it('allows custom scalar to accept complex literals', () => {
9461000
expectValid(`
9471001
{

0 commit comments

Comments
 (0)