Skip to content

Commit

Permalink
feat: allow passing null or undefined as response body
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkluijk committed Oct 28, 2024
1 parent 6133016 commit 8623768
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ type UrlOrPredicate = string | RegExp | ((input: Request) => boolean);
type RequestInput = string | URL | Request;
type ResponseProvider = ResponseLike | ((request: Request) => ResponseLike | Promise<ResponseLike>);
type ResponseLike = MockResponse | ResponseBody | Response;
type ResponseBody = string;
type ResponseBody = string | null | undefined;
type ErrorOrFunction = Error | ResponseBody | ResponseProvider;

export interface MockParams {
Expand Down Expand Up @@ -339,7 +339,7 @@ async function normalizeResponse(

if (responseLike instanceof Response) {
return responseLike;
} else if (typeof responseLike === 'string') {
} else if (typeof responseLike === 'string' || responseLike === null || responseLike === undefined) {
return new Response(responseLike, params);
} else {
return patchUrl(new Response(responseLike.body, { ...params, ...responseLike }), responseLike.url ?? params?.url);
Expand Down
36 changes: 36 additions & 0 deletions tests/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,42 @@ describe('testing mockResponse', () => {
expect(fetch.mock.calls.length).toEqual(1);
expect(fetch.mock.calls[0]![0]).toEqual(new URL('https://instagram.com'));
});

it('should allow empty response bodies', async () => {
fetch.mockResponseOnce(null, { status: 204 });
fetch.mockResponseOnce(undefined, { status: 204 });
fetch.mockResponseOnce(() => null, { status: 204 });
fetch.mockResponseOnce(() => undefined, { status: 204 });
fetch.mockResponseOnce(() => Promise.resolve(null), { status: 204 });
fetch.mockResponseOnce(() => Promise.resolve(undefined), { status: 204 });
fetch.mockResponseOnce({ status: 204 });
fetch.mockResponseOnce(() => ({ status: 204 }));
fetch.mockResponseOnce(() => Promise.resolve({ status: 204 }));
fetch.mockResponseOnce(new Response(null, { status: 204 }));
fetch.mockResponseOnce(new Response(undefined, { status: 204 }));
fetch.mockResponseOnce(() => new Response(null, { status: 204 }));
fetch.mockResponseOnce(() => new Response(undefined, { status: 204 }));
fetch.mockResponseOnce(() => Promise.resolve(new Response(null, { status: 204 })));
fetch.mockResponseOnce(() => Promise.resolve(new Response(undefined, { status: 204 })));
fetch.mockResponseOnce('done');

expect(await request()).toBe('');
expect(await request()).toBe('');
expect(await request()).toBe('');
expect(await request()).toBe('');
expect(await request()).toBe('');
expect(await request()).toBe('');
expect(await request()).toBe('');
expect(await request()).toBe('');
expect(await request()).toBe('');
expect(await request()).toBe('');
expect(await request()).toBe('');
expect(await request()).toBe('');
expect(await request()).toBe('');
expect(await request()).toBe('');
expect(await request()).toBe('');
expect(await request()).toBe('done');
});
});

describe('testing mockResponses', () => {
Expand Down

0 comments on commit 8623768

Please sign in to comment.