expect-mqtt

Jest tests for MQTT

Usage no npm install needed!

<script type="module">
  import expectMqtt from 'https://cdn.skypack.dev/expect-mqtt';
</script>

README

expect-mqtt

E2E Testing for MQTT-enabled applications using jest

Basic Usage

const { MqttTest } = require('expect-mqtt');
const waitForExpect = require('wait-for-expect');

waitForExpect.defaults.timeout = 10000;
waitForExpect.defaults.interval = 500;

describe('MqttTest', () => {
  const mqtt = new MqttTest();

  beforeAll(async () => {
    await mqtt.connect('mqtt://localhost:1883'); // This method mimics connect() from the 'mqtt' library
  });

  afterEach(() => {
    mqtt.clear(); // Clear internal message caches after each test
  })

  afterAll(async () => {
    await mqtt.disconnect(); // Disconnect from Mqtt after all tests are done
  })

  it('Receives a message', async () => {
    await mqtt.subscribe('topic01'); // Subscribe to a topic

    await mqtt.publish('topic01', 'message'); // Send a message

    await waitForExpect(() => {
      expect(mqtt).receivedMessage('topic01', 'message')  // Wait for a message to be received
    });
  })