forked from captbaritone/grats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ts
283 lines (253 loc) · 8.11 KB
/
test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import * as path from "path";
import TestRunner from "./TestRunner";
import {
buildSchemaAndDocResult,
buildSchemaAndDocResultWithHost,
} from "../lib";
import * as ts from "typescript";
import {
buildASTSchema,
graphql,
GraphQLSchema,
print,
printSchema,
specifiedScalarTypes,
} from "graphql";
import { Command } from "commander";
import { locate } from "../Locate";
import { gqlErr, ReportableDiagnostics } from "../utils/DiagnosticError";
import { writeFileSync } from "fs";
import { codegen } from "../codegen";
import { diff } from "jest-diff";
import { METADATA_DIRECTIVE_NAMES } from "../metadataDirectives";
import * as semver from "semver";
import {
ConfigOptions,
ParsedCommandLineGrats,
validateGratsOptions,
} from "../gratsConfig";
import { SEMANTIC_NON_NULL_DIRECTIVE } from "../publicDirectives";
import { applySDLHeader, applyTypeScriptHeader } from "../printSchema";
const TS_VERSION = ts.version;
const program = new Command();
program
.name("grats-tests")
.description("Run Grats' internal tests")
.option(
"-w, --write",
"Write the actual output of the test to the expected output files. Useful for updating tests.",
)
.option(
"-f, --filter <FILTER_REGEX>",
"A regex to filter the tests to run. Only tests with a file path matching the regex will be run.",
)
.action(async ({ filter, write }) => {
const filterRegex = filter ?? null;
let failures = false;
for (const {
fixturesDir,
transformer,
testFilePattern,
ignoreFilePattern,
} of testDirs) {
const runner = new TestRunner(
fixturesDir,
!!write,
filterRegex,
testFilePattern,
ignoreFilePattern,
transformer,
);
failures = !(await runner.run()) || failures;
}
if (failures) {
process.exit(1);
}
});
const gratsDir = path.join(__dirname, "../..");
const fixturesDir = path.join(__dirname, "fixtures");
const integrationFixturesDir = path.join(__dirname, "integrationFixtures");
const testDirs = [
{
fixturesDir,
testFilePattern: /\.ts$/,
ignoreFilePattern: null,
transformer: (code: string, fileName: string): string | false => {
const firstLine = code.split("\n")[0];
let options: Partial<ConfigOptions> = {
nullableByDefault: true,
schemaHeader: null,
tsSchemaHeader: null,
};
if (firstLine.startsWith("// {")) {
const json = firstLine.slice(3);
const { tsVersion, ...testOptions } = JSON.parse(json);
if (tsVersion != null && !semver.satisfies(TS_VERSION, tsVersion)) {
console.log(
"Skipping test because TS version doesn't match",
tsVersion,
"does not match",
TS_VERSION,
);
return false;
}
options = { ...options, ...testOptions };
}
const files = [
`${fixturesDir}/${fileName}`,
path.join(__dirname, `../Types.ts`),
];
let parsedOptions: ParsedCommandLineGrats;
try {
parsedOptions = validateGratsOptions({
options: {},
raw: {
grats: options,
},
errors: [],
fileNames: files,
});
} catch (e) {
return e.message;
}
// https://stackoverflow.com/a/66604532/1263117
const compilerHost = ts.createCompilerHost(
parsedOptions.options,
/* setParentNodes this is needed for finding jsDocs */
true,
);
const schemaResult = buildSchemaAndDocResultWithHost(
parsedOptions,
compilerHost,
);
if (schemaResult.kind === "ERROR") {
return schemaResult.err.formatDiagnosticsWithContext();
}
const { schema, doc } = schemaResult.value;
// We run codegen here just ensure that it doesn't throw.
const executableSchema = applyTypeScriptHeader(
parsedOptions.raw.grats,
codegen(schema, `${fixturesDir}/${fileName}`),
);
const LOCATION_REGEX = /^\/\/ Locate: (.*)/;
const locationMatch = code.match(LOCATION_REGEX);
if (locationMatch != null) {
const locResult = locate(schema, locationMatch[1].trim());
if (locResult.kind === "ERROR") {
return locResult.err;
}
return new ReportableDiagnostics(compilerHost, [
gqlErr(locResult.value, "Located here"),
]).formatDiagnosticsWithContext();
} else {
const docSansDirectives = {
...doc,
definitions: doc.definitions.filter((def) => {
if (def.kind === "DirectiveDefinition") {
return !METADATA_DIRECTIVE_NAMES.has(def.name.value);
}
if (def.kind === "ScalarTypeDefinition") {
return !specifiedScalarTypes.some(
(scalar) => scalar.name === def.name.value,
);
}
return true;
}),
};
const sdl = applySDLHeader(
parsedOptions.raw.grats,
print(docSansDirectives),
);
return `-- SDL --\n${sdl}\n-- TypeScript --\n${executableSchema}`;
}
},
},
{
fixturesDir: integrationFixturesDir,
testFilePattern: /index.ts$/,
ignoreFilePattern: /schema.ts$/,
transformer: async (
code: string,
fileName: string,
): Promise<string | false> => {
const firstLine = code.split("\n")[0];
let options: Partial<ConfigOptions> = {
nullableByDefault: true,
};
if (firstLine.startsWith("// {")) {
const json = firstLine.slice(3);
const testOptions = JSON.parse(json);
options = { ...options, ...testOptions };
}
const filePath = `${integrationFixturesDir}/${fileName}`;
const schemaPath = path.join(path.dirname(filePath), "schema.ts");
const files = [filePath, path.join(__dirname, `../Types.ts`)];
const parsedOptions: ParsedCommandLineGrats = validateGratsOptions({
options: {
// Required to enable ts-node to locate function exports
rootDir: gratsDir,
outDir: "dist",
configFilePath: "tsconfig.json",
},
raw: {
grats: options,
},
errors: [],
fileNames: files,
});
const schemaResult = buildSchemaAndDocResult(parsedOptions);
if (schemaResult.kind === "ERROR") {
throw new Error(schemaResult.err.formatDiagnosticsWithContext());
}
const { schema, doc } = schemaResult.value;
const tsSchema = codegen(schema, schemaPath);
writeFileSync(schemaPath, tsSchema);
const server = await import(filePath);
if (server.query == null || typeof server.query !== "string") {
throw new Error(
`Expected \`${filePath}\` to export a query text as \`query\``,
);
}
const schemaModule = await import(schemaPath);
const actualSchema = schemaModule.getSchema();
const schemaDiff = compareSchemas(actualSchema, buildASTSchema(doc));
if (schemaDiff) {
console.log(schemaDiff);
// TODO: Make this an actual test failure, not an error
throw new Error("The codegen schema does not match the SDL schema.");
}
const data = await graphql({
schema: actualSchema,
source: server.query,
variableValues: server.variables,
});
return JSON.stringify(data, null, 2);
},
},
];
// Returns null if the schemas are equal, otherwise returns a string diff.
function compareSchemas(
actual: GraphQLSchema,
expected: GraphQLSchema,
): string | null {
const actualSDL = printSDLFromSchemaWithoutDirectives(actual);
const expectedSDL = printSDLFromSchemaWithoutDirectives(expected);
if (actualSDL === expectedSDL) {
return null;
}
return diff(expectedSDL, actualSDL);
}
function printSDLFromSchemaWithoutDirectives(schema: GraphQLSchema): string {
return printSchema(
new GraphQLSchema({
...schema.toConfig(),
directives: schema.getDirectives().filter((directive) => {
return (
!METADATA_DIRECTIVE_NAMES.has(directive.name) &&
directive.name !== SEMANTIC_NON_NULL_DIRECTIVE
);
}),
}),
);
}
program.parse();