feat: add Jest testing framework and implement unit tests for configuration and service logic

This commit is contained in:
Shrev Dev
2025-10-21 11:08:26 -05:00
parent 526298bea4
commit 4ee1442589
12 changed files with 4517 additions and 3 deletions
+76
View File
@@ -0,0 +1,76 @@
import { UptimeKumaService } from '../src/services/uptime-kuma.service';
// Mock socket.io-client
jest.mock('socket.io-client', () => ({
io: jest.fn().mockImplementation(() => ({
connected: false,
connect: jest.fn(),
disconnect: jest.fn(),
on: jest.fn(),
emit: jest.fn(),
})),
}));
// Mock other dependencies
jest.mock('../src/config/config');
jest.mock('../src/utils/logger');
describe('UptimeKumaService', () => {
let uptimeKumaService: UptimeKumaService;
beforeEach(() => {
jest.clearAllMocks();
uptimeKumaService = new UptimeKumaService();
});
test('should create service instance', () => {
expect(uptimeKumaService).toBeDefined();
});
test('should return connection status', () => {
// Initially should be disconnected
expect(uptimeKumaService.isConnected()).toBe(false);
});
test('should handle connection state changes', () => {
// Mock socket connection
const mockSocket = {
connected: true,
connect: jest.fn(),
disconnect: jest.fn(),
on: jest.fn(),
emit: jest.fn(),
};
// Simulate connection by mocking the socket property and authentication
(uptimeKumaService as any).socket = mockSocket;
(uptimeKumaService as any).isAuthenticated = true;
expect(uptimeKumaService.isConnected()).toBe(true);
// Simulate disconnection
mockSocket.connected = false;
expect(uptimeKumaService.isConnected()).toBe(false);
});
test('should handle force reconnect', async () => {
// Mock the connect method to avoid socket.once issues
const mockConnect = jest.fn().mockResolvedValue(undefined);
(uptimeKumaService as any).connect = mockConnect;
// Should not throw error even when not connected
await expect(uptimeKumaService.forceReconnect()).resolves.not.toThrow();
expect(mockConnect).toHaveBeenCalled();
});
test('should handle disconnect', () => {
// Should not throw error
expect(() => uptimeKumaService.disconnect()).not.toThrow();
});
test('should get monitor stats', () => {
const stats = uptimeKumaService.getMonitorStats();
expect(stats).toBeDefined();
expect(Array.isArray(stats)).toBe(true);
});
});