Skip to content

mswEndpointHandler

Registers an MSW handler for a generated endpoint: method and URL pattern come from the endpoint (path params follow MSW’s :name form via mswPathPattern).

The return value is a normal MSW HttpHandler with the same endpoint attached, so tests can reuse the instance you passed in.

The resolver is the same function MSW expects for http.* handlers. Returning a plain value (not a Response) always builds a successful reply (typed like the endpoint’s success data). For errors, non-OK statuses, or extra headers, return a Response — typically mswEndpointErrorResponse / mswEndpointResponse or MSW’s helpers. Default status codes for the package-built responses come from testingDefaults.

Requires the msw peer dependency. Setup and lifecycle are in Connect MSW.

Example

ts
import {
  mswEndpointHandler,
  mswEndpointResponse,
} from "mobx-tanstack-query-api/testing";

export const handlers = [
  mswEndpointHandler(listFruitsEndpoint, () => ({
    items: ["apple", "banana"],
  })),
  mswEndpointHandler(createFruitEndpoint, async ({ request }) => {
    const body = (await request.json()) as { name: string };
    return mswEndpointResponse(createFruitEndpoint, { id: 1, name: body.name }, {
      status: 201,
    });
  }),
];

Released under the MIT License.