From ee21d1165bba9e75efb2a44cdffd5a09883bba8a Mon Sep 17 00:00:00 2001 From: Alessia Bellisario Date: Fri, 12 Apr 2024 09:53:43 -0400 Subject: [PATCH] Revert "suggestion: mock global `fetch` explicitly (#11779)" This reverts commit 243c53815cf7b99f93ac6f34470d0041dcd924de. --- .api-reports/api-report-testing.md | 15 +- .api-reports/api-report-testing_core.md | 15 +- .size-limits.json | 4 +- .../createTestSchema.test.tsx.snap | 17 -- .../core/__tests__/createTestSchema.test.tsx | 204 +----------------- src/testing/core/createSchemaFetch.ts | 43 +--- 6 files changed, 27 insertions(+), 271 deletions(-) delete mode 100644 src/testing/core/__tests__/__snapshots__/createTestSchema.test.tsx.snap diff --git a/.api-reports/api-report-testing.md b/.api-reports/api-report-testing.md index 64b6bbcaacf..b85108ace73 100644 --- a/.api-reports/api-report-testing.md +++ b/.api-reports/api-report-testing.md @@ -451,16 +451,11 @@ export function createMockClient(data: TData, query: DocumentNode, variab // @alpha export const createSchemaFetch: (schema: GraphQLSchema, mockFetchOpts?: { - validate?: boolean; - delay?: { - min: number; - max: number; - }; -}) => ((uri: any, options: any) => Promise) & { - mockGlobal: () => { - restore: () => void; - } & Disposable; -}; + validate: boolean; +}) => { + mock: (uri: any, options: any) => Promise; + restore: () => void; +} & Disposable; // Warning: (ae-forgotten-export) The symbol "TestSchemaOptions" needs to be exported by the entry point index.d.ts // Warning: (ae-forgotten-export) The symbol "ProxiedSchema" needs to be exported by the entry point index.d.ts diff --git a/.api-reports/api-report-testing_core.md b/.api-reports/api-report-testing_core.md index 273eb179402..6199767a19f 100644 --- a/.api-reports/api-report-testing_core.md +++ b/.api-reports/api-report-testing_core.md @@ -450,16 +450,11 @@ export function createMockClient(data: TData, query: DocumentNode, variab // @alpha export const createSchemaFetch: (schema: GraphQLSchema, mockFetchOpts?: { - validate?: boolean; - delay?: { - min: number; - max: number; - }; -}) => ((uri: any, options: any) => Promise) & { - mockGlobal: () => { - restore: () => void; - } & Disposable; -}; + validate: boolean; +}) => { + mock: (uri: any, options: any) => Promise; + restore: () => void; +} & Disposable; // Warning: (ae-forgotten-export) The symbol "TestSchemaOptions" needs to be exported by the entry point index.d.ts // Warning: (ae-forgotten-export) The symbol "ProxiedSchema" needs to be exported by the entry point index.d.ts diff --git a/.size-limits.json b/.size-limits.json index ba437de408e..3ad88477003 100644 --- a/.size-limits.json +++ b/.size-limits.json @@ -1,4 +1,4 @@ { - "dist/apollo-client.min.cjs": 39539, - "import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32810 + "dist/apollo-client.min.cjs": 39538, + "import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32809 } diff --git a/src/testing/core/__tests__/__snapshots__/createTestSchema.test.tsx.snap b/src/testing/core/__tests__/__snapshots__/createTestSchema.test.tsx.snap deleted file mode 100644 index ffcf4fddae3..00000000000 --- a/src/testing/core/__tests__/__snapshots__/createTestSchema.test.tsx.snap +++ /dev/null @@ -1,17 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`schema proxy should call invariant.error if min delay is greater than max delay 1`] = ` -[MockFunction] { - "calls": Array [ - Array [ - "Please configure a minimum delay that is less than the maximum delay.", - ], - ], - "results": Array [ - Object { - "type": "return", - "value": undefined, - }, - ], -} -`; diff --git a/src/testing/core/__tests__/createTestSchema.test.tsx b/src/testing/core/__tests__/createTestSchema.test.tsx index 0f9f3cadc90..d205fe3d6db 100644 --- a/src/testing/core/__tests__/createTestSchema.test.tsx +++ b/src/testing/core/__tests__/createTestSchema.test.tsx @@ -173,7 +173,7 @@ describe("schema proxy", () => { it("mocks scalars and resolvers", async () => { const Profiler = createDefaultProfiler(); - using _fetch = createSchemaFetch(schema).mockGlobal(); + using _fetch = createSchemaFetch(schema); const client = new ApolloClient({ cache: new InMemoryCache(), @@ -266,7 +266,7 @@ describe("schema proxy", () => { const Profiler = createDefaultProfiler(); - using _fetch = createSchemaFetch(forkedSchema).mockGlobal(); + using _fetch = createSchemaFetch(forkedSchema); const client = new ApolloClient({ cache: new InMemoryCache(), @@ -349,7 +349,7 @@ describe("schema proxy", () => { it("does not pollute the original schema", async () => { const Profiler = createDefaultProfiler(); - using _fetch = createSchemaFetch(schema).mockGlobal(); + using _fetch = createSchemaFetch(schema); const client = new ApolloClient({ cache: new InMemoryCache(), @@ -444,7 +444,7 @@ describe("schema proxy", () => { const Profiler = createDefaultProfiler(); - using _fetch = createSchemaFetch(forkedSchema).mockGlobal(); + using _fetch = createSchemaFetch(forkedSchema); const client = new ApolloClient({ cache: new InMemoryCache(), @@ -566,7 +566,7 @@ describe("schema proxy", () => { const Profiler = createDefaultProfiler(); - using _fetch = createSchemaFetch(forkedSchema).mockGlobal(); + using _fetch = createSchemaFetch(forkedSchema); const client = new ApolloClient({ cache: new InMemoryCache(), @@ -709,7 +709,7 @@ describe("schema proxy", () => { const { ErrorBoundary } = createTrackedErrorComponents(Profiler); - using _fetch = createSchemaFetch(forkedSchema).mockGlobal(); + using _fetch = createSchemaFetch(forkedSchema); const client = new ApolloClient({ cache: new InMemoryCache(), @@ -789,7 +789,7 @@ describe("schema proxy", () => { const { ErrorBoundary } = createTrackedErrorComponents(Profiler); // @ts-expect-error - we're intentionally passing an invalid schema - using _fetch = createSchemaFetch(forkedSchema).mockGlobal(); + using _fetch = createSchemaFetch(forkedSchema); const client = new ApolloClient({ cache: new InMemoryCache(), @@ -916,7 +916,7 @@ describe("schema proxy", () => { const Profiler = createDefaultProfiler(); - using _fetch = createSchemaFetch(forkedSchema).mockGlobal(); + using _fetch = createSchemaFetch(forkedSchema); const client = new ApolloClient({ cache: new InMemoryCache(), @@ -1036,192 +1036,4 @@ describe("schema proxy", () => { unmount(); }); - - it("createSchemaFetch respects min and max delay", async () => { - const Profiler = createDefaultProfiler(); - - const maxDelay = 2000; - - using _fetch = createSchemaFetch(schema, { - delay: { min: 10, max: maxDelay }, - }).mockGlobal(); - - const client = new ApolloClient({ - cache: new InMemoryCache(), - uri, - }); - - const query: TypedDocumentNode = gql` - query { - viewer { - id - name - age - book { - id - title - publishedAt - } - } - } - `; - - const Fallback = () => { - useTrackRenders(); - return
Loading...
; - }; - - const App = () => { - return ( - }> - - - ); - }; - - const Child = () => { - const result = useSuspenseQuery(query); - - useTrackRenders(); - - Profiler.mergeSnapshot({ - result, - } as Partial<{}>); - - return
Hello
; - }; - - const { unmount, rerender } = renderWithClient(, { - client, - wrapper: Profiler, - }); - - // initial suspended render - await Profiler.takeRender(); - - { - try { - const { snapshot: _snapshot } = await Profiler.takeRender(); - } catch (e) { - // default timeout is 1000, so this throws - if (e instanceof Error) { - expect(e.message).toMatch( - /Exceeded timeout waiting for next render./ - ); - } - } - } - - rerender(); - - // suspended render - await Profiler.takeRender(); - - { - // with a timeout > maxDelay, this passes - const { snapshot } = await Profiler.takeRender({ - timeout: maxDelay + 100, - }); - - expect(snapshot.result?.data).toEqual({ - viewer: { - __typename: "User", - age: 42, - id: "1", - name: "Jane Doe", - book: { - __typename: "TextBook", - id: "1", - publishedAt: "2024-01-01", - title: "The Book", - }, - }, - }); - } - - unmount(); - }); - - it("should call invariant.error if min delay is greater than max delay", async () => { - using _consoleSpy = spyOnConsole.takeSnapshots("error"); - const Profiler = createDefaultProfiler(); - - using _fetch = createSchemaFetch(schema, { - delay: { min: 3000, max: 1000 }, - }).mockGlobal(); - - const client = new ApolloClient({ - cache: new InMemoryCache(), - uri, - }); - - const query: TypedDocumentNode = gql` - query { - viewer { - id - name - age - book { - id - title - publishedAt - } - } - } - `; - - const Fallback = () => { - useTrackRenders(); - return
Loading...
; - }; - - const App = () => { - return ( - }> - - - ); - }; - - const Child = () => { - const result = useSuspenseQuery(query); - - useTrackRenders(); - - Profiler.mergeSnapshot({ - result, - } as Partial<{}>); - - return
Hello
; - }; - - const { unmount } = renderWithClient(, { - client, - wrapper: Profiler, - }); - - // suspended render - await Profiler.takeRender(); - - { - const { snapshot } = await Profiler.takeRender(); - - expect(snapshot.result?.data).toEqual({ - viewer: { - __typename: "User", - age: 42, - id: "1", - name: "Jane Doe", - book: { - __typename: "TextBook", - id: "1", - publishedAt: "2024-01-01", - title: "The Book", - }, - }, - }); - } - - unmount(); - }); }); diff --git a/src/testing/core/createSchemaFetch.ts b/src/testing/core/createSchemaFetch.ts index e4ccb9fcc67..8b9b5d82dd7 100644 --- a/src/testing/core/createSchemaFetch.ts +++ b/src/testing/core/createSchemaFetch.ts @@ -2,8 +2,6 @@ import { execute, validate } from "graphql"; import type { GraphQLError, GraphQLSchema } from "graphql"; import { ApolloError, gql } from "../../core/index.js"; import { withCleanup } from "../internal/index.js"; -import { wait } from "./wait.js"; -import { invariant } from "../../utilities/globals/invariantWrappers.js"; /** * A function that accepts a static `schema` and a `mockFetchOpts` object and @@ -34,31 +32,14 @@ import { invariant } from "../../utilities/globals/invariantWrappers.js"; */ const createSchemaFetch = ( schema: GraphQLSchema, - mockFetchOpts: { - validate?: boolean; - delay?: { min: number; max: number }; - } = { validate: true, delay: { min: 0, max: 0 } } + mockFetchOpts: { validate: boolean } = { validate: true } ) => { const prevFetch = window.fetch; - const mockFetch: (uri: any, options: any) => Promise = async ( + const mockFetch: (uri: any, options: any) => Promise = ( _uri, options ) => { - if (mockFetchOpts.delay) { - if (mockFetchOpts.delay.min > mockFetchOpts.delay.max) { - invariant.error( - "Please configure a minimum delay that is less than the maximum delay." - ); - } else { - const randomDelay = - Math.random() * (mockFetchOpts.delay.max - mockFetchOpts.delay.min) + - mockFetchOpts.delay.min; - - await wait(randomDelay); - } - } - return new Promise(async (resolve) => { const body = JSON.parse(options.body); const document = gql(body.query); @@ -94,23 +75,13 @@ const createSchemaFetch = ( }); }; - function mockGlobal() { - window.fetch = mockFetch; + window.fetch = mockFetch; - const restore = () => { - if (window.fetch === mockFetch) { - window.fetch = prevFetch; - } - }; - - return withCleanup({ restore }, restore); - } + const restore = () => { + window.fetch = prevFetch; + }; - return Object.assign(mockFetch, { - mockGlobal, - // if https://github.com/rbuckton/proposal-using-enforcement lands - // [Symbol.enter]: mockGlobal - }); + return withCleanup({ mock: mockFetch, restore }, restore); }; export { createSchemaFetch };