-
Notifications
You must be signed in to change notification settings - Fork 10
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
Invalid response status after update to 0.4.0 #25
Comments
Took me a moment to figure out what's happening here, but I found out. Before, we were relying on import { Response } from 'cross-fetch';
new Response('', { status: 204 }) which works fine. However, when running
This is by design. There are two solutions:
// either:
fetchMocker.mockResponseOnce(() => ({ status: 204 }));
// or:
fetchMocker.mockResponseOnce(() => new Response(null, { status: 204 }));
@IanVS let me know what you think. I'm willing to make a fix if we agree on solution 2. |
Aside from making the response body nullable in fetchMocker.mockResponseOnce({ status: 204 });
fetchMocker.mockResponseOnce(new Response(null, { status: 204 })); To allow passing null as response body, I opened a separate MR. |
This PR makes it possible to use more types of overloaded functions: ```ts fetch.mockResponseOnce('x'); fetch.mockResponseOnce({ body: 'x' }); // previously not allowed fetch.mockResponseOnce(new Response('x')); // previously not allowed fetch.mockResponseOnce(() => 'x'); fetch.mockResponseOnce(() => ({ body: 'x' })); fetch.mockResponseOnce(() => new Response('x')); fetch.mockResponseOnce(() => Promise.resolve('x')); fetch.mockResponseOnce(() => Promise.resolve({ body: 'x' })); fetch.mockResponseOnce(() => Promise.resolve(new Response('x'))); ``` This could make certain cases less awkward, like #25 (comment): ```ts fetch.mockResponseOnce({ status: 204 }); fetch.mockResponseOnce(new Response(null, { status: 204 })); ```
The fix has been released in v0.4.1. |
Thank you @dirkluijk I updated here and everything works fine now with 0.4.1 :) |
I ran in problem with the newer version.
I have a test like this:
But when I run the tests, it give me the error:
It should accept send
null
as response body here:vitest-fetch-mock/src/index.ts
Line 307 in 6b4ba2e
type ResponseBody = string | null;
...and here:
vitest-fetch-mock/src/index.ts
Line 381 in 6b4ba2e
if (responseProviderOrBody === null || typeof responseProviderOrBody === 'string') {
So this will work:
The text was updated successfully, but these errors were encountered: