Skip to content

Commit 83816e5

Browse files
authored
feat: allow passing null or undefined as response body (#27)
Fixes #25
1 parent 46bbb06 commit 83816e5

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ type UrlOrPredicate = string | RegExp | ((input: Request) => boolean);
259259
type RequestInput = string | URL | Request;
260260
type ResponseProvider = ResponseLike | ((request: Request) => ResponseLike | Promise<ResponseLike>);
261261
type ResponseLike = MockResponse | ResponseBody | Response;
262-
type ResponseBody = string;
262+
type ResponseBody = string | null | undefined;
263263
type ErrorOrFunction = Error | ResponseBody | ResponseProvider;
264264

265265
export interface MockParams {
@@ -351,7 +351,7 @@ async function normalizeResponse(
351351

352352
if (responseLike instanceof Response) {
353353
return responseLike;
354-
} else if (typeof responseLike === 'string') {
354+
} else if (typeof responseLike === 'string' || responseLike === null || responseLike === undefined) {
355355
return new Response(responseLike, params);
356356
} else {
357357
return patchUrl(new Response(responseLike.body, { ...params, ...responseLike }), responseLike.url ?? params?.url);

tests/api.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,42 @@ describe('testing mockResponse', () => {
9999
expect(fetch.mock.calls.length).toEqual(1);
100100
expect(fetch.mock.calls[0]![0]).toEqual(new URL('https://instagram.com'));
101101
});
102+
103+
it('should allow empty response bodies', async () => {
104+
fetch.mockResponseOnce(null, { status: 204 });
105+
fetch.mockResponseOnce(undefined, { status: 204 });
106+
fetch.mockResponseOnce(() => null, { status: 204 });
107+
fetch.mockResponseOnce(() => undefined, { status: 204 });
108+
fetch.mockResponseOnce(() => Promise.resolve(null), { status: 204 });
109+
fetch.mockResponseOnce(() => Promise.resolve(undefined), { status: 204 });
110+
fetch.mockResponseOnce({ status: 204 });
111+
fetch.mockResponseOnce(() => ({ status: 204 }));
112+
fetch.mockResponseOnce(() => Promise.resolve({ status: 204 }));
113+
fetch.mockResponseOnce(new Response(null, { status: 204 }));
114+
fetch.mockResponseOnce(new Response(undefined, { status: 204 }));
115+
fetch.mockResponseOnce(() => new Response(null, { status: 204 }));
116+
fetch.mockResponseOnce(() => new Response(undefined, { status: 204 }));
117+
fetch.mockResponseOnce(() => Promise.resolve(new Response(null, { status: 204 })));
118+
fetch.mockResponseOnce(() => Promise.resolve(new Response(undefined, { status: 204 })));
119+
fetch.mockResponseOnce('done');
120+
121+
expect(await request()).toBe('');
122+
expect(await request()).toBe('');
123+
expect(await request()).toBe('');
124+
expect(await request()).toBe('');
125+
expect(await request()).toBe('');
126+
expect(await request()).toBe('');
127+
expect(await request()).toBe('');
128+
expect(await request()).toBe('');
129+
expect(await request()).toBe('');
130+
expect(await request()).toBe('');
131+
expect(await request()).toBe('');
132+
expect(await request()).toBe('');
133+
expect(await request()).toBe('');
134+
expect(await request()).toBe('');
135+
expect(await request()).toBe('');
136+
expect(await request()).toBe('done');
137+
});
102138
});
103139

104140
describe('testing mockResponses', () => {

0 commit comments

Comments
 (0)