|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { Logger } from '../../src/Logger.js'; |
| 3 | +import { LogLevel, LogLevelThreshold } from '../../src/constants.js'; |
| 4 | +import type { ConfigServiceInterface } from '../../src/types/ConfigServiceInterface.js'; |
| 5 | +import type { |
| 6 | + LogFunction, |
| 7 | + LogLevel as LogLevelType, |
| 8 | +} from '../../src/types/Logger.js'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Helper function to get the console method for a given log level, we use this |
| 12 | + * for properly mocking the console methods in the tests and account for the |
| 13 | + * fact that `critical` is not a valid console method, which we proxy to `error`, |
| 14 | + * and `trace` is internally proxied to `log`. |
| 15 | + * |
| 16 | + * @param method - The method to get the console method for |
| 17 | + */ |
| 18 | +const getConsoleMethod = ( |
| 19 | + method: string |
| 20 | +): keyof Omit<LogFunction, 'critical'> | 'log' => { |
| 21 | + const lowercaseMethod = method.toLowerCase(); |
| 22 | + switch (lowercaseMethod) { |
| 23 | + case 'trace': |
| 24 | + return 'log'; |
| 25 | + case 'critical': |
| 26 | + return 'error'; |
| 27 | + default: |
| 28 | + return lowercaseMethod as keyof Omit<LogFunction, 'critical'>; |
| 29 | + } |
| 30 | +}; |
| 31 | + |
| 32 | +describe('Log flush buffer', () => { |
| 33 | + const ENVIRONMENT_VARIABLES = process.env; |
| 34 | + |
| 35 | + beforeEach(() => { |
| 36 | + process.env = { ...ENVIRONMENT_VARIABLES, POWERTOOLS_DEV: 'true' }; |
| 37 | + vi.resetAllMocks(); |
| 38 | + }); |
| 39 | + |
| 40 | + it("doesn't flush buffer when calling a log level lower than the configured log level", () => { |
| 41 | + // Prepare |
| 42 | + const logger = new Logger({ logLevel: 'WARN' }); |
| 43 | + |
| 44 | + // Act |
| 45 | + logger.debug('debug'); |
| 46 | + logger.info('info'); |
| 47 | + |
| 48 | + // Assess |
| 49 | + expect(console.debug).not.toHaveBeenCalled(); |
| 50 | + expect(console.info).not.toHaveBeenCalled(); |
| 51 | + }); |
| 52 | + |
| 53 | + it("doesn't flush buffer when calling a log level equal to the configured log level", () => { |
| 54 | + // Prepare |
| 55 | + const logger = new Logger({ logLevel: 'WARN' }); |
| 56 | + |
| 57 | + // Act |
| 58 | + logger.debug('debug'); |
| 59 | + logger.info('info'); |
| 60 | + logger.warn('warn'); |
| 61 | + |
| 62 | + // Assess |
| 63 | + expect(console.debug).not.toHaveBeenCalled(); |
| 64 | + expect(console.info).not.toHaveBeenCalled(); |
| 65 | + expect(console.warn).toHaveBeenCalled(); |
| 66 | + }); |
| 67 | + |
| 68 | + it('flushes buffer when calling a log level higher than the configured log level', () => { |
| 69 | + // Prepare |
| 70 | + const logger = new Logger({ logLevel: 'WARN' }); |
| 71 | + |
| 72 | + // Act |
| 73 | + logger.debug('debug'); |
| 74 | + logger.info('info'); |
| 75 | + logger.warn('warn'); |
| 76 | + logger.error('error'); |
| 77 | + |
| 78 | + // Assess |
| 79 | + expect(console.debug).toHaveBeenCalled(); |
| 80 | + expect(console.info).toHaveBeenCalled(); |
| 81 | + expect(console.warn).toHaveBeenCalled(); |
| 82 | + expect(console.error).toHaveBeenCalled(); |
| 83 | + }); |
| 84 | +}); |
0 commit comments