Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(handleRequest): remove transformResponse option #2351

Merged
merged 2 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading