mocks
Every export of @kitr/mocks, read from the published type definitions. The guide shows them in use.
@kitr/mocks
Re-exports msw unchanged, plus the following.
kitrErrorResponse
function kitrErrorResponse(error: unknown): HttpResponse<import("msw").JsonBodyType>;
Turns anything thrown into the JSON response a kitr server sends for it: a KitrError keeps its
code and status, anything else becomes INTERNAL with status 500.
mockRoutes
function mockRoutes(table: RouteDescriptor[], baseUrl: string, resolvers: Record<string, Resolver | unknown>): RequestHandler[];
MSW handlers from a kitr route table (routes.toJSON()) or an OpenAPI-ish list.
Unhandled routes get a 501 so a missing mock is loud, not silent.
const handlers = mockRoutes(routeTable, 'http://localhost:3000', {
'projects.index': () => [{ id: '1', name: 'Alpha' }],
'projects.show': ({ params }) => ({ id: params.id, name: 'Alpha' }),
})
mockValidationError
function mockValidationError(method: 'post' | 'put' | 'patch', url: string, fieldErrors: Record<string, string[]>): import("msw").HttpHandler;
A handler that throws a kitr validation error, to test form error mapping.
setupBrowserMocks
function setupBrowserMocks(handlers: RequestHandler[], options?: {
quiet?: boolean;
}): Promise<import("msw/browser").SetupWorker>;
Starts MSW's service worker in the browser for development: await setupBrowserMocks(handlers).
Needs mockServiceWorker.js in the public folder. Pass quiet: false to see MSW's console messages.
setupNodeMocks
function setupNodeMocks(handlers: RequestHandler[]): Promise<import("msw/node").SetupServer>;
Starts MSW in Node for tests: const mocks = await setupNodeMocks(handlers). Requests no handler
matches go to the real network. Returns MSW's server, so use, resetHandlers and close work.
RouteDescriptor
interface RouteDescriptor {
/** The route name that `mockRoutes` resolvers are keyed by, such as `orders.show`. */
name: string;
/** The HTTP method, in any case: `GET`, `post`. */
method: string;
/** The path pattern with `:params`, such as `/api/orders/:id`. */
path: string;
}
One route of a kitr route table, as routes.toJSON() and kitr api:sync describe it.