mentoss
Version:
A utility to mock fetch requests and responses.
100 lines (99 loc) • 3.59 kB
TypeScript
/**
* A class for mocking the `fetch` function.
*/
export class FetchMocker {
/**
* Creates a new instance.
* @param {object} options Options for the instance.
* @param {MockServer[]} options.servers The servers to use.
* @param {string|URL} [options.baseUrl] The base URL to use for relative URLs.
* @param {Credentials[]} [options.credentials] The credentials to use.
* @param {typeof Response} [options.CustomResponse] The Response constructor to use.
* @param {typeof Request} [options.CustomRequest] The Request constructor to use.
*/
constructor({ servers, baseUrl, credentials, CustomRequest, CustomResponse, }: {
servers: MockServer[];
baseUrl?: string | URL | undefined;
credentials?: import("./types.js").Credentials[] | undefined;
CustomResponse?: {
new (body?: BodyInit | null, init?: ResponseInit): Response;
prototype: Response;
error(): Response;
json(data: any, init?: ResponseInit): Response;
redirect(url: string | URL, status?: number): Response;
} | undefined;
CustomRequest?: {
new (input: RequestInfo | URL, init?: RequestInit): Request;
prototype: Request;
} | undefined;
});
/**
* The created fetch function.
* @type {typeof fetch}
*/
fetch: typeof fetch;
/**
* Determines if a request was made.
* @param {string|RequestPattern} request The request to match.
* @returns {boolean} `true` if the request was made, `false` otherwise.
*/
called(request: string | RequestPattern): boolean;
/**
* Determines if all routes were called.
* @returns {boolean} `true` if all routes were called, `false` otherwise.
*/
allRoutesCalled(): boolean;
/**
* Gets the uncalled routes.
* @return {string[]} The uncalled routes.
*/
get uncalledRoutes(): string[];
/**
* Asserts that all routes were called.
* @returns {void}
* @throws {Error} When not all routes were called.
*/
assertAllRoutesCalled(): void;
/**
* Mocks the fetch property on a given object.
* @param {Record<string, any>} object The object to mock fetch on.
* @param {string} [property="fetch"] The property name to mock.
* @returns {void}
* @throws {TypeError} If the object is not an object.
*/
mockObject(object: Record<string, any>, property?: string): void;
/**
* Unmocks the fetch property on a given object.
* @param {Record<string, any>} object The object to unmock fetch on.
* @returns {void}
* @throws {TypeError} If the object is not an object.
*/
unmockObject(object: Record<string, any>): void;
/**
* Mocks the global fetch function.
* @returns {void}
*/
mockGlobal(): void;
/**
* Unmocks the global fetch function.
* @returns {void}
*/
unmockGlobal(): void;
/**
* Clears the CORS preflight cache.
* @returns {void}
*/
clearPreflightCache(): void;
/**
* Clears all data from the fetch mocker. This include the CORS preflight
* cache as well as the routes on the servers. The servers themselves
* remain intact.
* @returns {void}
*/
clearAll(): void;
#private;
}
export type RequestPattern = import("./types.js").RequestPattern;
export type Credentials = import("./types.js").Credentials;
export type MockServer = import("./mock-server.js").MockServer;
export type Trace = import("./mock-server.js").Trace;