vendredi 14 août 2020

Testing MQTT.js socket in mocha chai

I need to verify that correct data are send to mqtt broker. But i cannot assert inside callback

    const mqtt = require('async-mqtt');

    describe('MQTT tests', function () {
        let mqttClient = null

        before(async function () {
            mqttClient = mqtt.connect('mqtt://localhost:111') 
        })
   
        it('test that spy on mqtt', async function (done) {
            mqttClient.on('message', function (topic, message) {
                let msg = message.toString()
                console.log(mes)
                expect(msg).to.equal('some message')
                done()
            })
           await mqttClient.subscribe('topicToSubscribeTo')
           await triggerMqttRequest()
    })

This test always fail even if the logged value is same (in this example it would be "some message"). If i return from the callback the test always succeed.

Aucun commentaire:

Enregistrer un commentaire