Skip to content

Commit 25c9192

Browse files
rewrite connect test (mqttjs#1417)
1 parent 985c5d6 commit 25c9192

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

src/client.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ export class MqttClient extends EventEmitter {
9090

9191
// Using this method to clean up the constructor to do options handling
9292
logger.debug(`populating internal client options object...`);
93-
this._options = {...defaultConnectOptions, ...options}
93+
this._options = {
94+
clientId: defaultClientId(),
95+
...defaultConnectOptions,
96+
...options
97+
}
9498

9599
this.conn = this._options.customStreamFactory? this._options.customStreamFactory(this._options) : connectionFactory(this._options);
96100

@@ -144,10 +148,6 @@ export class MqttClient extends EventEmitter {
144148
})
145149
}
146150

147-
mergeDefaultOptions(options: ConnectOptions): ConnectOptions {
148-
return {clientId: defaultClientId(), ...defaultConnectOptions, ...options}
149-
}
150-
151151
async handleIncomingPacket (packet: Packet): Promise<void> {
152152
logger.debug(`handleIncomingPacket packet.cmd=${packet.cmd}`);
153153
switch (packet.cmd) {

test/test_connect.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,27 @@ test.before('set up aedes broker', async t => {
4444
});
4545
});
4646

47-
/* TODO */
48-
test('should send a CONNECT packet to the broker and receive a CONNACK', async t => {
49-
const client = await connect({
47+
test('should send a CONNECT packet to the broker and receive a CONNACK', (t) => {
48+
t.plan(0)
49+
const clientId = 'basic-connect-test'
50+
51+
let resolveClientConnected
52+
const clientConnectedPromise = new Promise((resolve) => {
53+
resolveClientConnected = resolve
54+
})
55+
56+
const clientConnectedListener = (client) => {
57+
if (client.id === clientId) {
58+
t.context.broker.removeListener('client', clientConnectedListener)
59+
resolveClientConnected()
60+
}
61+
}
62+
t.context.broker.on('client', clientConnectedListener)
63+
64+
const clientPromise = connect({
5065
brokerUrl: 'mqtt://localhost',
51-
});
52-
t.assert(!!client);
53-
});
66+
clientId
67+
})
5468

55-
/**
56-
* 1) Send a Connect packet
57-
* 2) writeToStream returns false and emits an error on this.conn('error')
58-
*
59-
* We shouldn't be throwing away the whole client because 1 packet failed.
60-
*
61-
* UPDATE: matteo advises otherwise: https://github.com/mqttjs/mqtt-packet/issues/126#issuecomment-1029373619
62-
*/
69+
return Promise.all([clientConnectedPromise, clientPromise])
70+
})

0 commit comments

Comments
 (0)