Javascript 在mocha chai中测试MQTT.js套接字

Javascript 在mocha chai中测试MQTT.js套接字,javascript,testing,mocha.js,mqtt,chai,Javascript,Testing,Mocha.js,Mqtt,Chai,我需要验证是否将正确的数据发送到mqtt代理。但我不能在回调中断言 const mqtt = require('async-mqtt'); describe('MQTT tests', function () { let mqttClient = null before(async function () { mqttClient = mqtt.connect('mqtt://localhost:111')

我需要验证是否将正确的数据发送到mqtt代理。但我不能在回调中断言

    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()
    })

即使记录的值相同,该测试也始终失败(在本例中,它将是“某些消息”)。如果我从回调返回,测试总是成功。

这与mqtt无关。不要把承诺与摩卡咖啡混为一谈

在测试函数中使用
async
,使其隐式返回
Promise
。您可以选择或
done
回调,但不能同时选择两者

您无需等待订阅和触发器完成-运行测试时,Mocha将等待调用
done
。如果从未调用,测试将失败并超时