Skip to content

Commit

Permalink
fix: remove node-fetch from mockFetchWithSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
alessbell committed Apr 2, 2024
1 parent 9b4badc commit 0079451
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
19 changes: 19 additions & 0 deletions patches/jest-environment-jsdom+29.7.0.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
diff --git a/node_modules/jest-environment-jsdom/build/index.js b/node_modules/jest-environment-jsdom/build/index.js
index 2e6c16c..ce5c77d 100644
--- a/node_modules/jest-environment-jsdom/build/index.js
+++ b/node_modules/jest-environment-jsdom/build/index.js
@@ -96,6 +96,14 @@ 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.fetch = fetch;
+ global.Headers = Headers;
+ global.Request = Request;
+ global.Response = Response;
+ global.AbortController = AbortController;
+
// Report uncaught errors.
this.errorEventListener = event => {
if (userErrorListenerCount === 0 && event.error != null) {
1 change: 1 addition & 0 deletions src/testing/core/__tests__/schemaProxy.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,7 @@ describe("schema proxy", () => {

const { ErrorBoundary } = createTrackedErrorComponents(Profiler);

// @ts-expect-error - we're intentionally passing an invalid schema
using _fetch = createMockFetch(forkedSchema);

const client = new ApolloClient({
Expand Down
11 changes: 4 additions & 7 deletions src/testing/core/mockFetchWithSchema.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Response as NodeFetchResponse } from "node-fetch";
import { execute, validate } from "graphql";
import type { GraphQLError } from "graphql";
import type { GraphQLError, GraphQLSchema } from "graphql";
import { ApolloError, gql } from "../../core/index.js";
import { withCleanup } from "../internal/index.js";

const createMockFetch = (
schema: any,
schema: GraphQLSchema,
mockFetchOpts: { validate: boolean } = { validate: true }
) => {
const prevFetch = window.fetch;
Expand All @@ -31,9 +30,7 @@ const createMockFetch = (

if (validationErrors?.length > 0) {
return resolve(
new NodeFetchResponse(
JSON.stringify({ errors: validationErrors })
) as unknown as Response
new Response(JSON.stringify({ errors: validationErrors }))
);
}
}
Expand All @@ -47,7 +44,7 @@ const createMockFetch = (

const stringifiedResult = JSON.stringify(result);

resolve(new NodeFetchResponse(stringifiedResult) as unknown as Response);
resolve(new Response(stringifiedResult));
});
};

Expand Down

0 comments on commit 0079451

Please sign in to comment.