Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

mock-stomp-broker

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mock-stomp-broker

A mock STOMP broker for Node, to be used for testing

latest
Source
npmnpm
Version
1.2.0
Version published
Maintainers
1
Created
Source

MockStompBroker

A Node-based mock STOMP message broker with hooks for easy testing of your STOMP clients. Supports TypeScript.

Installing

npm install --save-dev mock-stomp-broker

Usage example

import MockStompBroker from "mock-stomp-broker";
import MyUpdateableTable from "./MyUpdateableTable";

describe("MyUpdateableTable", () => {
  it("should add rows to the table when row data is pushed via STOMP", async () => {
    const broker = new MockStompBroker();
    const rowData = {
      id: 1,
      name: "Jane Doe"
    };
    const wrapper = mount(
      <MyUpdateableTable websocketPort={broker.getPort()} rows={[]} />
    );

    expect(wrapper.text()).not.toContain(rowData.name);

    const [sessionId] = await broker.newSessionsConnected();

    await broker.subscribed(sessionId);

    const messageId = broker.scheduleMessage(`topics/my-topic`, rowData);

    await broker.messageSent(messageId);

    expect(wrapper.text()).toContain(rowData.name);

    broker.kill();
  });

  it("should clean up the websocket connection when table is unmounted", async () => {
    const broker = new MockStompBroker();
    const wrapper = mount(
      <MyUpdateableTable websocketPort={broker.getPort()} rows={[]} />
    );

    const [sessionId] = await broker.newSessionsConnected();

    wrapper.unmount();

    await broker.disconnected(sessionId);

    broker.kill();
  });
});

Config

The MockStompBroker can take a config object as a constructor arg. The config object and all its fields are optional.

FieldDefaultEffect
portN/ASets a specific port for the broker to start up on. Overrides portRange.
portRange[8000, 9001]When provided, the broker will choose a random port number in the provided range to start up on (useful when running multiple tests in parallel). Will only take effect if port is not provided.
endpoint/websocketSets the websocket endpoint exposed by the broker. This is where your clients should connect.

Methods

MethodArgsReturnsEffect
getPortN/AnumberReturns the broker's port. Use this to point your client(s) at the right port when generating random ports (either with portRange or the default behavior).
newSessionsConnectedN/APromise<string[]>Resolves to an array of session IDs for sessions that were created since the last time you called this method. Rejects after 2000ms.
subscribedstringPromise<void>Resolves when a STOMP subscription has been established for the given session ID. Use this to ensure a session has an active subscription before trying to send messages. Rejects after 2000ms.
scheduleMessagestring, any, {}?stringSchedule a STOMP message with a given payload to be asynchronously sent by the broker to all subscribers to a given topic. Can customize the message headers in the optional third argument. Returns a message ID for the scheduled message.
messageSentstringPromise<void>Resolves when a scheduled STOMP message with a given message ID has been sent by the broker. Use this to ensure message delivery to your clients before asserting on the outcome of receiving a message. Rejects after 2000ms.
disconnectedstringPromise<void>Resolves when a session with a given session ID has disconnected. Use this to test your client cleanup logic. Rejects after 2000ms.
killN/AvoidShut down the broker. You'll want to call this after every test case (or in an afterEach) to ensure your ports are cleaned up.

Keywords

stomp

FAQs

Package last updated on 11 Dec 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts