|
| 1 | +// Logger.test.js |
| 2 | +const { Logger } = require('../src/Logger'); |
| 3 | +const fs = require('fs'); |
| 4 | +const { PassThrough } = require('stream'); |
| 5 | + |
| 6 | +jest.mock('fs'); |
| 7 | + |
| 8 | +describe('Logger class', () => { |
| 9 | + let logger; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + logger = new Logger({ |
| 13 | + level: 'debug', |
| 14 | + writeStream: null, |
| 15 | + }); |
| 16 | + }); |
| 17 | + |
| 18 | + afterEach(() => { |
| 19 | + jest.restoreAllMocks(); |
| 20 | + }); |
| 21 | + |
| 22 | + describe('translateToCode method', () => { |
| 23 | + it('should return 0 for "off" log level', () => { |
| 24 | + const result = logger.translateToCode('off'); |
| 25 | + expect(result).toBe(0); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should return 1 for "info" log level', () => { |
| 29 | + const result = logger.translateToCode('info'); |
| 30 | + expect(result).toBe(1); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should return 2 for "warn" log level', () => { |
| 34 | + const result = logger.translateToCode('warn'); |
| 35 | + expect(result).toBe(2); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should return 3 for "error" log level', () => { |
| 39 | + const result = logger.translateToCode('error'); |
| 40 | + expect(result).toBe(3); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should return 4 for "debug" log level', () => { |
| 44 | + const result = logger.translateToCode('debug'); |
| 45 | + expect(result).toBe(4); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should return 0 for invalid log level', () => { |
| 49 | + const result = logger.translateToCode('invalid'); |
| 50 | + expect(result).toBe(0); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + describe('info method', () => { |
| 55 | + it('should not log if log level is "off"', () => { |
| 56 | + logger.level = 0; |
| 57 | + const consoleLogSpy = jest.spyOn(console, 'log'); |
| 58 | + logger.info('Test message'); |
| 59 | + expect(consoleLogSpy).not.toHaveBeenCalled(); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should log if log level is "info"', () => { |
| 63 | + logger.level = 1; |
| 64 | + const consoleLogSpy = jest.spyOn(console, 'log'); |
| 65 | + logger.info('Test message'); |
| 66 | + expect(consoleLogSpy).toHaveBeenCalledTimes(1); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should log with extra information', () => { |
| 70 | + logger.level = 1; |
| 71 | + const consoleLogSpy = jest.spyOn(console, 'log'); |
| 72 | + logger.info('Test message', { extra: 'information' }); |
| 73 | + expect(consoleLogSpy).toHaveBeenCalledTimes(1); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('warn method', () => { |
| 78 | + it('should not log if log level is "off"', () => { |
| 79 | + logger.level = 0; |
| 80 | + const consoleLogSpy = jest.spyOn(console, 'log'); |
| 81 | + logger.warn('Test message'); |
| 82 | + expect(consoleLogSpy).not.toHaveBeenCalled(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should not log if log level is "info"', () => { |
| 86 | + logger.level = 1; |
| 87 | + const consoleLogSpy = jest.spyOn(console, 'log'); |
| 88 | + logger.warn('Test message'); |
| 89 | + expect(consoleLogSpy).not.toHaveBeenCalled(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should log if log level is "warn"', () => { |
| 93 | + logger.level = 2; |
| 94 | + const consoleLogSpy = jest.spyOn(console, 'log'); |
| 95 | + logger.warn('Test message'); |
| 96 | + expect(consoleLogSpy).toHaveBeenCalledTimes(1); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should log with extra information', () => { |
| 100 | + logger.level = 2; |
| 101 | + const consoleLogSpy = jest.spyOn(console, 'log'); |
| 102 | + logger.warn('Test message', { extra: 'information' }); |
| 103 | + expect(consoleLogSpy).toHaveBeenCalledTimes(1); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + describe('error method', () => { |
| 108 | + it('should not log if log level is "off"', () => { |
| 109 | + logger.level = 0; |
| 110 | + const consoleLogSpy = jest.spyOn(console, 'log'); |
| 111 | + logger.error('Test message'); |
| 112 | + expect(consoleLogSpy).not.toHaveBeenCalled(); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should not log if log level is "info"', () => { |
| 116 | + logger.level = 1; |
| 117 | + const consoleLogSpy = jest.spyOn(console, 'log'); |
| 118 | + logger.error('Test message'); |
| 119 | + expect(consoleLogSpy).not.toHaveBeenCalled(); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should not log if log level is "warn"', () => { |
| 123 | + logger.level = 2; |
| 124 | + const consoleLogSpy = jest.spyOn(console, 'log'); |
| 125 | + logger.error('Test message'); |
| 126 | + expect(consoleLogSpy).not.toHaveBeenCalled(); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should log if log level is "error"', () => { |
| 130 | + logger.level = 3; |
| 131 | + const consoleLogSpy = jest.spyOn(console, 'log'); |
| 132 | + logger.error('Test message'); |
| 133 | + logger.error('Test message', { mock: 'all' }); |
| 134 | + logger.error({ mock: 'all' }); |
| 135 | + |
| 136 | + expect(consoleLogSpy).toHaveBeenCalledTimes(3); |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + describe('fatal method', () => { |
| 141 | + it('should exit the process with code -1 if no message is provided', () => { |
| 142 | + const processExitSpy = jest.spyOn(process, 'exit'); |
| 143 | + const mockExit = jest |
| 144 | + .spyOn(process, 'exit') |
| 145 | + .mockImplementation((number) => { |
| 146 | + throw new Error('process.exit: ' + number); |
| 147 | + }); |
| 148 | + expect(() => { |
| 149 | + logger.fatal(); |
| 150 | + }).toThrow(); |
| 151 | + expect(() => { |
| 152 | + logger.fatal('error'); |
| 153 | + }).toThrow(); |
| 154 | + expect(() => { |
| 155 | + logger.fatal('error', 'extra'); |
| 156 | + }).toThrow(); |
| 157 | + |
| 158 | + expect(() => { |
| 159 | + logger.fatal('error', { mock: 'me' }); |
| 160 | + }).toThrow(); |
| 161 | + |
| 162 | + expect(() => { |
| 163 | + logger.fatal({ mock: 'me' }); |
| 164 | + }).toThrow(); |
| 165 | + |
| 166 | + expect(() => { |
| 167 | + logger.fatal({ mock: 'me' }, '22'); |
| 168 | + }).toThrow(); |
| 169 | + |
| 170 | + expect(mockExit).toHaveBeenCalledWith(-1); |
| 171 | + expect(processExitSpy).toHaveBeenCalledTimes(6); |
| 172 | + mockExit.mockRestore(); |
| 173 | + }); |
| 174 | + }); |
| 175 | + |
| 176 | + describe('debug method', () => { |
| 177 | + it('should not log if log level is "off"', () => { |
| 178 | + logger.level = 0; |
| 179 | + const consoleDebugSpy = jest.spyOn(console, 'debug'); |
| 180 | + logger.debug('Test message'); |
| 181 | + expect(consoleDebugSpy).not.toHaveBeenCalled(); |
| 182 | + }); |
| 183 | + |
| 184 | + it('should not log if log level is "info"', () => { |
| 185 | + logger.level = 1; |
| 186 | + const consoleDebugSpy = jest.spyOn(console, 'debug'); |
| 187 | + logger.debug('Test message'); |
| 188 | + expect(consoleDebugSpy).not.toHaveBeenCalled(); |
| 189 | + }); |
| 190 | + |
| 191 | + it('should not log if log level is "warn"', () => { |
| 192 | + logger.level = 2; |
| 193 | + const consoleDebugSpy = jest.spyOn(console, 'debug'); |
| 194 | + logger.debug('Test message'); |
| 195 | + expect(consoleDebugSpy).not.toHaveBeenCalled(); |
| 196 | + }); |
| 197 | + |
| 198 | + it('should not log if log level is "error"', () => { |
| 199 | + logger.level = 3; |
| 200 | + const consoleDebugSpy = jest.spyOn(console, 'debug'); |
| 201 | + logger.debug('Test message'); |
| 202 | + expect(consoleDebugSpy).not.toHaveBeenCalled(); |
| 203 | + }); |
| 204 | + |
| 205 | + it('should log if log level is "debug"', () => { |
| 206 | + logger.level = 4; |
| 207 | + const consoleDebugSpy = jest.spyOn(console, 'debug'); |
| 208 | + logger.debug('Test message'); |
| 209 | + expect(consoleDebugSpy).toHaveBeenCalledTimes(1); |
| 210 | + }); |
| 211 | + |
| 212 | + it('should log if log level is "debug" with extra', () => { |
| 213 | + logger.level = 4; |
| 214 | + const consoleDebugSpy = jest.spyOn(console, 'log'); |
| 215 | + logger.debug('Test message', 'extraaa'); |
| 216 | + logger.debug('Test message', { test: 'me' }); |
| 217 | + logger.debug({ test: 'me' }, { test: 'me' }); |
| 218 | + logger.debug({ test: 'me' }, 'me'); |
| 219 | + logger.debug({ test: 'me' }); |
| 220 | + |
| 221 | + expect(consoleDebugSpy).toHaveBeenCalledTimes(5); |
| 222 | + }); |
| 223 | + }); |
| 224 | + |
| 225 | + describe('writeToFile method', () => { |
| 226 | + it('should stringify ', () => { |
| 227 | + const mockWriteable = new PassThrough(); |
| 228 | + fs.createWriteStream.mockReturnValueOnce(mockWriteable); |
| 229 | + |
| 230 | + const log = new Logger({ |
| 231 | + level: 'debug', |
| 232 | + writeStream: fs.createWriteStream('here.txt'), |
| 233 | + }); |
| 234 | + const consoleDebugSpy = jest.spyOn(log, 'writeToFile'); |
| 235 | + log.writeToFile('hello'); |
| 236 | + log.writeToFile({ pro: 'name' }); |
| 237 | + |
| 238 | + log.writeToFile({ pro: 'name' }, { another: 1 }); |
| 239 | + log.writeToFile({ pro: 'name' }, 1); |
| 240 | + |
| 241 | + expect(consoleDebugSpy).toHaveBeenCalledTimes(4); |
| 242 | + }); |
| 243 | + }); |
| 244 | +}); |
0 commit comments