Skip to content

Commit e9d56cf

Browse files
committed
Run tests against multiple versions of GraphQL.js
1 parent 41cac0e commit e9d56cf

File tree

8 files changed

+99
-55
lines changed

8 files changed

+99
-55
lines changed

__tests__/graphql-pr-4192.test.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { runTest } from "./runTest.mjs";
2+
3+
runTest("graphql-pr-4192");

__tests__/graphql15.test.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { runTest } from "./runTest.mjs";
2+
3+
runTest("graphql15");

__tests__/graphql16.test.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { runTest } from "./runTest.mjs";
2+
3+
runTest("graphql16");

__tests__/graphql17.test.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { runTest } from "./runTest.mjs";
2+
3+
runTest("graphql17");

__tests__/index.test.mjs

Lines changed: 0 additions & 54 deletions
This file was deleted.

__tests__/runTest.mjs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
};

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
},
1010
"scripts": {
1111
"prepack": "tsc && chmod +x dist/cli/*.js",
12-
"test": "node --test",
12+
"test": "node --test --experimental-test-module-mocks",
1313
"watch": "tsc --watch",
1414
"lint": "yarn prettier:check && eslint --ext .js,.jsx,.ts,.tsx,.graphql .",
1515
"lint:fix": "eslint --ext .js,.jsx,.ts,.tsx,.graphql . --fix; prettier --cache --ignore-path .eslintignore --write '**/*.{js,jsx,ts,tsx,graphql,md,json}'",
@@ -59,6 +59,9 @@
5959
"eslint-plugin-simple-import-sort": "^10.0.0",
6060
"eslint_d": "^13.0.0",
6161
"graphql-pr-4192": "npm:graphql@16.9.0-canary.pr.4192.1813397076f44a55e5798478e7321db9877de97a",
62+
"graphql15": "npm:[email protected]",
63+
"graphql16": "npm:[email protected]",
64+
"graphql17": "npm:[email protected]",
6265
"prettier": "^3.3.3",
6366
"typescript": "^5.6.2"
6467
},

yarn.lock

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,21 @@ graphemer@^1.4.0:
923923
resolved "https://registry.yarnpkg.com/graphql/-/graphql-16.9.0-canary.pr.4192.1813397076f44a55e5798478e7321db9877de97a.tgz#c5bbcdb258959b98352bcd9ea7f17790647113ae"
924924
integrity sha512-P8UYoxSUI1KGr9O5f+AMA3TuLYxOcELoQebxGrnVAIUHM6HCpiLDT+CylrBWEBmvcc7S0xRFRiwvgwzChzLTyQ==
925925

926+
"graphql15@npm:[email protected]":
927+
version "15.10.1"
928+
resolved "https://registry.yarnpkg.com/graphql/-/graphql-15.10.1.tgz#e9ff3bb928749275477f748b14aa5c30dcad6f2f"
929+
integrity sha512-BL/Xd/T9baO6NFzoMpiMD7YUZ62R6viR5tp/MULVEnbYJXZA//kRNW7J0j1w/wXArgL0sCxhDfK5dczSKn3+cg==
930+
931+
"graphql16@npm:[email protected]":
932+
version "16.10.0"
933+
resolved "https://registry.yarnpkg.com/graphql/-/graphql-16.10.0.tgz#24c01ae0af6b11ea87bf55694429198aaa8e220c"
934+
integrity sha512-AjqGKbDGUFRKIRCP9tCKiIGHyriz2oHEbPIbEtcSLSs4YjReZOIPQQWek4+6hjw62H9QShXHyaGivGiYVLeYFQ==
935+
936+
"graphql17@npm:[email protected]":
937+
version "17.0.0-alpha.8"
938+
resolved "https://registry.yarnpkg.com/graphql/-/graphql-17.0.0-alpha.8.tgz#dba4a0cbe3efe8243666726f1b4ffd65f87d9b06"
939+
integrity sha512-j9Jn56NCWVaLMt1hSNkMDoCuAisBwY3bxp/5tbrJuPtNtHg9dAf4NjKnlVDCksVP3jBVcipFaEXKWsdNxTlcyg==
940+
926941
"[email protected] | 16.x | 17.x":
927942
version "17.0.0-alpha.8"
928943
resolved "https://registry.yarnpkg.com/graphql/-/graphql-17.0.0-alpha.8.tgz#dba4a0cbe3efe8243666726f1b4ffd65f87d9b06"

0 commit comments

Comments
 (0)