Skip to content

Commit e2dd4c9

Browse files
authored
Schema-driven testing utilities (#11605)
1 parent 78891f9 commit e2dd4c9

19 files changed

+1864
-20
lines changed

.api-reports/api-report-testing.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
55
```ts
66

7+
/// <reference types="node" />
8+
79
import type { ASTNode } from 'graphql';
810
import type { DocumentNode } from 'graphql';
911
import type { ExecutionResult } from 'graphql';
1012
import type { FieldNode } from 'graphql';
1113
import type { FragmentDefinitionNode } from 'graphql';
1214
import type { GraphQLError } from 'graphql';
1315
import type { GraphQLErrorExtensions } from 'graphql';
16+
import type { GraphQLSchema } from 'graphql';
1417
import { Observable } from 'zen-observable-ts';
1518
import type { Observer } from 'zen-observable-ts';
1619
import * as React_2 from 'react';
@@ -446,6 +449,24 @@ type ConcastSourcesIterable<T> = Iterable<Source<T>>;
446449
// @public (undocumented)
447450
export function createMockClient<TData>(data: TData, query: DocumentNode, variables?: {}): ApolloClient<NormalizedCacheObject>;
448451

452+
// @alpha
453+
export const createMockFetch: (schema: GraphQLSchema, mockFetchOpts?: {
454+
validate: boolean;
455+
}) => {
456+
mock: (uri: any, options: any) => Promise<Response>;
457+
restore: () => void;
458+
} & Disposable;
459+
460+
// @alpha
461+
export const createMockSchema: (staticSchema: GraphQLSchema, mocks: {
462+
[key: string]: any;
463+
}) => GraphQLSchema;
464+
465+
// Warning: (ae-forgotten-export) The symbol "ProxiedSchema" needs to be exported by the entry point index.d.ts
466+
//
467+
// @alpha
468+
export const createProxiedSchema: (schemaWithMocks: GraphQLSchema, resolvers: Resolvers) => ProxiedSchema;
469+
449470
// @public (undocumented)
450471
namespace DataProxy {
451472
// (undocumented)
@@ -1265,6 +1286,25 @@ type Path = ReadonlyArray<string | number>;
12651286
// @public (undocumented)
12661287
type Primitive = null | undefined | string | number | boolean | symbol | bigint;
12671288

1289+
// Warning: (ae-forgotten-export) The symbol "ProxiedSchemaFns" needs to be exported by the entry point index.d.ts
1290+
//
1291+
// @public (undocumented)
1292+
type ProxiedSchema = GraphQLSchema & ProxiedSchemaFns;
1293+
1294+
// @public (undocumented)
1295+
interface ProxiedSchemaFns {
1296+
// (undocumented)
1297+
add: (addOptions: {
1298+
resolvers: Resolvers;
1299+
}) => ProxiedSchema;
1300+
// (undocumented)
1301+
fork: (forkOptions?: {
1302+
resolvers?: Resolvers;
1303+
}) => ProxiedSchema;
1304+
// (undocumented)
1305+
reset: () => void;
1306+
}
1307+
12681308
// @public (undocumented)
12691309
class QueryInfo {
12701310
constructor(queryManager: QueryManager<any>, queryId?: string);

.api-reports/api-report-testing_core.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
55
```ts
66

7+
/// <reference types="node" />
8+
79
import type { ASTNode } from 'graphql';
810
import type { DocumentNode } from 'graphql';
911
import type { ExecutionResult } from 'graphql';
1012
import type { FieldNode } from 'graphql';
1113
import type { FragmentDefinitionNode } from 'graphql';
1214
import type { GraphQLError } from 'graphql';
1315
import type { GraphQLErrorExtensions } from 'graphql';
16+
import type { GraphQLSchema } from 'graphql';
1417
import { Observable } from 'zen-observable-ts';
1518
import type { Observer } from 'zen-observable-ts';
1619
import type { Subscriber } from 'zen-observable-ts';
@@ -445,6 +448,19 @@ type ConcastSourcesIterable<T> = Iterable<Source<T>>;
445448
// @public (undocumented)
446449
export function createMockClient<TData>(data: TData, query: DocumentNode, variables?: {}): ApolloClient<NormalizedCacheObject>;
447450

451+
// @alpha
452+
export const createMockFetch: (schema: GraphQLSchema, mockFetchOpts?: {
453+
validate: boolean;
454+
}) => {
455+
mock: (uri: any, options: any) => Promise<Response>;
456+
restore: () => void;
457+
} & Disposable;
458+
459+
// Warning: (ae-forgotten-export) The symbol "ProxiedSchema" needs to be exported by the entry point index.d.ts
460+
//
461+
// @alpha
462+
export const createProxiedSchema: (schemaWithMocks: GraphQLSchema, resolvers: Resolvers) => ProxiedSchema;
463+
448464
// @public (undocumented)
449465
namespace DataProxy {
450466
// (undocumented)
@@ -1220,6 +1236,25 @@ type Path = ReadonlyArray<string | number>;
12201236
// @public (undocumented)
12211237
type Primitive = null | undefined | string | number | boolean | symbol | bigint;
12221238

1239+
// Warning: (ae-forgotten-export) The symbol "ProxiedSchemaFns" needs to be exported by the entry point index.d.ts
1240+
//
1241+
// @public (undocumented)
1242+
type ProxiedSchema = GraphQLSchema & ProxiedSchemaFns;
1243+
1244+
// @public (undocumented)
1245+
interface ProxiedSchemaFns {
1246+
// (undocumented)
1247+
add: (addOptions: {
1248+
resolvers: Resolvers;
1249+
}) => ProxiedSchema;
1250+
// (undocumented)
1251+
fork: (forkOptions?: {
1252+
resolvers?: Resolvers;
1253+
}) => ProxiedSchema;
1254+
// (undocumented)
1255+
reset: () => void;
1256+
}
1257+
12231258
// @public (undocumented)
12241259
class QueryInfo {
12251260
constructor(queryManager: QueryManager<any>, queryId?: string);

.changeset/chatty-llamas-switch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@apollo/client": minor
3+
---
4+
5+
Adds `createMockFetch` utility for integration testing that includes the link chain

.changeset/stupid-bears-cheat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@apollo/client": minor
3+
---
4+
5+
Adds proxiedSchema and createMockSchema testing utilities

.size-limits.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"dist/apollo-client.min.cjs": 39512,
3-
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32801
2+
"dist/apollo-client.min.cjs": 39506,
3+
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32793
44
}

.vscode/launch.json

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
{
22
"version": "0.2.0",
33
"configurations": [
4-
{
5-
"name": "Attach to Node.js inspector",
6-
"port": 9229,
7-
"request": "attach",
8-
"skipFiles": ["<node_internals>/**"],
9-
"type": "pwa-node"
10-
},
114
{
125
"type": "node",
136
"request": "launch",
14-
"name": "Jest Current File",
15-
"program": "${workspaceFolder}/node_modules/.bin/jest",
16-
"args": ["${relativeFile}", "--config", "./config/jest.config.js"],
7+
"name": "Jest Attach Node Inspector for Current File",
8+
"cwd": "${workspaceFolder}",
9+
"runtimeArgs": [
10+
"--inspect-brk",
11+
"${workspaceRoot}/node_modules/.bin/jest",
12+
"${relativeFile}",
13+
"--config",
14+
"./config/jest.config.js",
15+
"--runInBand",
16+
"--watch"
17+
],
1718
"console": "integratedTerminal",
18-
"internalConsoleOptions": "neverOpen",
19-
"disableOptimisticBPs": true,
20-
"windows": {
21-
"program": "${workspaceFolder}/node_modules/jest/bin/jest"
22-
}
19+
"internalConsoleOptions": "neverOpen"
2320
}
2421
]
2522
}

config/jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const react17TestFileIgnoreList = [
3333
ignoreTSFiles,
3434
// We only support Suspense with React 18, so don't test suspense hooks with
3535
// React 17
36+
"src/testing/core/__tests__/createProxiedSchema.test.tsx",
3637
"src/react/hooks/__tests__/useSuspenseQuery.test.tsx",
3738
"src/react/hooks/__tests__/useBackgroundQuery.test.tsx",
3839
"src/react/hooks/__tests__/useLoadableQuery.test.tsx",

package-lock.json

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
"@changesets/changelog-github": "0.5.0",
113113
"@changesets/cli": "2.27.1",
114114
"@graphql-tools/schema": "10.0.3",
115+
"@graphql-tools/utils": "10.0.13",
115116
"@microsoft/api-extractor": "7.42.3",
116117
"@rollup/plugin-node-resolve": "11.2.1",
117118
"@size-limit/esbuild-why": "11.1.2",
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
diff --git a/node_modules/jest-environment-jsdom/build/index.js b/node_modules/jest-environment-jsdom/build/index.js
2+
index 2e6c16c..174e7a0 100644
3+
--- a/node_modules/jest-environment-jsdom/build/index.js
4+
+++ b/node_modules/jest-environment-jsdom/build/index.js
5+
@@ -96,6 +96,10 @@ class JSDOMEnvironment {
6+
// TODO: remove this ASAP, but it currently causes tests to run really slow
7+
global.Buffer = Buffer;
8+
9+
+ // Add mocks for schemaProxy tests that rely on `Response` and `fetch`
10+
+ // being globally available
11+
+ global.Response = Response;
12+
+
13+
// Report uncaught errors.
14+
this.errorEventListener = event => {
15+
if (userErrorListenerCount === 0 && event.error != null) {

0 commit comments

Comments
 (0)