From 8713b84e4100bafc602475313ea920defe080281 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=B6ran=20Sander?= Date: Mon, 15 Dec 2025 07:38:10 +0100 Subject: [PATCH] Fix broken tests --- src/lib/__tests__/healthmetrics.test.js | 3 +++ src/lib/__tests__/post-to-mqtt.test.js | 20 +++++++++++++++---- src/lib/__tests__/post-to-new-relic.test.js | 3 +++ src/lib/__tests__/proxysessionmetrics.test.js | 3 +++ .../log_events/__tests__/sanitization.test.js | 4 ++-- 5 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/lib/__tests__/healthmetrics.test.js b/src/lib/__tests__/healthmetrics.test.js index f1f30d5..b8a5699 100644 --- a/src/lib/__tests__/healthmetrics.test.js +++ b/src/lib/__tests__/healthmetrics.test.js @@ -23,6 +23,9 @@ jest.unstable_mockModule('../../globals.js', () => ({ verbose: jest.fn(), debug: jest.fn(), }, + errorTracker: { + incrementError: jest.fn(), + }, config: { get: jest.fn(), has: jest.fn(), diff --git a/src/lib/__tests__/post-to-mqtt.test.js b/src/lib/__tests__/post-to-mqtt.test.js index 5c20153..bbb6225 100644 --- a/src/lib/__tests__/post-to-mqtt.test.js +++ b/src/lib/__tests__/post-to-mqtt.test.js @@ -8,6 +8,9 @@ jest.unstable_mockModule('../../globals.js', () => ({ debug: jest.fn(), verbose: jest.fn(), }, + errorTracker: { + incrementError: jest.fn(), + }, mqttClient: { publish: jest.fn(), }, @@ -19,12 +22,20 @@ jest.unstable_mockModule('../../globals.js', () => ({ })); const globals = (await import('../../globals.js')).default; +// Mock log-error module +const mockLogError = jest.fn(); +jest.unstable_mockModule('../log-error.js', () => ({ + logError: mockLogError, +})); + // Import the module under test const { postHealthToMQTT, postUserSessionsToMQTT, postUserEventToMQTT } = await import('../post-to-mqtt.js'); describe('post-to-mqtt', () => { beforeEach(() => { + // Reset all mocks before each test + jest.clearAllMocks(); // Setup default config values globals.config.get.mockImplementation((path) => { if (path === 'Butler-SOS.mqttConfig.baseTopic') { @@ -496,7 +507,7 @@ describe('post-to-mqtt', () => { ); }); - test('should handle errors during publishing', () => { + test('should handle errors during publishing', async () => { // Force an error by making the MQTT client throw globals.mqttClient.publish.mockImplementation(() => { throw new Error('MQTT publish error'); @@ -515,11 +526,12 @@ describe('post-to-mqtt', () => { }; // Call the function being tested - postUserEventToMQTT(userEvent); + await postUserEventToMQTT(userEvent); // Verify error was logged - expect(globals.logger.error).toHaveBeenCalledWith( - expect.stringContaining('USER EVENT MQTT: Failed posting message to MQTT') + expect(mockLogError).toHaveBeenCalledWith( + expect.stringContaining('USER EVENT MQTT: Failed posting message to MQTT'), + expect.any(Error) ); }); }); diff --git a/src/lib/__tests__/post-to-new-relic.test.js b/src/lib/__tests__/post-to-new-relic.test.js index 127b31c..a38cbe8 100644 --- a/src/lib/__tests__/post-to-new-relic.test.js +++ b/src/lib/__tests__/post-to-new-relic.test.js @@ -39,6 +39,9 @@ jest.unstable_mockModule('../../globals.js', () => ({ debug: jest.fn(), error: jest.fn(), }, + errorTracker: { + incrementError: jest.fn(), + }, config: { get: jest.fn().mockImplementation((path) => { if (path === 'Butler-SOS.newRelic.enable') return true; diff --git a/src/lib/__tests__/proxysessionmetrics.test.js b/src/lib/__tests__/proxysessionmetrics.test.js index 44f30a9..cc409f3 100644 --- a/src/lib/__tests__/proxysessionmetrics.test.js +++ b/src/lib/__tests__/proxysessionmetrics.test.js @@ -52,6 +52,9 @@ jest.unstable_mockModule('../../globals.js', () => ({ debug: jest.fn(), error: jest.fn(), }, + errorTracker: { + incrementError: jest.fn(), + }, config: { get: jest.fn().mockImplementation((path) => { if (path === 'Butler-SOS.cert.clientCert') return '/path/to/cert.pem'; diff --git a/src/lib/udp_handlers/log_events/__tests__/sanitization.test.js b/src/lib/udp_handlers/log_events/__tests__/sanitization.test.js index b65c941..8049017 100644 --- a/src/lib/udp_handlers/log_events/__tests__/sanitization.test.js +++ b/src/lib/udp_handlers/log_events/__tests__/sanitization.test.js @@ -217,7 +217,7 @@ describe('Log Event Handler Sanitization', () => { }); describe('QIX Performance Event Handler', () => { - it('should sanitize method and object_type fields', () => { + it('should sanitize method and object_type fields', async () => { const msg = [ '/qseow-qix-perf/', '1', @@ -247,7 +247,7 @@ describe('Log Event Handler Sanitization', () => { 'linechart\x02', // Field 25: object_type ]; - const result = processQixPerfEvent(msg); + const result = await processQixPerfEvent(msg); if (result) { expect(result.method).not.toMatch(/[\x00-\x1F\x7F]/); expect(result.object_type).not.toMatch(/[\x00-\x1F\x7F]/);