UNPKG

mentoss

Version:

A utility to mock fetch requests and responses.

187 lines (186 loc) 7.93 kB
/** * Represents a route that the server can respond to. */ export class Route { /** * Creates a new instance. * @param {Object} options The route options. * @param {RequestPattern} options.request The request to match. * @param {ResponsePattern|undefined} options.response The response to return. * @param {ResponseCreator} options.createResponse The response creator to call. * @param {string} options.baseUrl The base URL for the server. */ constructor({ request, response, createResponse, baseUrl }: { request: RequestPattern; response: ResponsePattern | undefined; createResponse: ResponseCreator; baseUrl: string; }); /** * Checks if the route matches a request. * @param {RequestPattern} request The request to check. * @returns {boolean} `true` if the route matches, `false` if not. */ matches(request: RequestPattern): boolean; /** * Traces the details of the request to see why it doesn't match. * @param {RequestPattern} request The request to check. * @returns {{matches:boolean, messages:string[]}} The trace match result. */ traceMatches(request: RequestPattern): { matches: boolean; messages: string[]; }; /** * Creates a Response object from a route's response pattern. If the body * is an object then the response will be JSON; if the body is a string * then the response will be text; otherwise the response will be bytes. * @param {Request} request The request that was received. * @param {typeof Response} PreferredResponse The Response constructor to use. * @returns {Promise<Response>} The response to return. */ createResponse(request: Request, PreferredResponse: typeof Response): Promise<Response>; /** * Returns a string representation of the route. * @returns {string} The string representation of the route. */ toString(): string; #private; } /** * Represents a server that can respond to requests from `fetch()`. */ export class MockServer { /** * Creates a new instance. * @param {string} baseUrl The base URL for the server. */ constructor(baseUrl: string); /** * The base URL for the server. * @type {string} * @readonly */ readonly baseUrl: string; /** * Adds a new route to the server. * @param {RequestPattern} request * @param {ResponsePattern|ResponseCreator|number} response */ route(request: RequestPattern, response: ResponsePattern | ResponseCreator | number): void; /** * Adds a new route that responds to a POST request. * @param {MethodlessRequestPattern|string} request The request to match. * @param {ResponsePattern|ResponseCreator|number} response The response to return. */ post(request: MethodlessRequestPattern | string, response: ResponsePattern | ResponseCreator | number): void; /** * Adds a new route that responds to a GET request. * @param {MethodlessRequestPattern|string} request The request to match. * @param {ResponsePattern|ResponseCreator|number} response The response to return. */ get(request: MethodlessRequestPattern | string, response: ResponsePattern | ResponseCreator | number): void; /** * Adds a new route that responds to a PUT request. * @param {MethodlessRequestPattern|string} request The request to match. * @param {ResponsePattern|ResponseCreator|number} response The response to return. */ put(request: MethodlessRequestPattern | string, response: ResponsePattern | ResponseCreator | number): void; /** * Adds a new route that responds to a DELETE request. * @param {MethodlessRequestPattern|string} request The request to match. * @param {ResponsePattern|ResponseCreator|number} response The response to return. */ delete(request: MethodlessRequestPattern | string, response: ResponsePattern | ResponseCreator | number): void; /** * Adds a new route that responds to a PATCH request. * @param {MethodlessRequestPattern|string} request The request to match. * @param {ResponsePattern|ResponseCreator|number} response The response to return. */ patch(request: MethodlessRequestPattern | string, response: ResponsePattern | ResponseCreator | number): void; /** * Adds a new route that responds to a HEAD request. * @param {MethodlessRequestPattern|string} request The request to match. * @param {ResponsePattern|ResponseCreator|number} response The response to return. */ head(request: MethodlessRequestPattern | string, response: ResponsePattern | ResponseCreator | number): void; /** * Adds a new route that responds to an OPTIONS request. * @param {MethodlessRequestPattern|string} request The request to match. * @param {ResponsePattern|ResponseCreator|number} response The response to return. */ options(request: MethodlessRequestPattern | string, response: ResponsePattern | ResponseCreator | number): void; /** * Generates a `Response` for the given `Request` if a route matches. * @param {Request} request The request to respond to. * @param {typeof Response} [PreferredResponse] The Response constructor to use. * @returns {Promise<Response|undefined>} The response to return. */ receive(request: Request, PreferredResponse?: typeof Response): Promise<Response | undefined>; /** * Traces the details of the request to see why it doesn't match. * @param {Request} request The request to check. * @param {typeof Response} [PreferredResponse] The Response constructor to use. * @returns {Promise<{response:Response|undefined,traces: Array<Trace>}>} The trace match result. */ traceReceive(request: Request, PreferredResponse?: typeof Response): Promise<{ response: Response | undefined; traces: Array<Trace>; }>; /** * Traces the details of a request pattern to see if it matches any routes. * @param {RequestPattern|string} request The request pattern to check. * @returns {{traces:Array<Trace>, matched:boolean}} The trace result with match status. */ traceCalled(request: RequestPattern | string): { traces: Array<Trace>; matched: boolean; }; /** * Determines if a route has been called. * @param {RequestPattern|string} request The request pattern to check. * @returns {boolean} `true` if the route was called, `false` if not. * @throws {Error} If the request pattern doesn't match any registered routes. */ called(request: RequestPattern | string): boolean; /** * Returns the routes that have not been called. * @returns {string[]} The unmatched routes. */ get uncalledRoutes(): string[]; /** * Determines if all routes have been called. * @returns {boolean} `true` if all routes have been called, `false` if not. */ allRoutesCalled(): boolean; /** * Clears all routes and history from the server. * @returns {void} */ clear(): void; /** * Asserts that all routes have been called. * @returns {void} * @throws {Error} If any routes have not been called. */ assertAllRoutesCalled(): void; #private; } export type RequestPattern = import("./types.js").RequestPattern; export type MethodlessRequestPattern = import("./types.js").MethodlessRequestPattern; export type ResponsePattern = import("./types.js").ResponsePattern; export type ResponseCreator = import("./types.js").ResponseCreator; export type Trace = { /** * The route that was checked. */ title: string; /** * Whether the route matches the request. */ matches: boolean; /** * The messages explaining why the route doesn't match. */ messages: string[]; };