Модульное тестирование с Jest vs MQTT - PullRequest
0 голосов
/ 08 мая 2020

Я пишу тестовые примеры для API, используя Jest и MQTT. Я хочу проверить значение ответа сообщения с моим ожидаемым значением. В настоящее время я не могу обработать ответ функции client.on. Пожалуйста, помогите мне решить эту проблему, большое спасибо

test.ts

    async doTest() {

        client.on('connect', async function () {
            await client.subscribe(topic + '/reply', function (err) {
                 if (!err) {
                   client.publish(topic, JSON.stringify(payload));
                 }
            });
        });

        await client.on('message', async function (topic, message) {
              // message is Buffer
              const res = await JSON.parse(message.toString());
              const { err, response } = res;
              this.result = JSON.stringify(response);
        });
}

    async getResult() {
         return this.result;
}

test.spe c .ts

it('should call api', async () => {
    await test.doTest();
    result = await test.getResult();
    console.log(result);
});
...