Skip to content

Commit f497156

Browse files
authored
Schema-driven testing utility examples (#128)
1 parent 0fb9bb8 commit f497156

23 files changed

+14359
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ node_modules
55

66
apollo-server/v4/mern-stack/package-lock.json
77
**/.idea
8+
9+
.DS_Store
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module.exports = {
2+
root: true,
3+
env: { browser: true, es2020: true },
4+
extends: [
5+
"eslint:recommended",
6+
"plugin:@typescript-eslint/recommended",
7+
"plugin:react-hooks/recommended",
8+
"react-app/jest",
9+
],
10+
ignorePatterns: ["dist", ".eslintrc.cjs"],
11+
parser: "@typescript-eslint/parser",
12+
plugins: ["react-refresh"],
13+
rules: {
14+
"react-refresh/only-export-components": [
15+
"warn",
16+
{ allowConstantExport: true },
17+
],
18+
},
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import type { CodegenConfig } from "@graphql-codegen/cli";
2+
3+
const config: CodegenConfig = {
4+
hooks: {
5+
afterAllFileWrite: ["prettier --write"],
6+
},
7+
generates: {
8+
"./src/__generated__/resolvers-types.ts": {
9+
schema: "./schema.graphql",
10+
config: {
11+
useIndexSignature: true,
12+
},
13+
plugins: ["typescript", "typescript-resolvers"],
14+
},
15+
"./src/__generated__/api.ts": {
16+
schema: "./schema.graphql",
17+
documents: ["src/**/*.{ts,tsx}"],
18+
config: {
19+
avoidOptionals: {
20+
field: true,
21+
inputValue: false,
22+
object: false,
23+
defaultValue: false,
24+
},
25+
dedupeOperationSuffix: true,
26+
defaultScalarType: "unknown",
27+
nonOptionalTypename: true,
28+
omitOperationSuffix: true,
29+
skipTypeNameForRoot: true,
30+
scalars: {
31+
CountryCode: "string",
32+
DateTime: "string",
33+
ErrorRate: "number",
34+
ID: "string",
35+
Timestamp: "number",
36+
},
37+
namingConvention: {
38+
typeNames: "keep",
39+
},
40+
},
41+
plugins: ["typescript", "typescript-operations"],
42+
},
43+
},
44+
};
45+
46+
export default config;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Apollo Client: schema-driven testing example</title>
7+
</head>
8+
9+
<body>
10+
<div id="root"></div>
11+
<script type="module" src="/src/index.tsx"></script>
12+
</body>
13+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { Config } from "jest";
2+
3+
const config: Config = {
4+
globals: {
5+
"globalThis.__DEV__": JSON.stringify(true),
6+
},
7+
testEnvironment: "jsdom",
8+
setupFiles: ["./jest.polyfills.js"],
9+
setupFilesAfterEnv: ["<rootDir>/setupTests.ts"],
10+
// Opt out of the browser export condition for MSW tests
11+
// for more information, see: https://github.com/mswjs/msw/issues/1786#issuecomment-1782559851
12+
testEnvironmentOptions: {
13+
customExportConditions: [""],
14+
},
15+
transform: {
16+
"\\.(gql|graphql)$": "@graphql-tools/jest-transform",
17+
"^.+\\.tsx?$": [
18+
"ts-jest",
19+
{
20+
diagnostics: {
21+
warnOnly: process.env.TEST_ENV !== "ci",
22+
},
23+
},
24+
],
25+
},
26+
resolver: "ts-jest-resolver",
27+
};
28+
29+
export default config;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// jest.polyfills.js
2+
/**
3+
* @note The block below contains polyfills for Node.js globals
4+
* required for Jest to function when running JSDOM tests.
5+
* These HAVE to be require's and HAVE to be in this exact
6+
* order, since "undici" depends on the "TextEncoder" global API.
7+
*
8+
* Consider migrating to a more modern test runner if
9+
* you don't want to deal with this.
10+
*/
11+
12+
const { TextDecoder, TextEncoder } = require("node:util");
13+
const { ReadableStream } = require("node:stream/web");
14+
const { clearImmediate } = require("node:timers");
15+
const { performance } = require("node:perf_hooks");
16+
17+
Object.defineProperties(globalThis, {
18+
TextDecoder: { value: TextDecoder }, // jest
19+
TextEncoder: { value: TextEncoder }, // jest
20+
ReadableStream: { value: ReadableStream }, // jest
21+
performance: { value: performance },
22+
clearImmediate: { value: clearImmediate },
23+
});
24+
25+
const { Blob, File } = require("node:buffer");
26+
const { fetch, Headers, FormData, Request, Response } = require("undici");
27+
28+
Object.defineProperties(globalThis, {
29+
fetch: { value: fetch, writable: true }, // jest
30+
Blob: { value: Blob },
31+
File: { value: File },
32+
Headers: { value: Headers },
33+
FormData: { value: FormData },
34+
Request: { value: Request },
35+
Response: { value: Response }, // jest
36+
});
37+
38+
// Symbol.dispose is not defined
39+
// jest bug: https://github.com/jestjs/jest/issues/14874
40+
// fix is available in https://github.com/jestjs/jest/releases/tag/v30.0.0-alpha.3
41+
if (!Symbol.dispose) {
42+
Object.defineProperty(Symbol, "dispose", {
43+
value: Symbol("dispose"),
44+
});
45+
}
46+
if (!Symbol.asyncDispose) {
47+
Object.defineProperty(Symbol, "asyncDispose", {
48+
value: Symbol("asyncDispose"),
49+
});
50+
}

0 commit comments

Comments
 (0)