Skip to content

Commit

Permalink
Schema-driven testing utilities (#11605)
Browse files Browse the repository at this point in the history
  • Loading branch information
alessbell authored Apr 2, 2024
1 parent 78891f9 commit e2dd4c9
Show file tree
Hide file tree
Showing 19 changed files with 1,864 additions and 20 deletions.
40 changes: 40 additions & 0 deletions .api-reports/api-report-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
```ts

/// <reference types="node" />

import type { ASTNode } from 'graphql';
import type { DocumentNode } from 'graphql';
import type { ExecutionResult } from 'graphql';
import type { FieldNode } from 'graphql';
import type { FragmentDefinitionNode } from 'graphql';
import type { GraphQLError } from 'graphql';
import type { GraphQLErrorExtensions } from 'graphql';
import type { GraphQLSchema } from 'graphql';
import { Observable } from 'zen-observable-ts';
import type { Observer } from 'zen-observable-ts';
import * as React_2 from 'react';
Expand Down Expand Up @@ -446,6 +449,24 @@ type ConcastSourcesIterable<T> = Iterable<Source<T>>;
// @public (undocumented)
export function createMockClient<TData>(data: TData, query: DocumentNode, variables?: {}): ApolloClient<NormalizedCacheObject>;

// @alpha
export const createMockFetch: (schema: GraphQLSchema, mockFetchOpts?: {
validate: boolean;
}) => {
mock: (uri: any, options: any) => Promise<Response>;
restore: () => void;
} & Disposable;

// @alpha
export const createMockSchema: (staticSchema: GraphQLSchema, mocks: {
[key: string]: any;
}) => GraphQLSchema;

// Warning: (ae-forgotten-export) The symbol "ProxiedSchema" needs to be exported by the entry point index.d.ts
//
// @alpha
export const createProxiedSchema: (schemaWithMocks: GraphQLSchema, resolvers: Resolvers) => ProxiedSchema;

// @public (undocumented)
namespace DataProxy {
// (undocumented)
Expand Down Expand Up @@ -1265,6 +1286,25 @@ type Path = ReadonlyArray<string | number>;
// @public (undocumented)
type Primitive = null | undefined | string | number | boolean | symbol | bigint;

// Warning: (ae-forgotten-export) The symbol "ProxiedSchemaFns" needs to be exported by the entry point index.d.ts
//
// @public (undocumented)
type ProxiedSchema = GraphQLSchema & ProxiedSchemaFns;

// @public (undocumented)
interface ProxiedSchemaFns {
// (undocumented)
add: (addOptions: {
resolvers: Resolvers;
}) => ProxiedSchema;
// (undocumented)
fork: (forkOptions?: {
resolvers?: Resolvers;
}) => ProxiedSchema;
// (undocumented)
reset: () => void;
}

// @public (undocumented)
class QueryInfo {
constructor(queryManager: QueryManager<any>, queryId?: string);
Expand Down
35 changes: 35 additions & 0 deletions .api-reports/api-report-testing_core.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
```ts

/// <reference types="node" />

import type { ASTNode } from 'graphql';
import type { DocumentNode } from 'graphql';
import type { ExecutionResult } from 'graphql';
import type { FieldNode } from 'graphql';
import type { FragmentDefinitionNode } from 'graphql';
import type { GraphQLError } from 'graphql';
import type { GraphQLErrorExtensions } from 'graphql';
import type { GraphQLSchema } from 'graphql';
import { Observable } from 'zen-observable-ts';
import type { Observer } from 'zen-observable-ts';
import type { Subscriber } from 'zen-observable-ts';
Expand Down Expand Up @@ -445,6 +448,19 @@ type ConcastSourcesIterable<T> = Iterable<Source<T>>;
// @public (undocumented)
export function createMockClient<TData>(data: TData, query: DocumentNode, variables?: {}): ApolloClient<NormalizedCacheObject>;

// @alpha
export const createMockFetch: (schema: GraphQLSchema, mockFetchOpts?: {
validate: boolean;
}) => {
mock: (uri: any, options: any) => Promise<Response>;
restore: () => void;
} & Disposable;

// Warning: (ae-forgotten-export) The symbol "ProxiedSchema" needs to be exported by the entry point index.d.ts
//
// @alpha
export const createProxiedSchema: (schemaWithMocks: GraphQLSchema, resolvers: Resolvers) => ProxiedSchema;

// @public (undocumented)
namespace DataProxy {
// (undocumented)
Expand Down Expand Up @@ -1220,6 +1236,25 @@ type Path = ReadonlyArray<string | number>;
// @public (undocumented)
type Primitive = null | undefined | string | number | boolean | symbol | bigint;

// Warning: (ae-forgotten-export) The symbol "ProxiedSchemaFns" needs to be exported by the entry point index.d.ts
//
// @public (undocumented)
type ProxiedSchema = GraphQLSchema & ProxiedSchemaFns;

// @public (undocumented)
interface ProxiedSchemaFns {
// (undocumented)
add: (addOptions: {
resolvers: Resolvers;
}) => ProxiedSchema;
// (undocumented)
fork: (forkOptions?: {
resolvers?: Resolvers;
}) => ProxiedSchema;
// (undocumented)
reset: () => void;
}

// @public (undocumented)
class QueryInfo {
constructor(queryManager: QueryManager<any>, queryId?: string);
Expand Down
5 changes: 5 additions & 0 deletions .changeset/chatty-llamas-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": minor
---

Adds `createMockFetch` utility for integration testing that includes the link chain
5 changes: 5 additions & 0 deletions .changeset/stupid-bears-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": minor
---

Adds proxiedSchema and createMockSchema testing utilities
4 changes: 2 additions & 2 deletions .size-limits.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"dist/apollo-client.min.cjs": 39512,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32801
"dist/apollo-client.min.cjs": 39506,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32793
}
27 changes: 12 additions & 15 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to Node.js inspector",
"port": 9229,
"request": "attach",
"skipFiles": ["<node_internals>/**"],
"type": "pwa-node"
},
{
"type": "node",
"request": "launch",
"name": "Jest Current File",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": ["${relativeFile}", "--config", "./config/jest.config.js"],
"name": "Jest Attach Node Inspector for Current File",
"cwd": "${workspaceFolder}",
"runtimeArgs": [
"--inspect-brk",
"${workspaceRoot}/node_modules/.bin/jest",
"${relativeFile}",
"--config",
"./config/jest.config.js",
"--runInBand",
"--watch"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest"
}
"internalConsoleOptions": "neverOpen"
}
]
}
1 change: 1 addition & 0 deletions config/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const react17TestFileIgnoreList = [
ignoreTSFiles,
// We only support Suspense with React 18, so don't test suspense hooks with
// React 17
"src/testing/core/__tests__/createProxiedSchema.test.tsx",
"src/react/hooks/__tests__/useSuspenseQuery.test.tsx",
"src/react/hooks/__tests__/useBackgroundQuery.test.tsx",
"src/react/hooks/__tests__/useLoadableQuery.test.tsx",
Expand Down
7 changes: 4 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
"@changesets/changelog-github": "0.5.0",
"@changesets/cli": "2.27.1",
"@graphql-tools/schema": "10.0.3",
"@graphql-tools/utils": "10.0.13",
"@microsoft/api-extractor": "7.42.3",
"@rollup/plugin-node-resolve": "11.2.1",
"@size-limit/esbuild-why": "11.1.2",
Expand Down
15 changes: 15 additions & 0 deletions patches/jest-environment-jsdom+29.7.0.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
diff --git a/node_modules/jest-environment-jsdom/build/index.js b/node_modules/jest-environment-jsdom/build/index.js
index 2e6c16c..174e7a0 100644
--- a/node_modules/jest-environment-jsdom/build/index.js
+++ b/node_modules/jest-environment-jsdom/build/index.js
@@ -96,6 +96,10 @@ class JSDOMEnvironment {
// TODO: remove this ASAP, but it currently causes tests to run really slow
global.Buffer = Buffer;

+ // Add mocks for schemaProxy tests that rely on `Response` and `fetch`
+ // being globally available
+ global.Response = Response;
+
// Report uncaught errors.
this.errorEventListener = event => {
if (userErrorListenerCount === 0 && event.error != null) {
5 changes: 5 additions & 0 deletions src/__tests__/__snapshots__/exports.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,9 @@ Array [
"MockSubscriptionLink",
"MockedProvider",
"createMockClient",
"createMockFetch",
"createMockSchema",
"createProxiedSchema",
"itAsync",
"mockObservableLink",
"mockSingleLink",
Expand All @@ -386,6 +389,8 @@ Array [
"MockLink",
"MockSubscriptionLink",
"createMockClient",
"createMockFetch",
"createProxiedSchema",
"itAsync",
"mockObservableLink",
"mockSingleLink",
Expand Down
Loading

0 comments on commit e2dd4c9

Please sign in to comment.