mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 18:12:04 +00:00
fix(MQTT Node): Close connection if connection attempt fails (#10873)
This commit is contained in:
@@ -1,19 +1,20 @@
|
||||
import { MqttClient } from 'mqtt';
|
||||
import { mock } from 'jest-mock-extended';
|
||||
import { MqttClient } from 'mqtt';
|
||||
import { ApplicationError } from 'n8n-workflow';
|
||||
|
||||
import { createClient, type MqttCredential } from '../GenericFunctions';
|
||||
|
||||
describe('createClient', () => {
|
||||
const mockConnect = jest.spyOn(MqttClient.prototype, 'connect').mockImplementation(function (
|
||||
this: MqttClient,
|
||||
) {
|
||||
setImmediate(() => this.emit('connect', mock()));
|
||||
return this;
|
||||
});
|
||||
|
||||
beforeEach(() => jest.clearAllMocks());
|
||||
|
||||
it('should create a client with minimal credentials', async () => {
|
||||
const mockConnect = jest.spyOn(MqttClient.prototype, 'connect').mockImplementation(function (
|
||||
this: MqttClient,
|
||||
) {
|
||||
setImmediate(() => this.emit('connect', mock()));
|
||||
return this;
|
||||
});
|
||||
|
||||
const credentials = mock<MqttCredential>({
|
||||
protocol: 'mqtt',
|
||||
host: 'localhost',
|
||||
@@ -35,4 +36,31 @@ describe('createClient', () => {
|
||||
clientId: 'testClient',
|
||||
});
|
||||
});
|
||||
|
||||
it('should reject with ApplicationError on connection error and close connection', async () => {
|
||||
const mockConnect = jest.spyOn(MqttClient.prototype, 'connect').mockImplementation(function (
|
||||
this: MqttClient,
|
||||
) {
|
||||
setImmediate(() => this.emit('error', new Error('Connection failed')));
|
||||
return this;
|
||||
});
|
||||
const mockEnd = jest.spyOn(MqttClient.prototype, 'end').mockImplementation();
|
||||
|
||||
const credentials: MqttCredential = {
|
||||
protocol: 'mqtt',
|
||||
host: 'localhost',
|
||||
port: 1883,
|
||||
clean: true,
|
||||
clientId: 'testClientId',
|
||||
username: 'testUser',
|
||||
password: 'testPass',
|
||||
ssl: false,
|
||||
};
|
||||
|
||||
const clientPromise = createClient(credentials);
|
||||
|
||||
await expect(clientPromise).rejects.toThrow(ApplicationError);
|
||||
expect(mockConnect).toBeCalledTimes(1);
|
||||
expect(mockEnd).toBeCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user