Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schema-driven testing utilities #11605

Merged
merged 30 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
d3d0ed9
feat: introduce schema proxy, copy graphql-tools/utils fns and licens…
alessbell Feb 16, 2024
88c3f4f
scaffold integration test
alessbell Feb 16, 2024
ea891f2
export createMockSchema and proxiedSchema
alessbell Feb 16, 2024
9a63ad9
tests: add schemaProxy tests
alessbell Feb 23, 2024
614b8f6
commit failing mutation test
alessbell Feb 23, 2024
a2375e1
chore: remove integration tests and update mutation test
alessbell Mar 20, 2024
e92a0af
chore: add changeset
alessbell Mar 20, 2024
e765917
chore: update tests and fix ts errors
alessbell Mar 21, 2024
7897611
update mockFetchWithSchema
alessbell Mar 26, 2024
d355cd2
fix: call .bind and move test file
alessbell Mar 26, 2024
85671a6
feat: adds schema validation
alessbell Mar 26, 2024
57ab7e4
chore: update api reports
alessbell Mar 26, 2024
919d66d
chore: remove skipped tests
alessbell Mar 26, 2024
de6aa06
fix: remove typescript ignore comments and improve accuracy of code c…
alessbell Mar 28, 2024
7d30ccf
chore: update launch.json
alessbell Mar 29, 2024
d1f3c19
feat: make fetch automocking/unmocking nicer
alessbell Mar 29, 2024
4f66938
feat: change API to .add and .fork that accept options including reso…
alessbell Mar 29, 2024
177790b
fix: hoist result of shallow merged resolvers to outer scope on calls…
alessbell Apr 1, 2024
a60a313
Clean up Prettier, Size-limit, and Api-Extractor
alessbell Apr 1, 2024
9b4badc
Merge branch 'release-3.10' into issue-9738-schema-driven-testing-utils
alessbell Apr 2, 2024
0079451
fix: remove node-fetch from mockFetchWithSchema
alessbell Apr 2, 2024
f7c04c9
Clean up Prettier, Size-limit, and Api-Extractor
alessbell Apr 2, 2024
d0a9119
chore: adds createMockFetch changeset
alessbell Apr 2, 2024
f72cbba
chore: add doc blocks
alessbell Apr 2, 2024
c86d306
Clean up Prettier, Size-limit, and Api-Extractor
alessbell Apr 2, 2024
59b847c
Merge branch 'release-3.10' into issue-9738-schema-driven-testing-utils
alessbell Apr 2, 2024
225223e
chore: remove unneeded fetch mock from jest-environment-jsdom to fix …
alessbell Apr 2, 2024
26d3e20
Merge branch 'issue-9738-schema-driven-testing-utils' of github.com:a…
alessbell Apr 2, 2024
6670ad8
fix: dont set AbortController in the env, it breaks two PQ tests
alessbell Apr 2, 2024
614cd3f
Merge branch 'release-3.10' into issue-9738-schema-driven-testing-utils
alessbell Apr 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading