|
| 1 | +// @ts-check |
| 2 | + |
| 3 | +import * as assert from "node:assert"; |
| 4 | +import { readdir, readFile } from "node:fs/promises"; |
| 5 | +import { test } from "node:test"; |
| 6 | + |
| 7 | +const TEST_DIR = import.meta.dirname; |
| 8 | +const files = await readdir(TEST_DIR); |
| 9 | +const skip = test.skip.bind(test); |
| 10 | + |
| 11 | +/** @param graphqlModuleName {string} */ |
| 12 | +export const runTest = async (graphqlModuleName) => { |
| 13 | + test(graphqlModuleName, async (t) => { |
| 14 | + const mod = await import(graphqlModuleName); |
| 15 | + const { default: defaultExport, ...namedExports } = mod; |
| 16 | + const mockGraphql = t.mock.module("graphql", { |
| 17 | + cache: true, |
| 18 | + defaultExport, |
| 19 | + namedExports, |
| 20 | + }); |
| 21 | + const graphql = await import("graphql"); |
| 22 | + const { buildSchema, printSchema } = graphql; |
| 23 | + const isSemanticNonNullType = /** @type {any} */ (graphql) |
| 24 | + .isSemanticNonNullType; |
| 25 | + |
| 26 | + const { semanticToNullable, semanticToStrict } = await import( |
| 27 | + `../dist/index.js?graphql=${graphqlModuleName}` |
| 28 | + ); |
| 29 | + |
| 30 | + for (const file of files) { |
| 31 | + if (file.endsWith(".test.graphql") && !file.startsWith(".")) { |
| 32 | + const pureDirective = |
| 33 | + file === "schema-with-directive-only.test.graphql"; |
| 34 | + const maybeTest = |
| 35 | + pureDirective || isSemanticNonNullType != null ? test : skip; |
| 36 | + await maybeTest(file.replace(/\.test\.graphql$/, ""), async () => { |
| 37 | + const sdl = await readFile(TEST_DIR + "/" + file, "utf8"); |
| 38 | + const schema = buildSchema(sdl); |
| 39 | + await test("semantic-to-strict", async () => { |
| 40 | + const expectedSdl = await readFile( |
| 41 | + TEST_DIR + "/snapshots/" + file.replace(".test.", ".strict."), |
| 42 | + "utf8", |
| 43 | + ); |
| 44 | + const converted = semanticToStrict(schema); |
| 45 | + assert.equal( |
| 46 | + printSchema(converted).trim(), |
| 47 | + expectedSdl.trim(), |
| 48 | + "Expected semantic-to-strict to match", |
| 49 | + ); |
| 50 | + }); |
| 51 | + await test("semantic-to-nullable", async () => { |
| 52 | + const expectedSdl = await readFile( |
| 53 | + TEST_DIR + "/snapshots/" + file.replace(".test.", ".nullable."), |
| 54 | + "utf8", |
| 55 | + ); |
| 56 | + const converted = semanticToNullable(schema); |
| 57 | + assert.equal( |
| 58 | + printSchema(converted).trim(), |
| 59 | + expectedSdl.trim(), |
| 60 | + "Expected semantic-to-nullable to match", |
| 61 | + ); |
| 62 | + }); |
| 63 | + }); |
| 64 | + } |
| 65 | + } |
| 66 | + mockGraphql.restore(); |
| 67 | + }); |
| 68 | +}; |
0 commit comments