0.0.1 • Published 4 years ago

expect-mqtt v0.0.1

Weekly downloads
-
License
MIT
Repository
gitlab
Last release
4 years ago

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
    });
  })
0.0.1

4 years ago