diff --git a/package.json b/package.json index 43492c2..4ae5940 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "@uci/mqtt", "version": "0.0.1", "description": "mqtt client with json payloads and mqtt custom broker", - "main": "src/index.js", + "main": "src", "scripts": { "testw": "mocha -r @std/esm test/*.test.mjs --watch --recurse --watch-extensions mjs", "test": "mocha -r @std/esm test/*.test.mjs", diff --git a/src/client.mjs b/src/client.mjs index 213dcb7..26896e9 100644 --- a/src/client.mjs +++ b/src/client.mjs @@ -1,7 +1,6 @@ import { connect } from 'async-mqtt' import merge from 'lodash.merge' import btc from 'better-try-catch' -import isArray from 'lodash.isarray' import union from 'lodash.union' import xor from 'lodash.xor' import logger from '@uci/logger' @@ -14,7 +13,8 @@ export default class Client { this.id = opts.id || opts.name || 'mqtt:'+ new Date().getTime() this.url = opts.url // subscription topics can be string of commna delimited or array of strings see object see mqtt.js docs - this.topics = isArray(opts.topics) ? opts.topic : (opts.topics.split(',') || ['default']) + this.topics = Array.isArray(opts.topics) ? opts.topics : (opts.topics ? opts.topics.split(',') : [this.id]) + console.log(this.topics) this.opts = opts.connect || {} // see options for new mqtt.Client // self bindings this.connect = this.connect.bind(this) @@ -70,7 +70,7 @@ export default class Client { async send(topics,payload,options) { - if (typeof topics !=='string'|| !isArray(topics)) { + if (typeof topics !=='string'|| !Array.isArray(topics)) { payload = topics topics = this.topics } @@ -106,7 +106,7 @@ export default class Client { this.on('message',messageProcess.bind(this)) async function messageProcess (topic,payload) { - // console.log('incoming messeage on topic', topic) + console.log('incoming messeage on topic', topic) let packet = this._handlePayload(payload) if (packet) await this._packetProcess (packet,topic) diff --git a/test/amodule.test.js b/test/amodule.test.js index 3ca5d4a..fe35e74 100644 --- a/test/amodule.test.js +++ b/test/amodule.test.js @@ -1,51 +1,49 @@ -'use strict' +import aClass from '../src' +import chai from 'chai' +import chaiAsPromised from 'chai-as-promised' -const - Changeme = require('../src/changeme'), - expect = require('chai').expect, - pause = require('@uci/utils').pPause +chai.use(chaiAsPromised) +const expect = chai.expect describe( - `Testing `, - function () { - hooks() - sometests() - someothertests() - }) + 'Testing ', + function () { + hooks() + sometests() + someothertests() + }) //****************** TESTS ********************** function sometests() { - it('==> test something', async function () { + it('==> test something', async function () { - let result = await someasyncfunction() - expect(result, `test failed`).to.equal('expectedresult') - await pause(1000) + let result = await someasyncfunction() + expect(result, 'test failed').to.equal('expectedresult') - }) + }) } function someothertests() { - it('==> test something', async function () { + it('==> test something', async function () { - let result = await someasyncfunction() - expect(result, `test failed`).to.equal('expectedresult') - await pause(1000) + let result = await someasyncfunction() + expect(result, 'test failed').to.equal('expectedresult') - }) + }) } function hooks() { - before(async() => { - await someasyncfunctiontodobefore() - }) + before(async() => { + await someasyncfunctiontodobefore() + }) - beforeEach(async() => { - await someasyncfunctiontodobeforeeachtest() - }) + beforeEach(async() => { + await someasyncfunctiontodobeforeeachtest() + }) - after(async() => { - await someasyncfunctiontodoaftereeachtest() - }) + after(async() => { + await someasyncfunctiontodoaftereeachtest() + }) } diff --git a/test/apromise.test.js b/test/apromise.test.js index 6fa66b6..29fd4e7 100644 --- a/test/apromise.test.js +++ b/test/apromise.test.js @@ -1,9 +1,9 @@ -'use strict' - -const chai = require('chai'), - chaiAsPromised = require("chai-as-promised"), - lib = require('../') +import changeme from '../src/changeme' +import path from 'path' +import chai from 'chai' +import chaiAsPromised from 'chai-as-promised' +const { assert } = chai; chai.use(chaiAsPromised); const expect = chai.expect @@ -12,8 +12,8 @@ describe('Promise Stuff - ', function () { it('Can test a promise', function () { - return expect(lib.apromise).to.eventually.equal('some promise hey') - return Promise.resolve().then(() => expect(lib.apromise).to.eventually.equal('some promise hey')) + return expect(changeme.apromise).to.eventually.equal('some promise hey') + return Promise.resolve().then(() => expect(changeme.apromise).to.eventually.equal('some promise hey')) }) })