Mocky Balboa API Reference
    Preparing search index...

    Client used to interface with the WebSocket server for mocking server-side network requests. And optionally integrating for mocking client-side network requests.

    Index

    Properties

    clientIdentifier: string

    Unique identifier for the client, used for continuity across the WebSocket connection enabling parallel mocking

    RouteType: {
        Both: "both";
        ClientOnly: "client-only";
        ServerOnly: "server-only";
    }

    Convenient access to route types

    Accessors

    • get ws(): WebSocket

      WebSocket connection.

      Returns WebSocket

      an error if the client has not been connected yet with Client.connect

    Methods

    • Internal

      Allows an external handler to be injected into the route handling process for dealing with client-side request interception.

      Type Parameters

      • TCallbackArgs extends unknown[]
      • TResult

      Parameters

      • options: {
            extractRequest: (...args: TCallbackArgs) => Request | Promise<Request>;
            handleResult: (
                response: undefined | ExternalRouteHandlerRouteResponse,
                ...args: TCallbackArgs,
            ) => undefined | TResult | Promise<undefined | TResult>;
        }

        options for the external route handler

        • extractRequest: (...args: TCallbackArgs) => Request | Promise<Request>

          a function that extracts a request from the arguments passed to the external handler

        • handleResult: (
              response: undefined | ExternalRouteHandlerRouteResponse,
              ...args: TCallbackArgs,
          ) => undefined | TResult | Promise<undefined | TResult>

          a function that transforms the internal result to the external handlers expected result

      Returns (...args: TCallbackArgs) => Promise<undefined | TResult>

      a function that acts as a handler for the external route handler

    • Used to connect the client to the WebSocket server. This will also identify the client on the server to enable concurrency for mocking. You need to call this method before you can register any route handlers.

      Parameters

      Returns Promise<void>

    • Disconnects the WebSocket connection. You need to run Client.connect again to reconnect.

      Returns void

      Route handlers and message handlers are not removed when the client disconnects.

    • Registers a handler for a specific message type.

      Type Parameters

      Parameters

      • messageType: TMessageType

        The type of message to handle.

      • handler: (message: ParsedMessageType<TMessageType>) => void | Promise<void>

        The handler function to call when a message of the specified type is received. The handler function receives the parsed message as an argument.

      Returns void

    • Registers a route handler, when the client receives a request message.

      Parameters

      Returns string

      a route handler ID that can be used to unregister the handler later if required

    • Used to unregister a route handler.

      Parameters

      • routeHandlerId: string

        the route handler ID returned from Client.route

      Returns void

    • Removes all route handlers

      Returns void

    • Waits for a request to be made that matches the given URL matcher.

      Parameters

      Returns Promise<Request>

      Promise resolving to an instance of Request matching the original request dispatched by the server, rejects with an error if the request is not found within the specified timeout duration.