From 2764dcd92e179f705dcc256b08f417430390aa08 Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Thu, 26 Dec 2024 13:39:53 -0500 Subject: [PATCH] chore(deps): update dependency fetch-mock to v12 (#606) * chore(deps): update dependency fetch-mock to v12 * updates tests to use the new v12 fetch-mock apis --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Nick Floyd --- package-lock.json | 39 ++++--- package.json | 2 +- test/defaults.test.ts | 62 ++++++----- test/error.test.ts | 38 ++++--- test/graphql.test.ts | 179 ++++++++++++++++--------------- test/with-custom-request.test.ts | 24 +++-- 6 files changed, 188 insertions(+), 156 deletions(-) diff --git a/package-lock.json b/package-lock.json index 38cb46e5..790eab5f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19,7 +19,7 @@ "@types/jest": "^29.0.0", "@types/node": "^22.0.0", "esbuild": "^0.24.0", - "fetch-mock": "^11.0.0", + "fetch-mock": "^12.0.0", "glob": "^11.0.0", "jest": "^29.0.0", "prettier": "3.4.2", @@ -2476,25 +2476,20 @@ } }, "node_modules/fetch-mock": { - "version": "11.1.5", - "resolved": "https://registry.npmjs.org/fetch-mock/-/fetch-mock-11.1.5.tgz", - "integrity": "sha512-KHmZDnZ1ry0pCTrX4YG5DtThHi0MH+GNI9caESnzX/nMJBrvppUHMvLx47M0WY9oAtKOMiPfZDRpxhlHg89BOA==", + "version": "12.0.0", + "resolved": "https://registry.npmjs.org/fetch-mock/-/fetch-mock-12.0.0.tgz", + "integrity": "sha512-JSsjzoRN4rYqHa2/+8ushJGDsK9HGNTdBZo6Hrpu3KFN7Y03nRCt2VJ2WG4OUvyTUukOQ4TQIfjcFcEkMPGZ0Q==", "dev": true, "license": "MIT", "dependencies": { "@types/glob-to-regexp": "^0.4.4", "dequal": "^2.0.3", "glob-to-regexp": "^0.4.1", - "is-subset": "^0.1.1", + "is-subset-of": "^3.1.10", "regexparam": "^3.0.0" }, "engines": { - "node": ">=8.0.0" - }, - "peerDependenciesMeta": { - "node-fetch": { - "optional": true - } + "node": ">=18.11.0" } }, "node_modules/filelist": { @@ -2842,12 +2837,16 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/is-subset": { - "version": "0.1.1", - "resolved": "https://registry.npmjs.org/is-subset/-/is-subset-0.1.1.tgz", - "integrity": "sha512-6Ybun0IkarhmEqxXCNw/C0bna6Zb/TkfUX9UbwJtK6ObwAVCxmAP308WWTHviM/zAqXk05cdhYsUsZeGQh99iw==", + "node_modules/is-subset-of": { + "version": "3.1.10", + "resolved": "https://registry.npmjs.org/is-subset-of/-/is-subset-of-3.1.10.tgz", + "integrity": "sha512-avvaYgVmYWyaZ1NDFiv4y9JGkrE2je3op1Po4VYKKJKR8H2qVPsg1GZuuXl5elCTxTlwAIsrAjWAs4BVrISFRw==", + "deprecated": "Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.", "dev": true, - "license": "MIT" + "license": "MIT", + "dependencies": { + "typedescriptor": "3.0.2" + } }, "node_modules/isexe": { "version": "2.0.0", @@ -4846,6 +4845,14 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/typedescriptor": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/typedescriptor/-/typedescriptor-3.0.2.tgz", + "integrity": "sha512-hyVbaCUd18UiXk656g/imaBLMogpdijIEpnhWYrSda9rhvO4gOU16n2nh7xG5lv/rjumnZzGOdz0CEGTmFe0fQ==", + "deprecated": "Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.", + "dev": true, + "license": "MIT" + }, "node_modules/typescript": { "version": "5.7.2", "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.7.2.tgz", diff --git a/package.json b/package.json index a9558ec3..dcf07836 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "@types/jest": "^29.0.0", "@types/node": "^22.0.0", "esbuild": "^0.24.0", - "fetch-mock": "^11.0.0", + "fetch-mock": "^12.0.0", "glob": "^11.0.0", "jest": "^29.0.0", "prettier": "3.4.2", diff --git a/test/defaults.test.ts b/test/defaults.test.ts index 9cbcdc37..07d853da 100644 --- a/test/defaults.test.ts +++ b/test/defaults.test.ts @@ -36,20 +36,22 @@ describe("graphql.defaults()", () => { }, }; + const mock = fetchMock.createInstance().post( + "https://api.github.com/graphql", + { data: mockData }, + { + headers: { + authorization: "token secret123", + }, + }, + ); + const authenticatedGraphql = graphql.defaults({ headers: { authorization: `token secret123`, }, request: { - fetch: fetchMock.sandbox().post( - "https://api.github.com/graphql", - { data: mockData }, - { - headers: { - authorization: "token secret123", - }, - }, - ), + fetch: mock.fetchHandler, }, }); return authenticatedGraphql(`{ @@ -92,20 +94,22 @@ describe("graphql.defaults()", () => { }, }; + const mock = fetchMock.createInstance().post( + "https://github.acme-inc.com/api/graphql", + { data: mockData }, + { + headers: { + authorization: "token secret123", + }, + }, + ); + const authenticatedGraphql = graphql.defaults({ headers: { authorization: `token secret123`, }, request: { - fetch: fetchMock.sandbox().post( - "https://github.acme-inc.com/api/graphql", - { data: mockData }, - { - headers: { - authorization: "token secret123", - }, - }, - ), + fetch: mock.fetchHandler, }, }); const acmeGraphql = authenticatedGraphql.defaults({ @@ -127,21 +131,23 @@ describe("graphql.defaults()", () => { }); it("handle baseUrl set with /api/v3 suffix", () => { + const mock = fetchMock.createInstance().post( + "https://github.acme-inc.com/api/graphql", + { data: { ok: true } }, + { + headers: { + authorization: "token secret123", + }, + }, + ); + const ghesGraphQl = graphql.defaults({ baseUrl: "https://github.acme-inc.com/api/v3", headers: { authorization: `token secret123`, }, request: { - fetch: fetchMock.sandbox().post( - "https://github.acme-inc.com/api/graphql", - { data: { ok: true } }, - { - headers: { - authorization: "token secret123", - }, - }, - ), + fetch: mock.fetchHandler, }, }); @@ -182,7 +188,7 @@ describe("graphql.defaults()", () => { authorization: `token secret123`, }, request: { - fetch: fetchMock.sandbox().post( + fetch: fetchMock.createInstance().post( "https://github.acme-inc.com/api/graphql", { data: mockData }, { diff --git a/test/error.test.ts b/test/error.test.ts index 5904133a..ff474971 100644 --- a/test/error.test.ts +++ b/test/error.test.ts @@ -24,14 +24,16 @@ describe("errors", () => { ], }; + const mock = fetchMock + .createInstance() + .post("https://api.github.com/graphql", mockResponse); + return graphql(query, { headers: { authorization: `token secret123`, }, request: { - fetch: fetchMock - .sandbox() - .post("https://api.github.com/graphql", mockResponse), + fetch: mock.fetchHandler, }, }) .then(() => { @@ -72,14 +74,16 @@ describe("errors", () => { ], }; + const mock = fetchMock + .createInstance() + .post("https://api.github.com/graphql", mockResponse); + return graphql(query, { headers: { authorization: `token secret123`, }, request: { - fetch: fetchMock - .sandbox() - .post("https://api.github.com/graphql", mockResponse), + fetch: mock.fetchHandler, }, }) .then(() => { @@ -131,17 +135,21 @@ describe("errors", () => { ], }; + const mock = fetchMock + .createInstance() + .post("https://api.github.com/graphql", { + body: mockResponse, + headers: { + "x-github-request-id": "C5E6:259A:1351B40:2E88B87:5F1F9C41", + }, + }); + return graphql(query, { headers: { authorization: `token secret123`, }, request: { - fetch: fetchMock.sandbox().post("https://api.github.com/graphql", { - body: mockResponse, - headers: { - "x-github-request-id": "C5E6:259A:1351B40:2E88B87:5F1F9C41", - }, - }), + fetch: mock.fetchHandler, }, }) .then(() => { @@ -173,12 +181,16 @@ describe("errors", () => { } }`; + const mock = fetchMock + .createInstance() + .post("https://api.github.com/graphql", 500); + return graphql(query, { headers: { authorization: `token secret123`, }, request: { - fetch: fetchMock.sandbox().post("https://api.github.com/graphql", 500), + fetch: mock.fetchHandler, }, }) .then(() => { diff --git a/test/graphql.test.ts b/test/graphql.test.ts index caa66746..882b2854 100644 --- a/test/graphql.test.ts +++ b/test/graphql.test.ts @@ -1,6 +1,5 @@ import fetchMock from "fetch-mock"; import { getUserAgent } from "universal-user-agent"; -import type * as OctokitTypes from "@octokit/types"; import { graphql } from "../src"; import { VERSION } from "../src/version"; @@ -38,6 +37,18 @@ describe("graphql()", () => { }, }; + const mock = fetchMock.createInstance().post( + "https://api.github.com/graphql", + { data: mockData }, + { + headers: { + accept: "application/vnd.github.v3+json", + authorization: "token secret123", + "user-agent": userAgent, + }, + }, + ); + return graphql( ` { @@ -57,17 +68,7 @@ describe("graphql()", () => { authorization: `token secret123`, }, request: { - fetch: fetchMock.sandbox().post( - "https://api.github.com/graphql", - { data: mockData }, - { - headers: { - accept: "application/vnd.github.v3+json", - authorization: "token secret123", - "user-agent": userAgent, - }, - }, - ), + fetch: mock.fetchHandler, }, }, ).then((result) => { @@ -88,6 +89,20 @@ describe("graphql()", () => { } }`; + const mock = fetchMock + .createInstance() + .post("https://api.github.com/graphql", (_url: any) => { + //@ts-ignore mock.fetchHandler is not typed + const body = JSON.parse(mock.callHistory.calls()[0].options.body); + expect(body.query).toEqual(query); + expect(body.variables).toStrictEqual({ + owner: "octokit", + repo: "graphql.js", + }); + + return { data: {} }; + }); + return graphql(query, { headers: { authorization: `token secret123`, @@ -95,21 +110,7 @@ describe("graphql()", () => { owner: "octokit", repo: "graphql.js", request: { - fetch: fetchMock - .sandbox() - .post( - "https://api.github.com/graphql", - (_url, options: OctokitTypes.RequestOptions) => { - const body = JSON.parse(options.body); - expect(body.query).toEqual(query); - expect(body.variables).toStrictEqual({ - owner: "octokit", - repo: "graphql.js", - }); - - return { data: {} }; - }, - ), + fetch: mock.fetchHandler, }, }); }); @@ -127,6 +128,23 @@ describe("graphql()", () => { } }`; + const mock = fetchMock + .createInstance() + .post("https://api.github.com/graphql", (_url: any) => { + //@ts-ignore mock.fetchHandler is not typed + const body = JSON.parse(mock.callHistory.calls()[0].options.body); + expect(body.query).toEqual(query); + expect(body.variables).toStrictEqual({ + owner: "octokit", + repo: "graphql.js", + }); + expect(options.headers!.authorization).toEqual("token secret123"); + // @ts-ignore `request.headers` are typed incorrectly by fetch-mock + const custom = mock.callHistory.calls()[0].options.headers!["x-custom"]; + expect(custom).toContain("value"); + return { data: {} }; + }); + const options: RequestParameters = { headers: { authorization: `token secret123`, @@ -135,25 +153,7 @@ describe("graphql()", () => { owner: "octokit", repo: "graphql.js", request: { - fetch: fetchMock - .sandbox() - .post( - "https://api.github.com/graphql", - (_url, options: OctokitTypes.RequestOptions) => { - const body = JSON.parse(options.body); - expect(body.query).toEqual(query); - expect(body.variables).toStrictEqual({ - owner: "octokit", - repo: "graphql.js", - }); - expect(options.headers["authorization"]).toEqual( - "token secret123", - ); - expect(options.headers["x-custom"]).toEqual("value"); - - return { data: {} }; - }, - ), + fetch: mock.fetchHandler, }, }; @@ -173,6 +173,20 @@ describe("graphql()", () => { } }`; + const mock = fetchMock + .createInstance() + .post("https://api.github.com/graphql", (_url: any) => { + //@ts-ignore mock.fetchHandler is not typed + const body = JSON.parse(mock.callHistory.calls()[0].options.body); + expect(body.query).toEqual(query); + expect(body.variables).toStrictEqual({ + owner: "octokit", + repo: "graphql.js", + }); + + return { data: {} }; + }); + const options: RequestParameters = { headers: { authorization: `token secret123`, @@ -181,21 +195,7 @@ describe("graphql()", () => { query, repo: "graphql.js", request: { - fetch: fetchMock - .sandbox() - .post( - "https://api.github.com/graphql", - (_url, options: OctokitTypes.RequestOptions) => { - const body = JSON.parse(options.body); - expect(body.query).toEqual(query); - expect(body.variables).toStrictEqual({ - owner: "octokit", - repo: "graphql.js", - }); - - return { data: {} }; - }, - ), + fetch: mock.fetchHandler, }, }; @@ -205,50 +205,55 @@ describe("graphql()", () => { it("Don’t send empty variables object", () => { const query = "{ viewer { login } }"; + const mock = fetchMock + .createInstance() + .post("https://api.github.com/graphql", (_url: any) => { + //@ts-ignore mock.fetchHandler is not typed + const body = JSON.parse(mock.callHistory.calls()[0].options.body); + expect(body.query).toEqual(query); + expect(body.variables).toEqual(undefined); + return { data: {} }; + }); + return graphql(query, { headers: { authorization: `token secret123`, }, request: { - fetch: fetchMock - .sandbox() - .post( - "https://api.github.com/graphql", - (_url, options: OctokitTypes.RequestOptions) => { - const body = JSON.parse(options.body); - expect(body.query).toEqual(query); - expect(body.variables).toEqual(undefined); - - return { data: {} }; - }, - ), + fetch: mock.fetchHandler, }, }); }); - it("MediaType previews are added to header", () => { + it("MediaType previews are added to header", async () => { const query = `{ viewer { login } }`; - return graphql(query, { + // Create a new FetchMock instance + const mock = fetchMock + .createInstance() + .postOnce("https://api.github.com/graphql", (_url: any) => { + // @ts-ignore `request.headers` are typed incorrectly by fetch-mock + const accept = mock.callHistory.calls()[0].options.headers["accept"]; + expect(accept).toContain("antiope-preview"); + expect(accept).toContain("testpkg-preview"); + return { + status: 200, + body: { data: {} }, + }; + }); + + // Perform the GraphQL request using mock.fetchHandler + const response = await graphql(query, { headers: { authorization: `token secret123`, }, - owner: "octokit", - repo: "graphql.js", mediaType: { previews: ["antiope", "testpkg"] }, request: { - fetch: fetchMock - .sandbox() - .post( - "https://api.github.com/graphql", - (_url, options: OctokitTypes.RequestOptions) => { - expect(options.headers.accept).toContain("antiope-preview"); - expect(options.headers.accept).toContain("testpkg-preview"); - return { data: {} }; - }, - ), + fetch: mock.fetchHandler, }, }); + + expect(response).toEqual({}); }); it("query variable (#166)", () => { diff --git a/test/with-custom-request.test.ts b/test/with-custom-request.test.ts index d08fcb7f..7bf4c961 100644 --- a/test/with-custom-request.test.ts +++ b/test/with-custom-request.test.ts @@ -37,6 +37,18 @@ describe("withCustomRequest()", () => { }, }; + const mock = fetchMock.createInstance().post( + "https://api.github.com/graphql", + { data: mockData }, + { + headers: { + accept: "application/vnd.github.v3+json", + authorization: "token secret123", + "user-agent": "test", + }, + }, + ); + return myGraphql( ` { @@ -56,17 +68,7 @@ describe("withCustomRequest()", () => { authorization: `token secret123`, }, request: { - fetch: fetchMock.sandbox().post( - "https://api.github.com/graphql", - { data: mockData }, - { - headers: { - accept: "application/vnd.github.v3+json", - authorization: "token secret123", - "user-agent": "test", - }, - }, - ), + fetch: mock.fetchHandler, }, }, ).then((result) => {