autobaan/tests/unit/common/logger.test.ts
2023-05-23 15:09:13 -05:00

84 lines
2.2 KiB
TypeScript

import dayjs from '../../../src/common/dayjs'
import { Logger, LogLevel } from '../../../src/common/logger'
jest.useFakeTimers().setSystemTime(new Date('2023-01-01'))
describe('Logger', () => {
beforeEach(() => {
jest.resetAllMocks()
})
test('should log messages', () => {
const consoleLogSpy = jest.fn()
const consoleErrorSpy = jest.fn()
jest.spyOn(console, 'log').mockImplementation(consoleLogSpy)
jest.spyOn(console, 'error').mockImplementation(consoleErrorSpy)
const logger = new Logger('tag', 'abc', LogLevel.DEBUG)
logger.debug('first')
logger.info('second')
logger.error('third', { errorMessage: 'test' })
expect(consoleLogSpy).toHaveBeenCalledTimes(2)
expect(consoleLogSpy).toHaveBeenNthCalledWith(
1,
'(%s) <%s> [%s] %s: %s',
dayjs().format(),
'tag',
'abc',
'DEBUG',
'first'
)
expect(consoleLogSpy).toHaveBeenNthCalledWith(
2,
'(%s) <%s> [%s] %s: %s',
dayjs().format(),
'tag',
'abc',
'INFO',
'second'
)
expect(consoleErrorSpy).toHaveBeenCalledTimes(1)
expect(consoleErrorSpy).toHaveBeenCalledWith(
'(%s) <%s> [%s] %s: %s - %O',
dayjs().format(),
'tag',
'abc',
'ERROR',
'third',
{ errorMessage: 'test' }
)
})
test('should log only when level is >= LogLevel of LoggerInstance', () => {
const consoleLogSpy = jest.fn()
jest.spyOn(console, 'log').mockImplementationOnce(consoleLogSpy)
const logger = new Logger('tag', 'abc', LogLevel.INFO)
logger.debug("shouldn't appear")
expect(consoleLogSpy).not.toHaveBeenCalled()
})
test('should obfuscate password from message', () => {
const consoleLogSpy = jest.fn()
const consoleErrorSpy = jest.fn()
jest.spyOn(console, 'log').mockImplementation(consoleLogSpy)
jest.spyOn(console, 'error').mockImplementation(consoleErrorSpy)
const logger = new Logger('tag', 'abc', LogLevel.DEBUG)
logger.info('first', { password: 'test' })
expect(consoleLogSpy).toHaveBeenCalledTimes(1)
expect(consoleLogSpy).toHaveBeenNthCalledWith(
1,
'(%s) <%s> [%s] %s: %s - %O',
dayjs().format(),
'tag',
'abc',
'INFO',
'first',
{ password: '***' }
)
})
})