Skip to content

Commit 8649199

Browse files
authored
Merge pull request #636 from dackroyd/refactor-validation-test-json
Refactor Validation Tests JSON
2 parents 6029c30 + 78dd5ce commit 8649199

File tree

3 files changed

+325
-271
lines changed

3 files changed

+325
-271
lines changed

internal/validation/testdata/export.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as fs from 'node:fs';
2+
import { createHash } from 'crypto';
23
import { printSchema } from 'graphql/src/utilities/printSchema.js';
34
import { schemas, testCases } from 'graphql/src/validation/__tests__/harness.js';
45

@@ -36,10 +37,31 @@ import 'graphql/src/validation/__tests__/VariablesAreInputTypesRule-test.js';
3637
// require('graphql/src/validation/__tests__/VariablesDefaultValueAllowed-test');
3738
import 'graphql/src/validation/__tests__/VariablesInAllowedPositionRule-test.js';
3839

39-
let output = JSON.stringify({
40-
schemas: schemas().map(s => printSchema(s)),
41-
tests: testCases(),
42-
}, null, 2)
40+
// Schema index in the source array can be unstable, as its dependent on the order they are used in the registered test
41+
// files. The SHA256 of the schema will change if there are any changes to the content, but is a better reference than
42+
// the schema indexes all changing when a new schema is inserted.
43+
let s = schemas().map(s => {
44+
const sdl = printSchema(s)
45+
const id = createHash('sha256').update(sdl).digest('base64');
46+
const v: { id: string; sdl: string; } = {id: id, sdl: sdl};
47+
return v;
48+
});
49+
50+
const tests = testCases().map(c => {
51+
const schema = s[c.schema];
52+
return {
53+
name: c.name,
54+
rule: c.rule,
55+
schema: schema.id,
56+
query: c.query,
57+
errors: c.errors,
58+
}
59+
});
60+
61+
// Order based on the schema string to provide semi-stable ordering
62+
s = s.sort((a, b) => a.sdl.localeCompare(b.sdl));
63+
64+
let output = JSON.stringify({schemas: s, tests: tests}, null, 2)
4365
output = output.replace(/ Did you mean to use an inline fragment on [^?]*\?/g, '');
4466
// Ignore suggested types in errors, which graphql-go does not currently produce.
4567
output = output.replace(/ Did you mean \\"[A-Z].*\"\?/g, '');

0 commit comments

Comments
 (0)