mirror of
https://github.com/vale981/apollo-server
synced 2025-03-04 17:21:42 -05:00
61 lines
1.1 KiB
TypeScript
61 lines
1.1 KiB
TypeScript
/// <reference types="jest" />
|
|
|
|
import {
|
|
fetch,
|
|
Request,
|
|
RequestInit,
|
|
Response,
|
|
Body,
|
|
BodyInit,
|
|
Headers,
|
|
HeadersInit,
|
|
URL,
|
|
URLSearchParams,
|
|
URLSearchParamsInit,
|
|
} from '../packages/apollo-server-env';
|
|
|
|
interface FetchMock extends jest.Mock<typeof fetch> {
|
|
mockResponseOnce(data?: any, headers?: HeadersInit, status?: number): void;
|
|
mockJSONResponseOnce(data?: object, headers?: HeadersInit): void;
|
|
}
|
|
|
|
const mockFetch = jest.fn<typeof fetch>(fetch) as FetchMock;
|
|
|
|
mockFetch.mockResponseOnce = (
|
|
data?: BodyInit,
|
|
headers?: Headers,
|
|
status: number = 200,
|
|
) => {
|
|
return mockFetch.mockImplementationOnce(async () => {
|
|
return new Response(data, {
|
|
status,
|
|
headers,
|
|
});
|
|
});
|
|
};
|
|
|
|
mockFetch.mockJSONResponseOnce = (
|
|
data = {},
|
|
headers?: Headers,
|
|
status?: number,
|
|
) => {
|
|
return mockFetch.mockResponseOnce(
|
|
JSON.stringify(data),
|
|
Object.assign({ 'Content-Type': 'application/json' }, headers),
|
|
status,
|
|
);
|
|
};
|
|
|
|
const env = {
|
|
fetch: mockFetch,
|
|
Request,
|
|
Response,
|
|
Body,
|
|
Headers,
|
|
URL,
|
|
URLSearchParams,
|
|
};
|
|
|
|
jest.doMock('apollo-server-env', () => env);
|
|
|
|
export = env;
|