Skip to content

Commit b9ae119

Browse files
committed
feat: allow passing null as response body
1 parent 6133016 commit b9ae119

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ type UrlOrPredicate = string | RegExp | ((input: Request) => boolean);
247247
type RequestInput = string | URL | Request;
248248
type ResponseProvider = ResponseLike | ((request: Request) => ResponseLike | Promise<ResponseLike>);
249249
type ResponseLike = MockResponse | ResponseBody | Response;
250-
type ResponseBody = string;
250+
type ResponseBody = string | null;
251251
type ErrorOrFunction = Error | ResponseBody | ResponseProvider;
252252

253253
export interface MockParams {
@@ -339,7 +339,7 @@ async function normalizeResponse(
339339

340340
if (responseLike instanceof Response) {
341341
return responseLike;
342-
} else if (typeof responseLike === 'string') {
342+
} else if (typeof responseLike === 'string' || responseLike === null) {
343343
return new Response(responseLike, params);
344344
} else {
345345
return patchUrl(new Response(responseLike.body, { ...params, ...responseLike }), responseLike.url ?? params?.url);

tests/api.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,30 @@ 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(() => null, { status: 204 });
106+
fetch.mockResponseOnce(() => Promise.resolve(null), { status: 204 });
107+
fetch.mockResponseOnce({ status: 204 });
108+
fetch.mockResponseOnce(() => ({ status: 204 }));
109+
fetch.mockResponseOnce(() => Promise.resolve({ status: 204 }));
110+
fetch.mockResponseOnce(new Response(null, { status: 204 }));
111+
fetch.mockResponseOnce(() => new Response(null, { status: 204 }));
112+
fetch.mockResponseOnce(() => Promise.resolve(new Response(null, { status: 204 })));
113+
fetch.mockResponseOnce('done');
114+
115+
expect(await request()).toBe('');
116+
expect(await request()).toBe('');
117+
expect(await request()).toBe('');
118+
expect(await request()).toBe('');
119+
expect(await request()).toBe('');
120+
expect(await request()).toBe('');
121+
expect(await request()).toBe('');
122+
expect(await request()).toBe('');
123+
expect(await request()).toBe('');
124+
expect(await request()).toBe('done');
125+
});
102126
});
103127

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

0 commit comments

Comments
 (0)