Skip to content

Commit

Permalink
fix: remove transformResponse option
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Nov 6, 2024
1 parent 28d26bd commit 664c053
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 101 deletions.
87 changes: 1 addition & 86 deletions src/core/utils/handleRequest.test.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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<RequestHandler> = [
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()

Expand Down
17 changes: 2 additions & 15 deletions src/core/utils/handleRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -118,16 +112,9 @@ export async function handleRequest(
const requiredLookupResult =
lookupResult.data as RequiredDeep<HandlersExecutionResult>

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
}

0 comments on commit 664c053

Please sign in to comment.