|
| 1 | +/* eslint-disable no-console */ |
| 2 | +import { logger, LogLevel } from '../../src'; |
| 3 | + |
| 4 | +// Mock the config module |
| 5 | +const mockConfigStore: { logLevel: LogLevel; [key: string]: any } = { |
| 6 | + logLevel: 'INFO', |
| 7 | +}; |
| 8 | + |
| 9 | +jest.mock('../../src/global/config', () => ({ |
| 10 | + config: { |
| 11 | + get: jest.fn().mockImplementation((key: string, defaultValue?: any) => { |
| 12 | + return mockConfigStore[key] ?? defaultValue; |
| 13 | + }), |
| 14 | + set: jest.fn().mockImplementation((key: string, value: any) => { |
| 15 | + mockConfigStore[key] = value; |
| 16 | + }), |
| 17 | + }, |
| 18 | +})); |
| 19 | + |
| 20 | +// Mock console methods |
| 21 | +const mockConsole = { |
| 22 | + debug: jest.fn(), |
| 23 | + info: jest.fn(), |
| 24 | + warn: jest.fn(), |
| 25 | + error: jest.fn(), |
| 26 | + log: jest.fn(), |
| 27 | +}; |
| 28 | + |
| 29 | +global.console = mockConsole as any; |
| 30 | + |
| 31 | +describe('Logger', () => { |
| 32 | + // const gLog = jest.spyOn(global.console, 'log'); |
| 33 | + const gInfo = jest.spyOn(global.console, 'info'); |
| 34 | + const gDebug = jest.spyOn(global.console, 'debug'); |
| 35 | + const gWarn = jest.spyOn(global.console, 'warn'); |
| 36 | + const gError = jest.spyOn(global.console, 'error'); |
| 37 | + |
| 38 | + beforeEach(() => { |
| 39 | + // Reset mock config and console calls |
| 40 | + mockConfigStore.logLevel = 'INFO'; |
| 41 | + jest.clearAllMocks(); |
| 42 | + jest.useFakeTimers(); |
| 43 | + jest.setSystemTime(new Date('2024-01-01T00:00:00Z')); |
| 44 | + }); |
| 45 | + |
| 46 | + afterEach(() => { |
| 47 | + jest.useRealTimers(); |
| 48 | + }); |
| 49 | + |
| 50 | + describe('Log Level Configuration', () => { |
| 51 | + it('should have config log level', () => { |
| 52 | + expect(logger.getLogLevel()).toBe('INFO'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should set and get log level OFF', () => { |
| 56 | + logger.setLogLevel('OFF'); |
| 57 | + expect(logger.getLogLevel()).toBe('OFF'); |
| 58 | + expect(logger.getEnabledLogLevels()).toStrictEqual([]); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should set and get log level FATAL', () => { |
| 62 | + logger.setLogLevel('FATAL'); |
| 63 | + expect(logger.getLogLevel()).toBe('FATAL'); |
| 64 | + expect(logger.getEnabledLogLevels()).toStrictEqual(['FATAL']); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should set and get log level ERROR', () => { |
| 68 | + logger.setLogLevel('ERROR'); |
| 69 | + expect(logger.getLogLevel()).toBe('ERROR'); |
| 70 | + expect(logger.getEnabledLogLevels()).toStrictEqual(['ERROR', 'FATAL']); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should set and get log level WARN', () => { |
| 74 | + logger.setLogLevel('WARN'); |
| 75 | + expect(logger.getLogLevel()).toBe('WARN'); |
| 76 | + expect(logger.getEnabledLogLevels()).toStrictEqual(['WARN', 'ERROR', 'FATAL']); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should set and get log level INFO', () => { |
| 80 | + logger.setLogLevel('INFO'); |
| 81 | + expect(logger.getLogLevel()).toBe('INFO'); |
| 82 | + expect(logger.getEnabledLogLevels()).toStrictEqual(['INFO', 'WARN', 'ERROR', 'FATAL']); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should set and get log level DEBUG', () => { |
| 86 | + logger.setLogLevel('DEBUG'); |
| 87 | + expect(logger.getLogLevel()).toBe('DEBUG'); |
| 88 | + expect(logger.getEnabledLogLevels()).toStrictEqual([ |
| 89 | + 'DEBUG', |
| 90 | + 'INFO', |
| 91 | + 'WARN', |
| 92 | + 'ERROR', |
| 93 | + 'FATAL', |
| 94 | + ]); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('Log Filtering', () => { |
| 99 | + it('should log messages at or above current level', () => { |
| 100 | + logger.setLogLevel('WARN'); |
| 101 | + |
| 102 | + logger.debug('Debug message'); |
| 103 | + logger.warn('Warning message'); |
| 104 | + |
| 105 | + expect(gDebug).not.toHaveBeenCalled(); |
| 106 | + expect(gWarn).toHaveBeenCalled(); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should not log when level is OFF', () => { |
| 110 | + logger.setLogLevel('OFF'); |
| 111 | + |
| 112 | + logger.error('Error message'); |
| 113 | + logger.fatal('Fatal message'); |
| 114 | + |
| 115 | + expect(gError).not.toHaveBeenCalled(); |
| 116 | + expect(gError).not.toHaveBeenCalled(); |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + describe('Log Methods', () => { |
| 121 | + it('should format messages correctly', () => { |
| 122 | + logger.info('Test message', { key: 'value' }); |
| 123 | + |
| 124 | + const expectedMessage = `[2024-01-01T00:00:00.000Z] INFO: Test message\n${JSON.stringify({ key: 'value' }, null, 2)}`; |
| 125 | + expect(gInfo).toHaveBeenCalledWith(expectedMessage); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should use appropriate console methods', () => { |
| 129 | + logger.setLogLevel('DEBUG'); |
| 130 | + logger.debug('Debug'); |
| 131 | + logger.info('Info'); |
| 132 | + logger.warn('Warn'); |
| 133 | + logger.error('Error'); |
| 134 | + logger.fatal('Fatal'); |
| 135 | + |
| 136 | + expect(gDebug).toHaveBeenCalled(); |
| 137 | + expect(gInfo).toHaveBeenCalled(); |
| 138 | + expect(gWarn).toHaveBeenCalled(); |
| 139 | + expect(gError).toHaveBeenCalledTimes(2); |
| 140 | + }); |
| 141 | + }); |
| 142 | + |
| 143 | + describe('Edge Cases', () => { |
| 144 | + it('should handle empty data', () => { |
| 145 | + logger.info('Message without data'); |
| 146 | + const expectedMessage = '[2024-01-01T00:00:00.000Z] INFO: Message without data'; |
| 147 | + expect(gInfo).toHaveBeenCalledWith(expectedMessage); |
| 148 | + }); |
| 149 | + |
| 150 | + it('should handle circular data structures', () => { |
| 151 | + logger.setLogLevel('DEBUG'); |
| 152 | + const circularObj: any = { a: 'test' }; |
| 153 | + circularObj.myself = circularObj; |
| 154 | + |
| 155 | + logger.error('Circular error', circularObj); |
| 156 | + |
| 157 | + // Should handle circular references in stringification |
| 158 | + expect(gError).toHaveBeenCalled(); |
| 159 | + }); |
| 160 | + }); |
| 161 | +}); |
0 commit comments