diff --git a/src/core/utils/handleRequest.test.ts b/src/core/utils/handleRequest.test.ts index e3bbf3c1e..5e9e896c5 100644 --- a/src/core/utils/handleRequest.test.ts +++ b/src/core/utils/handleRequest.test.ts @@ -1,6 +1,4 @@ -/** - * @vitest-environment jsdom - */ +// @vitest-environment jsdom import { Emitter } from 'strict-event-emitter' import { createRequestId } from '@mswjs/interceptors' import { LifeCycleEventsMap, SharedOptions } from '../sharedOptions' @@ -239,89 +237,6 @@ test('returns the mocked response for a request with a matching request handler' }) }) -test('returns a transformed response if the "transformResponse" option is provided', async () => { - const { emitter, events } = setup() - - const requestId = createRequestId() - const request = new Request(new URL('http://localhost/user')) - const mockedResponse = HttpResponse.json({ firstName: 'John' }) - const handlers: Array = [ - http.get('/user', () => { - return mockedResponse - }), - ] - const transformResponseImpelemntation = (response: Response): Response => { - return new Response('transformed', response) - } - const transformResponse = vi - .fn<[Response], Response>() - .mockImplementation(transformResponseImpelemntation) - const finalResponse = transformResponseImpelemntation(mockedResponse) - const lookupResult = { - handler: handlers[0], - response: mockedResponse, - request, - parsedResult: { - match: { matches: true, params: {} }, - cookies: {}, - }, - } - - const result = await handleRequest( - request, - requestId, - handlers, - options, - emitter, - { - ...handleRequestOptions, - transformResponse, - }, - ) - - expect(result?.status).toEqual(finalResponse.status) - expect(result?.statusText).toEqual(finalResponse.statusText) - expect(Object.fromEntries(result!.headers.entries())).toEqual( - Object.fromEntries(mockedResponse.headers.entries()), - ) - - expect(events).toEqual([ - ['request:start', { request, requestId }], - ['request:match', { request, requestId }], - ['request:end', { request, requestId }], - ]) - expect(handleRequestOptions.onPassthroughResponse).not.toHaveBeenCalled() - - expect(transformResponse).toHaveBeenCalledTimes(1) - const [responseParam] = transformResponse.mock.calls[0] - - expect(responseParam.status).toBe(mockedResponse.status) - expect(responseParam.statusText).toBe(mockedResponse.statusText) - expect(Object.fromEntries(responseParam.headers.entries())).toEqual( - Object.fromEntries(mockedResponse.headers.entries()), - ) - - expect(handleRequestOptions.onMockedResponse).toHaveBeenCalledTimes(1) - const [mockedResponseParam, lookupResultParam] = - handleRequestOptions.onMockedResponse.mock.calls[0] - - expect(mockedResponseParam.status).toBe(finalResponse.status) - expect(mockedResponseParam.statusText).toBe(finalResponse.statusText) - expect(Object.fromEntries(mockedResponseParam.headers.entries())).toEqual( - Object.fromEntries(mockedResponse.headers.entries()), - ) - expect(await mockedResponseParam.text()).toBe('transformed') - - expect(lookupResultParam).toEqual({ - handler: lookupResult.handler, - parsedResult: lookupResult.parsedResult, - response: expect.objectContaining({ - status: lookupResult.response.status, - statusText: lookupResult.response.statusText, - }), - }) -}) - it('returns undefined without warning on a passthrough request', async () => { const { emitter, events } = setup() diff --git a/src/core/utils/handleRequest.ts b/src/core/utils/handleRequest.ts index d78259c2e..1f5780bbc 100644 --- a/src/core/utils/handleRequest.ts +++ b/src/core/utils/handleRequest.ts @@ -22,12 +22,6 @@ export interface HandleRequestOptions { baseUrl?: string } - /** - * Transforms a `MockedResponse` instance returned from a handler - * to a response instance supported by the lower tooling (i.e. interceptors). - */ - transformResponse?(response: Response): Response - /** * Invoked whenever a request is performed as-is. */ @@ -118,16 +112,9 @@ export async function handleRequest( const requiredLookupResult = lookupResult.data as RequiredDeep - const transformedResponse = - handleRequestOptions?.transformResponse?.(response) || - (response as any as Response) - - handleRequestOptions?.onMockedResponse?.( - transformedResponse, - requiredLookupResult, - ) + handleRequestOptions?.onMockedResponse?.(response, requiredLookupResult) emitter.emit('request:end', { request, requestId }) - return transformedResponse + return response }