Skip to content
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

support buffers when mocking responses #223

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { StringDecoder } = require('string_decoder');
const { APIRequest, APIRequest2, defaultRequestUri, request } = require('./api')

describe('testing mockResponse and alias once', () => {
Expand Down Expand Up @@ -358,6 +359,15 @@ describe('request', () => {
fetch('https://bar', {}).then((response) => response.headers.get('ding'))
).resolves.toEqual('dang')
})

it('resolves with mocked response containing buffer', () => {
fetch.mockResponseOnce(() => Promise.resolve(new Response(Buffer.from('foo'))))
return expect(
fetch('https://bar', {})
.then((response) => response.arrayBuffer())
.then((arrayBuffer) => new StringDecoder('utf8').write(new Uint8Array(arrayBuffer)))
).resolves.toEqual('foo')
});
})

describe('conditional mocking', () => {
Expand Down
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export interface MockResponseInit extends MockParams {
export type ErrorOrFunction = Error | ((...args: any[]) => Promise<any>);
export type UrlOrPredicate = string | RegExp | ((input: Request) => boolean);

export type MockResponseInitFunction = (request: Request) => MockResponseInit | string | Promise<MockResponseInit | string>;
export type MockResponseInitFunction = (request: Request) => MockResponseInit | string | Promise<MockResponseInit | string | Response>;
alexkolson marked this conversation as resolved.
Show resolved Hide resolved

// alias of fetchMock.enableMocks() for ES6 import syntax to not clash with other libraries
export function enableFetchMocks(): void;
Expand Down
7 changes: 7 additions & 0 deletions types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ fetchMock.mockResponse(JSON.stringify({foo: "bar"}), {
fetchMock.mockResponse(JSON.stringify({foo: "bar"}), {});
fetchMock.mockResponse(someAsyncHandler);
fetchMock.mockResponse(someAsyncStringHandler);
fetchMock.mockResponse(someAsyncBufferHandler)

fetchMock.mockResponseOnce(JSON.stringify({foo: "bar"}));
fetchMock.mockResponseOnce(JSON.stringify({foo: "bar"}), {
Expand All @@ -21,6 +22,7 @@ fetchMock.mockResponseOnce(JSON.stringify({foo: "bar"}), {
fetchMock.mockResponseOnce(JSON.stringify({foo: "bar"}), {});
fetchMock.mockResponseOnce(someAsyncHandler);
fetchMock.mockResponseOnce(someAsyncStringHandler);
fetchMock.mockResponseOnce(someAsyncBufferHandler)

fetchMock.once(JSON.stringify({foo: "bar"}));
fetchMock.once(JSON.stringify({foo: "bar"}), {
Expand All @@ -43,6 +45,7 @@ fetchMock.mockResponses(
someAsyncHandler,
someSyncStringHandler,
someAsyncStringHandler,
someAsyncBufferHandler,
[JSON.stringify({foo: "bar"}), {status: 200}]
);

Expand Down Expand Up @@ -105,6 +108,10 @@ function someSyncStringHandler(): string {
return JSON.stringify({foo: "bar"});
}

async function someAsyncBufferHandler(): Promise<Response> {
return new Response(Buffer.from('foo'));
}

enableFetchMocks();
disableFetchMocks();
fm.enableMocks();
Expand Down