-
Notifications
You must be signed in to change notification settings - Fork 119
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
Please support all acceptable types for the Response constructor #218
Comments
Alternatively, you could allow for |
Hi @andreasg123! I went ahead and tried my hand at supporting this in #223. What do you think? |
Agh, took me a while to find this issue - this issue is nuking my tests in a cloudflare workers environment, where I'm attempting to test the result of
Also, I've found that if I make any calls in a given test file to I tried your fork @alexkolson (thanks!) but didn't manage to get it working for my case: let response = new Response(
createMinimalHtml(),
{ headers: { 'content-type': 'text/html' } }
)
fetchMock.enableMocks()
fetchMock.mockResponseOnce(response)
// (in the functional tests, fetch is called deep in another handler)
response = await fetch('/')
response = rewriteHtmlResponse(response, ctx) Tried various combinations of fudging types and passing responseInit per @andreasg123 suggestions, but no dice yet. fetchMock.mockResponseOnce(
(_req) =>
Promise.resolve({
body: response.body as unknown as string, // text/plain, no body.pipeTo
}),
// if using [Pull Request #223 · jefflau/jest-fetch-mock](https://github.com/jefflau/jest-fetch-mock/pull/223)
// response as unknown as string, // text/plain
// (req) => Promise.resolve(response) as unknown as string, // text/html, but no body.pipeTo
) |
In addition to
string
, theResponse
constructor supports several other types. That is important for mocking anarrayBuffer
response.Unfortunately, the typing for
MockResponseInitFunction
only supportsstring
. The following works but requires an ugly cast:As a bonus, it would be nice if these other types could be checked in addition to
string
to allow for the shorter form that doesn't require an object with abody
property.The text was updated successfully, but these errors were encountered: