nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: wheresrhys/fetch-mock
Pull request #676 (2024-06-15 19:47:57)

match fetch when called with a string object


Patch:
diff --git a/docs/_usage/version-10-caveat.md b/docs/_usage/version-10-caveat.md
index dc85b89..894e9eb 100644
--- a/docs/_usage/version-10-caveat.md
+++ b/docs/_usage/version-10-caveat.md
@@ -1,5 +1,5 @@
 ---
-title: Versions 
+title: Versions
 position: 1
 content_markdown: |-
   Note that the documentation below refers to **version 9** of the library.
diff --git a/src/lib/fetch-handler.js b/src/lib/fetch-handler.js
index 5e95aa2..b0bb9d4 100755
--- a/src/lib/fetch-handler.js
+++ b/src/lib/fetch-handler.js
@@ -4,21 +4,6 @@ import * as requestUtils from './request-utils.js';
 
 const FetchMock = {};
 
-// see https://heycam.github.io/webidl/#aborterror for the standardised interface
-// Note that this differs slightly from node-fetch
-class AbortError extends Error {
-	constructor() {
-		super(...arguments);
-		this.name = 'AbortError';
-		this.message = 'The operation was aborted.';
-
-		// Do not include this class in the stacktrace
-		if (Error.captureStackTrace) {
-			Error.captureStackTrace(this, this.constructor);
-		}
-	}
-}
-
 const resolve = async (
 	{ response, responseIsFetch = false },
 	url,
@@ -117,14 +102,7 @@ FetchMock._fetchHandler = function ({ url, options, request, signal }) {
 			debug('signal exists - enabling fetch abort');
 			const abort = () => {
 				debug('aborting fetch');
-				// note that DOMException is not available in node.js;
-				// even node-fetch uses a custom error class:
-				// https://github.com/bitinn/node-fetch/blob/master/src/abort-error.js
-				rej(
-					typeof DOMException !== 'undefined'
-						? new DOMException('The operation was aborted.', 'AbortError')
-						: new AbortError(),
-				);
+				rej(new DOMException('The operation was aborted.', 'AbortError'));
 				done();
 			};
 			if (signal.aborted) {
diff --git a/src/lib/request-utils.js b/src/lib/request-utils.js
index 2726d8c..1d0c99e 100644
--- a/src/lib/request-utils.js
+++ b/src/lib/request-utils.js
@@ -63,6 +63,7 @@ export function normalizeRequest(url, options, Request) {
 	}
 	if (
 		typeof url === 'string' ||
+		url instanceof String ||
 		// horrible URL object duck-typing
 		(typeof url === 'object' && 'href' in url)
 	) {


Test patch:
diff --git a/test/specs/routing/url-matching.test.js b/test/specs/routing/url-matching.test.js
index 91e9600..d62d2cc 100644
--- a/test/specs/routing/url-matching.test.js
+++ b/test/specs/routing/url-matching.test.js
@@ -23,6 +23,12 @@ describe('url matching', () => {
 		expect(fm.calls(true).length).toEqual(2);
 	});
 
+	it('match string objects', async () => {
+		fm.mock('http://a.com/path', 200).catch();
+		await fm.fetchHandler(new String('http://a.com/path')); // eslint-disable-line no-new-wrappers
+		expect(fm.calls(true).length).toEqual(1);
+	});
+
 	it('match exact strings with relative url', async () => {
 		fm.mock('/path', 200).catch();
 		await fm.fetchHandler('/pat');
Repository: wheresrhys/fetch-mock
Pull request #821 (2024-08-29 16:30:37)

refactor: prefer to use top level methods exposed in @fetch-mock/core…
… for resetting


Patch:
diff --git a/packages/core/src/FetchMock.ts b/packages/core/src/FetchMock.ts
index b129bf0..e22f4e9 100644
--- a/packages/core/src/FetchMock.ts
+++ b/packages/core/src/FetchMock.ts
@@ -143,6 +143,7 @@ export class FetchMock {
 		this.callHistory.clear();
 		return this;
 	}
+
 	mockGlobal(this: FetchMock): FetchMock {
 		globalThis.fetch = this.fetchHandler;
 		return this;


Test patch:
diff --git a/packages/vitest/src/index.ts b/packages/vitest/src/index.ts
index 57f2b98..dfeebe0 100644
--- a/packages/vitest/src/index.ts
+++ b/packages/vitest/src/index.ts
@@ -8,11 +8,11 @@ import './vitest-extensions';
 
 class FetchMockVitest extends FetchMock {
 	mockClear() {
-		this.callHistory.clear();
+		this.clearHistory();
 		return this;
 	}
 	mockReset(options?: RemoveRouteOptions) {
-		this.router.removeRoutes(options);
+		this.removeRoutes(options);
 		return this.mockClear();
 	}
 	mockRestore(options?: RemoveRouteOptions) {
Repository: wheresrhys/fetch-mock
Pull request #921 (2025-03-03 17:12:36)

fix: allow matching body for delete requests
Request bodies were ignored for DELETE requests (along with GET and HEAD requests) in #749, but DELETE requests can specify a body and it works with the `fetch-mock` library. As per [this MDN reference](https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch), only GET and HEAD requests cannot specify a body.

A test was added to verify that this works and this was also verified in a consuming library.



Patch:
diff --git a/packages/fetch-mock/src/Matchers.ts b/packages/fetch-mock/src/Matchers.ts
index 48ff0d2..b43148d 100644
--- a/packages/fetch-mock/src/Matchers.ts
+++ b/packages/fetch-mock/src/Matchers.ts
@@ -212,7 +212,7 @@ const getBodyMatcher: MatcherGenerator = (route) => {
 	}
 
 	return ({ options: { body, method = 'get' } }) => {
-		if (['get', 'head', 'delete'].includes(method.toLowerCase())) {
+		if (['get', 'head'].includes(method.toLowerCase())) {
 			// GET requests don’t send a body so even if it exists in the options
 			// we treat as no body because it would never actually make it to the server
 			// in the application code


Test patch:
diff --git a/packages/fetch-mock/src/__tests__/Matchers/body.test.js b/packages/fetch-mock/src/__tests__/Matchers/body.test.js
index c3c6d6e..f91f991 100644
--- a/packages/fetch-mock/src/__tests__/Matchers/body.test.js
+++ b/packages/fetch-mock/src/__tests__/Matchers/body.test.js
@@ -144,6 +144,25 @@ describe('body matching', () => {
 			).toBe(true);
 		});
 
+		it('should match if request is delete', () => {
+			const route = new Route({
+				body: { foo: 'bar' },
+				method: 'delete',
+				response: 200,
+			});
+
+			expect(
+				route.matcher({
+					url: 'http://a.com/',
+					options: {
+						method: 'DELETE',
+						body: JSON.stringify({ foo: 'bar' }),
+						headers: { 'Content-Type': 'application/json' },
+					},
+				}),
+			).toBe(true);
+		});
+
 		describe('partial body matching', () => {
 			it('match when missing properties', () => {
 				const route = new Route({