From 779fb6feee483a6e2fc0698a4120f7927cef2ef4 Mon Sep 17 00:00:00 2001 From: Diego Aquino Date: Sun, 17 Mar 2024 12:06:40 -0300 Subject: [PATCH] chore: sync `canary` with `v0.2.x` (#107) This pull request is migrating the following changes from `v0.2.x` to `canary`: * [fix: bypassed requests](https://github.com/diego-aquino/zimic/pull/105) (#104) --- .../__tests__/shared/methods/delete.ts | 11 ++++---- .../__tests__/shared/methods/get.ts | 11 ++++---- .../__tests__/shared/methods/head.ts | 11 ++++---- .../__tests__/shared/methods/options.ts | 11 ++++---- .../__tests__/shared/methods/patch.ts | 11 ++++---- .../__tests__/shared/methods/post.ts | 11 ++++---- .../__tests__/shared/methods/put.ts | 11 ++++---- .../HttpInterceptorWorker.ts | 6 +++-- .../__tests__/shared/workerTests.ts | 17 ++++++------ .../__tests__/shared/requestTrackerTests.ts | 27 ++++++++++++------- packages/zimic/tests/utils/fetch.ts | 18 +++++++++++++ 11 files changed, 91 insertions(+), 54 deletions(-) create mode 100644 packages/zimic/tests/utils/fetch.ts diff --git a/packages/zimic/src/interceptor/http/interceptor/__tests__/shared/methods/delete.ts b/packages/zimic/src/interceptor/http/interceptor/__tests__/shared/methods/delete.ts index 586e72da8..5835d2f60 100644 --- a/packages/zimic/src/interceptor/http/interceptor/__tests__/shared/methods/delete.ts +++ b/packages/zimic/src/interceptor/http/interceptor/__tests__/shared/methods/delete.ts @@ -5,6 +5,7 @@ import HttpSearchParams from '@/http/searchParams/HttpSearchParams'; import { createHttpInterceptorWorker } from '@/interceptor/http/interceptorWorker/factory'; import HttpInterceptorWorker from '@/interceptor/http/interceptorWorker/HttpInterceptorWorker'; import HttpRequestTracker from '@/interceptor/http/requestTracker/HttpRequestTracker'; +import { expectToThrowFetchError } from '@tests/utils/fetch'; import { usingHttpInterceptor } from '@tests/utils/interceptors'; import { HttpInterceptorSchema } from '../../../types/schema'; @@ -434,7 +435,7 @@ export function declareDeleteHttpInterceptorTests({ platform }: SharedHttpInterc expect(specificDeletionRequest.response.body).toEqual(users[0]); const unmatchedDeletionPromise = fetch(`${baseURL}/users/${2}`, { method: 'DELETE' }); - await expect(unmatchedDeletionPromise).rejects.toThrowError(); + await expectToThrowFetchError(unmatchedDeletionPromise); }); }); @@ -455,7 +456,7 @@ export function declareDeleteHttpInterceptorTests({ platform }: SharedHttpInterc method: 'DELETE', body: JSON.stringify({ name: userName } satisfies User), }); - await expect(deletionPromise).rejects.toThrowError(); + await expectToThrowFetchError(deletionPromise); const deletionTrackerWithoutResponse = interceptor.delete(`/users/:id`); expect(deletionTrackerWithoutResponse).toBeInstanceOf(HttpRequestTracker); @@ -471,7 +472,7 @@ export function declareDeleteHttpInterceptorTests({ platform }: SharedHttpInterc method: 'DELETE', body: JSON.stringify({ name: userName } satisfies User), }); - await expect(deletionPromise).rejects.toThrowError(); + await expectToThrowFetchError(deletionPromise); expect(deletionRequestsWithoutResponse).toHaveLength(0); @@ -617,7 +618,7 @@ export function declareDeleteHttpInterceptorTests({ platform }: SharedHttpInterc expect(initialDeletionRequests).toHaveLength(0); const deletionPromise = fetch(`${baseURL}/users/${1}`, { method: 'DELETE' }); - await expect(deletionPromise).rejects.toThrowError(); + await expectToThrowFetchError(deletionPromise); deletionTracker.respond({ status: 200, @@ -722,7 +723,7 @@ export function declareDeleteHttpInterceptorTests({ platform }: SharedHttpInterc expect(initialDeletionRequests).toHaveLength(0); const deletionPromise = fetch(`${baseURL}/users/${1}`, { method: 'DELETE' }); - await expect(deletionPromise).rejects.toThrowError(); + await expectToThrowFetchError(deletionPromise); }); }); diff --git a/packages/zimic/src/interceptor/http/interceptor/__tests__/shared/methods/get.ts b/packages/zimic/src/interceptor/http/interceptor/__tests__/shared/methods/get.ts index 91a552388..b563b785d 100644 --- a/packages/zimic/src/interceptor/http/interceptor/__tests__/shared/methods/get.ts +++ b/packages/zimic/src/interceptor/http/interceptor/__tests__/shared/methods/get.ts @@ -5,6 +5,7 @@ import HttpSearchParams from '@/http/searchParams/HttpSearchParams'; import { createHttpInterceptorWorker } from '@/interceptor/http/interceptorWorker/factory'; import HttpInterceptorWorker from '@/interceptor/http/interceptorWorker/HttpInterceptorWorker'; import HttpRequestTracker from '@/interceptor/http/requestTracker/HttpRequestTracker'; +import { expectToThrowFetchError } from '@tests/utils/fetch'; import { usingHttpInterceptor } from '@tests/utils/interceptors'; import { HttpInterceptorSchema } from '../../../types/schema'; @@ -454,7 +455,7 @@ export function declareGetHttpInterceptorTests({ platform }: SharedHttpIntercept expect(specificGetRequest.response.body).toEqual(users[0]); const unmatchedGetPromise = fetch(`${baseURL}/users/${2}`, { method: 'GET' }); - await expect(unmatchedGetPromise).rejects.toThrowError(); + await expectToThrowFetchError(unmatchedGetPromise); }); }); @@ -469,7 +470,7 @@ export function declareGetHttpInterceptorTests({ platform }: SharedHttpIntercept }; }>({ worker, baseURL }, async (interceptor) => { let fetchPromise = fetch(`${baseURL}/users`, { method: 'GET' }); - await expect(fetchPromise).rejects.toThrowError(); + await expectToThrowFetchError(fetchPromise); const listTrackerWithoutResponse = interceptor.get('/users'); expect(listTrackerWithoutResponse).toBeInstanceOf(HttpRequestTracker); @@ -482,7 +483,7 @@ export function declareGetHttpInterceptorTests({ platform }: SharedHttpIntercept expectTypeOf().toEqualTypeOf(); fetchPromise = fetch(`${baseURL}/users`, { method: 'GET' }); - await expect(fetchPromise).rejects.toThrowError(); + await expectToThrowFetchError(fetchPromise); expect(listRequestsWithoutResponse).toHaveLength(0); @@ -626,7 +627,7 @@ export function declareGetHttpInterceptorTests({ platform }: SharedHttpIntercept expect(initialListRequests).toHaveLength(0); const listPromise = fetch(`${baseURL}/users`, { method: 'GET' }); - await expect(listPromise).rejects.toThrowError(); + await expectToThrowFetchError(listPromise); listTracker.respond({ status: 200, @@ -731,7 +732,7 @@ export function declareGetHttpInterceptorTests({ platform }: SharedHttpIntercept expect(initialListRequests).toHaveLength(0); const listPromise = fetch(`${baseURL}/users`, { method: 'GET' }); - await expect(listPromise).rejects.toThrowError(); + await expectToThrowFetchError(listPromise); }); }); diff --git a/packages/zimic/src/interceptor/http/interceptor/__tests__/shared/methods/head.ts b/packages/zimic/src/interceptor/http/interceptor/__tests__/shared/methods/head.ts index 3ff756460..4cefd4ef0 100644 --- a/packages/zimic/src/interceptor/http/interceptor/__tests__/shared/methods/head.ts +++ b/packages/zimic/src/interceptor/http/interceptor/__tests__/shared/methods/head.ts @@ -5,6 +5,7 @@ import HttpSearchParams from '@/http/searchParams/HttpSearchParams'; import { createHttpInterceptorWorker } from '@/interceptor/http/interceptorWorker/factory'; import HttpInterceptorWorker from '@/interceptor/http/interceptorWorker/HttpInterceptorWorker'; import HttpRequestTracker from '@/interceptor/http/requestTracker/HttpRequestTracker'; +import { expectToThrowFetchError } from '@tests/utils/fetch'; import { usingHttpInterceptor } from '@tests/utils/interceptors'; import { HttpInterceptorSchema } from '../../../types/schema'; @@ -399,7 +400,7 @@ export function declareHeadHttpInterceptorTests({ platform }: SharedHttpIntercep expect(specificHeadRequest.response.body).toBe(null); const unmatchedHeadPromise = fetch(`${baseURL}/users/${2}`, { method: 'HEAD' }); - await expect(unmatchedHeadPromise).rejects.toThrowError(); + await expectToThrowFetchError(unmatchedHeadPromise); }); }); @@ -414,7 +415,7 @@ export function declareHeadHttpInterceptorTests({ platform }: SharedHttpIntercep }; }>({ worker, baseURL }, async (interceptor) => { let fetchPromise = fetch(`${baseURL}/users`, { method: 'HEAD' }); - await expect(fetchPromise).rejects.toThrowError(); + await expectToThrowFetchError(fetchPromise); const headTrackerWithoutResponse = interceptor.head('/users'); expect(headTrackerWithoutResponse).toBeInstanceOf(HttpRequestTracker); @@ -427,7 +428,7 @@ export function declareHeadHttpInterceptorTests({ platform }: SharedHttpIntercep expectTypeOf().toEqualTypeOf(); fetchPromise = fetch(`${baseURL}/users`, { method: 'HEAD' }); - await expect(fetchPromise).rejects.toThrowError(); + await expectToThrowFetchError(fetchPromise); expect(headRequestsWithoutResponse).toHaveLength(0); @@ -563,7 +564,7 @@ export function declareHeadHttpInterceptorTests({ platform }: SharedHttpIntercep expect(initialHeadRequests).toHaveLength(0); const headPromise = fetch(`${baseURL}/users`, { method: 'HEAD' }); - await expect(headPromise).rejects.toThrowError(); + await expectToThrowFetchError(headPromise); const noContentHeadTracker = headTracker.respond({ status: 204, @@ -661,7 +662,7 @@ export function declareHeadHttpInterceptorTests({ platform }: SharedHttpIntercep expect(initialHeadRequests).toHaveLength(0); const headPromise = fetch(`${baseURL}/users`, { method: 'HEAD' }); - await expect(headPromise).rejects.toThrowError(); + await expectToThrowFetchError(headPromise); }); }); diff --git a/packages/zimic/src/interceptor/http/interceptor/__tests__/shared/methods/options.ts b/packages/zimic/src/interceptor/http/interceptor/__tests__/shared/methods/options.ts index be3fba5a2..a92b0c716 100644 --- a/packages/zimic/src/interceptor/http/interceptor/__tests__/shared/methods/options.ts +++ b/packages/zimic/src/interceptor/http/interceptor/__tests__/shared/methods/options.ts @@ -5,6 +5,7 @@ import HttpSearchParams from '@/http/searchParams/HttpSearchParams'; import { createHttpInterceptorWorker } from '@/interceptor/http/interceptorWorker/factory'; import HttpInterceptorWorker from '@/interceptor/http/interceptorWorker/HttpInterceptorWorker'; import HttpRequestTracker from '@/interceptor/http/requestTracker/HttpRequestTracker'; +import { expectToThrowFetchError } from '@tests/utils/fetch'; import { usingHttpInterceptor } from '@tests/utils/interceptors'; import { HttpInterceptorSchema } from '../../../types/schema'; @@ -414,7 +415,7 @@ export function declareOptionsHttpInterceptorTests({ platform }: SharedHttpInter expect(specificOptionsRequest.response.body).toBe(null); const unmatchedOptionsPromise = fetch(`${baseURL}/filters/${2}`, { method: 'OPTIONS' }); - await expect(unmatchedOptionsPromise).rejects.toThrowError(); + await expectToThrowFetchError(unmatchedOptionsPromise); }); }); @@ -429,7 +430,7 @@ export function declareOptionsHttpInterceptorTests({ platform }: SharedHttpInter }; }>({ worker, baseURL }, async (interceptor) => { let fetchPromise = fetch(`${baseURL}/filters`, { method: 'OPTIONS' }); - await expect(fetchPromise).rejects.toThrowError(); + await expectToThrowFetchError(fetchPromise); const optionsTrackerWithoutResponse = interceptor.options('/filters'); expect(optionsTrackerWithoutResponse).toBeInstanceOf(HttpRequestTracker); @@ -442,7 +443,7 @@ export function declareOptionsHttpInterceptorTests({ platform }: SharedHttpInter expectTypeOf().toEqualTypeOf(); fetchPromise = fetch(`${baseURL}/filters`, { method: 'OPTIONS' }); - await expect(fetchPromise).rejects.toThrowError(); + await expectToThrowFetchError(fetchPromise); expect(optionsRequestsWithoutResponse).toHaveLength(0); @@ -578,7 +579,7 @@ export function declareOptionsHttpInterceptorTests({ platform }: SharedHttpInter expect(initialOptionsRequests).toHaveLength(0); const optionsPromise = fetch(`${baseURL}/filters`, { method: 'OPTIONS' }); - await expect(optionsPromise).rejects.toThrowError(); + await expectToThrowFetchError(optionsPromise); const noContentOptionsTracker = optionsTracker.respond({ status: 204, @@ -676,7 +677,7 @@ export function declareOptionsHttpInterceptorTests({ platform }: SharedHttpInter expect(initialOptionsRequests).toHaveLength(0); const optionsPromise = fetch(`${baseURL}/filters`, { method: 'OPTIONS' }); - await expect(optionsPromise).rejects.toThrowError(); + await expectToThrowFetchError(optionsPromise); }); }); diff --git a/packages/zimic/src/interceptor/http/interceptor/__tests__/shared/methods/patch.ts b/packages/zimic/src/interceptor/http/interceptor/__tests__/shared/methods/patch.ts index daaa180b2..eca6e7f53 100644 --- a/packages/zimic/src/interceptor/http/interceptor/__tests__/shared/methods/patch.ts +++ b/packages/zimic/src/interceptor/http/interceptor/__tests__/shared/methods/patch.ts @@ -5,6 +5,7 @@ import HttpSearchParams from '@/http/searchParams/HttpSearchParams'; import { createHttpInterceptorWorker } from '@/interceptor/http/interceptorWorker/factory'; import HttpInterceptorWorker from '@/interceptor/http/interceptorWorker/HttpInterceptorWorker'; import HttpRequestTracker from '@/interceptor/http/requestTracker/HttpRequestTracker'; +import { expectToThrowFetchError } from '@tests/utils/fetch'; import { usingHttpInterceptor } from '@tests/utils/interceptors'; import { HttpInterceptorSchema } from '../../../types/schema'; @@ -435,7 +436,7 @@ export function declarePatchHttpInterceptorTests({ platform }: SharedHttpInterce expect(specificUpdateRequest.response.body).toEqual(users[0]); const unmatchedUpdatePromise = fetch(`${baseURL}/users/${2}`, { method: 'PATCH' }); - await expect(unmatchedUpdatePromise).rejects.toThrowError(); + await expectToThrowFetchError(unmatchedUpdatePromise); }); }); @@ -456,7 +457,7 @@ export function declarePatchHttpInterceptorTests({ platform }: SharedHttpInterce method: 'PATCH', body: JSON.stringify({ name: userName } satisfies User), }); - await expect(updatePromise).rejects.toThrowError(); + await expectToThrowFetchError(updatePromise); const updateTrackerWithoutResponse = interceptor.patch('/users'); expect(updateTrackerWithoutResponse).toBeInstanceOf(HttpRequestTracker); @@ -472,7 +473,7 @@ export function declarePatchHttpInterceptorTests({ platform }: SharedHttpInterce method: 'PATCH', body: JSON.stringify({ name: userName } satisfies User), }); - await expect(updatePromise).rejects.toThrowError(); + await expectToThrowFetchError(updatePromise); expect(updateRequestsWithoutResponse).toHaveLength(0); @@ -618,7 +619,7 @@ export function declarePatchHttpInterceptorTests({ platform }: SharedHttpInterce expect(initialUpdateRequests).toHaveLength(0); const updatePromise = fetch(`${baseURL}/users`, { method: 'PATCH' }); - await expect(updatePromise).rejects.toThrowError(); + await expectToThrowFetchError(updatePromise); updateTracker.respond({ status: 200, @@ -723,7 +724,7 @@ export function declarePatchHttpInterceptorTests({ platform }: SharedHttpInterce expect(initialUpdateRequests).toHaveLength(0); const updatePromise = fetch(`${baseURL}/users`, { method: 'PATCH' }); - await expect(updatePromise).rejects.toThrowError(); + await expectToThrowFetchError(updatePromise); }); }); diff --git a/packages/zimic/src/interceptor/http/interceptor/__tests__/shared/methods/post.ts b/packages/zimic/src/interceptor/http/interceptor/__tests__/shared/methods/post.ts index e313066f9..f5ebc6699 100644 --- a/packages/zimic/src/interceptor/http/interceptor/__tests__/shared/methods/post.ts +++ b/packages/zimic/src/interceptor/http/interceptor/__tests__/shared/methods/post.ts @@ -5,6 +5,7 @@ import HttpSearchParams from '@/http/searchParams/HttpSearchParams'; import { createHttpInterceptorWorker } from '@/interceptor/http/interceptorWorker/factory'; import HttpInterceptorWorker from '@/interceptor/http/interceptorWorker/HttpInterceptorWorker'; import HttpRequestTracker from '@/interceptor/http/requestTracker/HttpRequestTracker'; +import { expectToThrowFetchError } from '@tests/utils/fetch'; import { usingHttpInterceptor } from '@tests/utils/interceptors'; import { HttpInterceptorSchema } from '../../../types/schema'; @@ -441,7 +442,7 @@ export function declarePostHttpInterceptorTests({ platform }: SharedHttpIntercep expect(specificCreationRequest.response.body).toEqual(users[0]); const unmatchedCreationPromise = fetch(`${baseURL}/users/${2}`, { method: 'POST' }); - await expect(unmatchedCreationPromise).rejects.toThrowError(); + await expectToThrowFetchError(unmatchedCreationPromise); }); }); @@ -462,7 +463,7 @@ export function declarePostHttpInterceptorTests({ platform }: SharedHttpIntercep method: 'POST', body: JSON.stringify({ name: userName } satisfies User), }); - await expect(creationPromise).rejects.toThrowError(); + await expectToThrowFetchError(creationPromise); const creationTrackerWithoutResponse = interceptor.post('/users'); expect(creationTrackerWithoutResponse).toBeInstanceOf(HttpRequestTracker); @@ -478,7 +479,7 @@ export function declarePostHttpInterceptorTests({ platform }: SharedHttpIntercep method: 'POST', body: JSON.stringify({ name: userName } satisfies User), }); - await expect(creationPromise).rejects.toThrowError(); + await expectToThrowFetchError(creationPromise); expect(creationRequestsWithoutResponse).toHaveLength(0); @@ -629,7 +630,7 @@ export function declarePostHttpInterceptorTests({ platform }: SharedHttpIntercep expect(initialCreationRequests).toHaveLength(0); const creationPromise = fetch(`${baseURL}/users`, { method: 'POST' }); - await expect(creationPromise).rejects.toThrowError(); + await expectToThrowFetchError(creationPromise); creationTracker.respond({ status: 201, @@ -734,7 +735,7 @@ export function declarePostHttpInterceptorTests({ platform }: SharedHttpIntercep expect(initialCreationRequests).toHaveLength(0); const creationPromise = fetch(`${baseURL}/users`, { method: 'POST' }); - await expect(creationPromise).rejects.toThrowError(); + await expectToThrowFetchError(creationPromise); }); }); diff --git a/packages/zimic/src/interceptor/http/interceptor/__tests__/shared/methods/put.ts b/packages/zimic/src/interceptor/http/interceptor/__tests__/shared/methods/put.ts index cf1a58116..a1806cbb3 100644 --- a/packages/zimic/src/interceptor/http/interceptor/__tests__/shared/methods/put.ts +++ b/packages/zimic/src/interceptor/http/interceptor/__tests__/shared/methods/put.ts @@ -5,6 +5,7 @@ import HttpSearchParams from '@/http/searchParams/HttpSearchParams'; import { createHttpInterceptorWorker } from '@/interceptor/http/interceptorWorker/factory'; import HttpInterceptorWorker from '@/interceptor/http/interceptorWorker/HttpInterceptorWorker'; import HttpRequestTracker from '@/interceptor/http/requestTracker/HttpRequestTracker'; +import { expectToThrowFetchError } from '@tests/utils/fetch'; import { usingHttpInterceptor } from '@tests/utils/interceptors'; import { HttpInterceptorSchema } from '../../../types/schema'; @@ -435,7 +436,7 @@ export function declarePutHttpInterceptorTests({ platform }: SharedHttpIntercept expect(specificUpdateRequest.response.body).toEqual(users[0]); const unmatchedUpdatePromise = fetch(`${baseURL}/users/${2}`, { method: 'PUT' }); - await expect(unmatchedUpdatePromise).rejects.toThrowError(); + await expectToThrowFetchError(unmatchedUpdatePromise); }); }); @@ -456,7 +457,7 @@ export function declarePutHttpInterceptorTests({ platform }: SharedHttpIntercept method: 'PUT', body: JSON.stringify({ name: userName } satisfies User), }); - await expect(updatePromise).rejects.toThrowError(); + await expectToThrowFetchError(updatePromise); const updateTrackerWithoutResponse = interceptor.put('/users'); expect(updateTrackerWithoutResponse).toBeInstanceOf(HttpRequestTracker); @@ -472,7 +473,7 @@ export function declarePutHttpInterceptorTests({ platform }: SharedHttpIntercept method: 'PUT', body: JSON.stringify({ name: userName } satisfies User), }); - await expect(updatePromise).rejects.toThrowError(); + await expectToThrowFetchError(updatePromise); expect(updateRequestsWithoutResponse).toHaveLength(0); @@ -618,7 +619,7 @@ export function declarePutHttpInterceptorTests({ platform }: SharedHttpIntercept expect(initialUpdateRequests).toHaveLength(0); const updatePromise = fetch(`${baseURL}/users`, { method: 'PUT' }); - await expect(updatePromise).rejects.toThrowError(); + await expectToThrowFetchError(updatePromise); updateTracker.respond({ status: 200, @@ -723,7 +724,7 @@ export function declarePutHttpInterceptorTests({ platform }: SharedHttpIntercept expect(initialUpdateRequests).toHaveLength(0); const updatePromise = fetch(`${baseURL}/users`, { method: 'PUT' }); - await expect(updatePromise).rejects.toThrowError(); + await expectToThrowFetchError(updatePromise); }); }); diff --git a/packages/zimic/src/interceptor/http/interceptorWorker/HttpInterceptorWorker.ts b/packages/zimic/src/interceptor/http/interceptorWorker/HttpInterceptorWorker.ts index 18c661c48..dab855cba 100644 --- a/packages/zimic/src/interceptor/http/interceptorWorker/HttpInterceptorWorker.ts +++ b/packages/zimic/src/interceptor/http/interceptorWorker/HttpInterceptorWorker.ts @@ -237,8 +237,9 @@ class HttpInterceptorWorker implements PublicHttpInterceptorWorker { } static async parseRawRequest( - rawRequest: HttpRequest, + originalRawRequest: HttpRequest, ): Promise> { + const rawRequest = originalRawRequest.clone(); const rawRequestClone = rawRequest.clone(); type BodySchema = Default['body']>; @@ -289,7 +290,8 @@ class HttpInterceptorWorker implements PublicHttpInterceptorWorker { static async parseRawResponse< MethodSchema extends HttpInterceptorMethodSchema, StatusCode extends HttpInterceptorResponseSchemaStatusCode>, - >(rawResponse: HttpResponse): Promise> { + >(originalRawResponse: HttpResponse): Promise> { + const rawResponse = originalRawResponse.clone(); const rawResponseClone = rawResponse.clone(); type BodySchema = Default[StatusCode]['body']>; diff --git a/packages/zimic/src/interceptor/http/interceptorWorker/__tests__/shared/workerTests.ts b/packages/zimic/src/interceptor/http/interceptorWorker/__tests__/shared/workerTests.ts index 96d7e771c..ed7534790 100644 --- a/packages/zimic/src/interceptor/http/interceptorWorker/__tests__/shared/workerTests.ts +++ b/packages/zimic/src/interceptor/http/interceptorWorker/__tests__/shared/workerTests.ts @@ -3,6 +3,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { createHttpInterceptor } from '@/interceptor/http/interceptor/factory'; import { fetchWithTimeout } from '@/utils/fetch'; import { waitForDelay } from '@/utils/time'; +import { expectToThrowFetchError } from '@tests/utils/fetch'; import { HTTP_INTERCEPTOR_METHODS, HttpInterceptorSchema } from '../../../interceptor/types/schema'; import InvalidHttpInterceptorWorkerPlatform from '../../errors/InvalidHttpInterceptorWorkerPlatform'; @@ -208,7 +209,7 @@ export function declareSharedHttpInterceptorWorkerTests(options: { platform: Htt spiedRequestHandler.mockClear(); const unmatchedResponsePromise = fetch(`${baseURL}/path/${2}`, { method }); - await expect(unmatchedResponsePromise).rejects.toThrowError(); + await expectToThrowFetchError(unmatchedResponsePromise); expect(spiedRequestHandler).toHaveBeenCalledTimes(0); }); @@ -225,7 +226,7 @@ export function declareSharedHttpInterceptorWorkerTests(options: { platform: Htt expect(bypassedSpiedRequestHandler).not.toHaveBeenCalled(); const fetchPromise = fetch(`${baseURL}/path`, { method }); - await expect(fetchPromise).rejects.toThrowError(); + await expectToThrowFetchError(fetchPromise); expect(bypassedSpiedRequestHandler).toHaveBeenCalledTimes(1); @@ -251,7 +252,7 @@ export function declareSharedHttpInterceptorWorkerTests(options: { platform: Htt expect(delayedSpiedRequestHandler).not.toHaveBeenCalled(); let fetchPromise = fetchWithTimeout(`${baseURL}/path`, { method, timeout: 50 }); - await expect(fetchPromise).rejects.toThrowError(); + await expectToThrowFetchError(fetchPromise, { canBeAborted: true }); fetchPromise = fetchWithTimeout(`${baseURL}/path`, { method, timeout: 200 }); await expect(fetchPromise).resolves.toBeInstanceOf(Response); @@ -277,7 +278,7 @@ export function declareSharedHttpInterceptorWorkerTests(options: { platform: Htt expect(spiedRequestHandler).not.toHaveBeenCalled(); const fetchPromise = fetchWithTimeout(`${baseURL}/path`, { method, timeout: 200 }); - await expect(fetchPromise).rejects.toThrowError(); + await expectToThrowFetchError(fetchPromise, { canBeAborted: true }); expect(spiedRequestHandler).not.toHaveBeenCalled(); }); @@ -292,7 +293,7 @@ export function declareSharedHttpInterceptorWorkerTests(options: { platform: Htt await interceptorWorker.stop(); const fetchPromise = fetchWithTimeout(`${baseURL}/path`, { method, timeout: 200 }); - await expect(fetchPromise).rejects.toThrowError(); + await expectToThrowFetchError(fetchPromise, { canBeAborted: true }); expect(spiedRequestHandler).not.toHaveBeenCalled(); }); @@ -308,7 +309,7 @@ export function declareSharedHttpInterceptorWorkerTests(options: { platform: Htt await interceptorWorker.start(); const fetchPromise = fetchWithTimeout(`${baseURL}/path`, { method, timeout: 200 }); - await expect(fetchPromise).rejects.toThrowError(); + await expectToThrowFetchError(fetchPromise, { canBeAborted: true }); expect(spiedRequestHandler).not.toHaveBeenCalled(); }); @@ -323,7 +324,7 @@ export function declareSharedHttpInterceptorWorkerTests(options: { platform: Htt interceptorWorker.clearHandlers(); const fetchPromise = fetchWithTimeout(`${baseURL}/path`, { method, timeout: 200 }); - await expect(fetchPromise).rejects.toThrowError(); + await expectToThrowFetchError(fetchPromise, { canBeAborted: true }); expect(spiedRequestHandler).not.toHaveBeenCalled(); @@ -423,7 +424,7 @@ export function declareSharedHttpInterceptorWorkerTests(options: { platform: Htt expect(interceptorsWithHandlers).toHaveLength(0); const fetchPromise = fetchWithTimeout(`${baseURL}/path`, { method, timeout: 200 }); - await expect(fetchPromise).rejects.toThrowError(); + await expectToThrowFetchError(fetchPromise, { canBeAborted: true }); expect(okSpiedRequestHandler).toHaveBeenCalledTimes(2); expect(noContentSpiedRequestHandler).toHaveBeenCalledTimes(1); diff --git a/packages/zimic/src/interceptor/http/requestTracker/__tests__/shared/requestTrackerTests.ts b/packages/zimic/src/interceptor/http/requestTracker/__tests__/shared/requestTrackerTests.ts index e46b782d9..4c5bcc576 100644 --- a/packages/zimic/src/interceptor/http/requestTracker/__tests__/shared/requestTrackerTests.ts +++ b/packages/zimic/src/interceptor/http/requestTracker/__tests__/shared/requestTrackerTests.ts @@ -190,7 +190,6 @@ export function declareSharedHttpRequestTrackerTests(options: { platform: HttpIn expect(response.body).toEqual(responseBody); expect(responseFactory).toHaveBeenCalledTimes(1); - expect(responseFactory).toHaveBeenCalledWith(request); }); it('should throw an error if trying to create a response without a declared response', async () => { @@ -217,6 +216,7 @@ export function declareSharedHttpRequestTrackerTests(options: { platform: HttpIn const firstResponse = Response.json(firstResponseDeclaration.body, { status: firstResponseDeclaration.status, }); + const firstResponseClone = firstResponse.clone(); const parsedFirstResponse = await HttpInterceptorWorker.parseRawResponse(firstResponse); tracker.registerInterceptedRequest(parsedFirstRequest, parsedFirstResponse); @@ -224,8 +224,10 @@ export function declareSharedHttpRequestTrackerTests(options: { platform: HttpIn const interceptedRequests = tracker.requests(); expect(interceptedRequests).toHaveLength(1); - expect(interceptedRequests[0]).toEqual(firstRequest); - expect(interceptedRequests[0].response).toEqual(firstResponse); + expect(interceptedRequests[0].url).toEqual(firstRequest.url); + expect(interceptedRequests[0].method).toEqual(firstRequest.method); + expect(interceptedRequests[0].response.status).toEqual(firstResponse.status); + expect(interceptedRequests[0].response.body).toEqual(await firstResponse.json()); const secondRequest = new Request(`${baseURL}/path`); const parsedSecondRequest = await HttpInterceptorWorker.parseRawRequest(secondRequest); @@ -240,11 +242,15 @@ export function declareSharedHttpRequestTrackerTests(options: { platform: HttpIn expect(interceptedRequests).toHaveLength(2); - expect(interceptedRequests[0]).toEqual(firstRequest); - expect(interceptedRequests[0].response).toEqual(firstResponse); + expect(interceptedRequests[0].url).toEqual(firstRequest.url); + expect(interceptedRequests[0].method).toEqual(firstRequest.method); + expect(interceptedRequests[0].response.status).toEqual(firstResponse.status); + expect(interceptedRequests[0].response.body).toEqual(await firstResponseClone.json()); - expect(interceptedRequests[1]).toEqual(secondRequest); - expect(interceptedRequests[1].response).toEqual(secondResponse); + expect(interceptedRequests[1].url).toEqual(secondRequest.url); + expect(interceptedRequests[1].method).toEqual(secondRequest.method); + expect(interceptedRequests[1].response.status).toEqual(secondResponse.status); + expect(interceptedRequests[1].response.body).toEqual(await secondResponse.json()); }); it('should clear the intercepted requests and responses after cleared', async () => { @@ -260,6 +266,7 @@ export function declareSharedHttpRequestTrackerTests(options: { platform: HttpIn const firstResponse = Response.json(firstResponseDeclaration.body, { status: firstResponseDeclaration.status, }); + const firstResponseClone = firstResponse.clone(); const parsedFirstResponse = await HttpInterceptorWorker.parseRawResponse(firstResponse); tracker.registerInterceptedRequest(parsedFirstRequest, parsedFirstResponse); @@ -267,8 +274,10 @@ export function declareSharedHttpRequestTrackerTests(options: { platform: HttpIn const interceptedRequests = tracker.requests(); expect(interceptedRequests).toHaveLength(1); - expect(interceptedRequests[0]).toEqual(firstRequest); - expect(interceptedRequests[0].response).toEqual(firstResponse); + expect(interceptedRequests[0].url).toEqual(firstRequest.url); + expect(interceptedRequests[0].method).toEqual(firstRequest.method); + expect(interceptedRequests[0].response.status).toEqual(firstResponse.status); + expect(interceptedRequests[0].response.body).toEqual(await firstResponseClone.json()); tracker.clear(); diff --git a/packages/zimic/tests/utils/fetch.ts b/packages/zimic/tests/utils/fetch.ts new file mode 100644 index 000000000..201e4c47e --- /dev/null +++ b/packages/zimic/tests/utils/fetch.ts @@ -0,0 +1,18 @@ +import { expect } from 'vitest'; + +export async function expectToThrowFetchError( + value: Promise | (() => Promise), + options: { canBeAborted?: boolean } = {}, +) { + const { canBeAborted = false } = options; + + const errorMessageOptions = [ + 'fetch failed', + 'Failed to fetch', + canBeAborted && 'This operation was aborted', + canBeAborted && 'The user aborted a request.', + ].filter((option): option is string => typeof option === 'string'); + + const errorMessageExpression = new RegExp(`^${errorMessageOptions.join('|')}$`); + await expect(value).rejects.toThrowError(errorMessageExpression); +}