mardi 20 décembre 2016

Proper way to test MQTT Client Paho with mocha and typescript

I googled for days but can't find anything about how to test the Paho MQTT Client. I tried it in a sort of naive way, like that:

import { suite, test, slow, timeout, skip, only } from 'mocha-typescript';
import chai = require('chai');

var jsdom = require('jsdom');
var Paho: any;
const expect: any = chai.expect;
const host: string = '127.0.0.1';
const port: number = 1384;
const clientId1: string = 'EMBRACE-TEST-ID_1';
const clientId2: string = 'EMBRACE-TEST-ID_2';

let client1;
let client2;

describe('test', function () {
  it('should', function (done) {
    // emulate browser window, which is required by Paho
    jsdom.env("<html><body></body></html>", [],
      function (err: any, window: any) {
        // when window is ready, require Paho and
        // initialize with built window
        Paho = require('ng2-mqtt/mqttws31').jsdom(window);
        // This does not work -> exception in mqttws31.js: window is not defined
        client1 = new Paho.MQTT.Client(host, port, clientId1);
        client1.connect({ onSuccess: () => { expect(true).to.be.true; done(); }, onFailure: () => { expect(false).to.be.true; } })
        done();
      });
  });
});

However the Paho = require(...)-Part inside the callback function of jsdom.env(...) throws the exception in mqttws31.js: "window is not defined". Has anyone an idea how to solve that, in order to get the Paho Client running in a non-browser environment?

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire