Fix broken tests

This commit is contained in:
Göran Sander
2025-12-15 07:38:10 +01:00
parent 5ff9e7c566
commit 8713b84e41
5 changed files with 27 additions and 6 deletions

View File

@@ -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(),

View File

@@ -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)
);
});
});

View File

@@ -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;

View File

@@ -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';

View File

@@ -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]/);