Skip to content

Commit 50776a7

Browse files
authored
fix: expose mqttjs version on MqttClient.VERSION (#1821)
* fix: expose mqttjs version on MqttClient.VERSION * fix: add tests * fix: browser tests
1 parent f9b1204 commit 50776a7

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

src/lib/client.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,8 @@ export interface MqttClientEventCallbacks {
390390
* (see Connection#connect)
391391
*/
392392
export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbacks> {
393+
public static VERSION = process.env.npm_package_version
394+
393395
/** Public fields */
394396

395397
/** It's true when client is connected to broker */
@@ -489,7 +491,7 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
489491
this.log = this.options.log || _debug('mqttjs:client')
490492
this.noop = this._noop.bind(this)
491493

492-
this.log('MqttClient :: version:', process.env.npm_package_version)
494+
this.log('MqttClient :: version:', MqttClient.VERSION)
493495

494496
if (isWebWorker) {
495497
this.log('MqttClient :: environment', 'webworker')

test/browser/test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ function testProto(proto, port, cb = () => { }) {
4141
let client = null;
4242

4343
it('should connect-publish-subscribe', (done) => {
44+
45+
expect(typeof mqtt.Client.VERSION).to.equal('string')
46+
4447
client = mqtt.connect(`${proto}://localhost:${port}`, {
4548
// log: console.log.bind(console),
4649
clientId: `testClient-${browser}-${proto}`,

test/client.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import abstractClientTests from './abstract_client'
1717
import { IClientOptions } from 'src/lib/client'
1818
import { describe, it, after } from 'node:test'
1919

20+
// eslint-disable-next-line @typescript-eslint/no-var-requires
21+
const pkgJson = require('../package.json')
22+
2023
const debug = _debug('mqttjs:client-test')
2124

2225
const ports = getPorts(2)
@@ -39,6 +42,10 @@ describe('MqttClient', () => {
3942
process.exit(0)
4043
})
4144

45+
it('should have static VERSION set', function _test(t) {
46+
assert.equal(mqtt.MqttClient.VERSION, pkgJson.version)
47+
})
48+
4249
abstractClientTests(server, config, ports)
4350

4451
describe('creating', () => {

0 commit comments

Comments
 (0)