|
const { |
|
LOG_LEVEL_DEBUG, |
|
LOG_LEVEL_INFO, |
|
LOG_LEVEL_WARN, |
|
LOG_LEVEL_ERROR |
|
} = require('./logger'); |
|
|
|
describe('Logger', () => { |
|
// const OLD_ENV = process.env; |
|
|
|
beforeEach(() => { |
|
jest.resetModules(); |
|
// process.env = { ...OLD_ENV }; |
|
// delete process.env.LOG_LEVEL; |
|
}); |
|
|
|
afterEach(() => { |
|
// process.env = OLD_ENV; |
|
delete process.env.LOG_LEVEL; |
|
}); |
|
|
|
describe('debug()', () => { |
|
const originalLogger = console.debug; |
|
beforeEach(() => { |
|
console.debug = jest.fn(); |
|
}); |
|
|
|
afterEach(() => { |
|
console.debug = originalLogger; |
|
jest.clearAllMocks(); |
|
}); |
|
|
|
it('with LOG_LEVEL_DEBUG', () => { |
|
process.env.LOG_LEVEL = LOG_LEVEL_DEBUG; |
|
const { debug } = require('./logger'); |
|
|
|
debug('test', 'test2'); |
|
expect(console.debug).toBeCalledWith('test', 'test2'); |
|
}); |
|
|
|
it('with LOG_LEVEL_ERROR', () => { |
|
process.env.LOG_LEVEL = LOG_LEVEL_ERROR; |
|
const { debug } = require('./logger'); |
|
|
|
debug('test', 'test2'); |
|
expect(console.debug).not.toBeCalled(); |
|
}); |
|
}); |
|
|
|
describe('info()', () => { |
|
const originalLogger = console.info; |
|
beforeEach(() => { |
|
console.info = jest.fn(); |
|
}); |
|
|
|
afterEach(() => { |
|
console.info = originalLogger; |
|
jest.clearAllMocks(); |
|
}); |
|
|
|
it('with LOG_LEVEL_DEBUG', () => { |
|
process.env.LOG_LEVEL = LOG_LEVEL_DEBUG; |
|
const { info } = require('./logger'); |
|
|
|
info('test', 'test2'); |
|
expect(console.info).toBeCalledWith('test', 'test2'); |
|
}); |
|
|
|
it('with LOG_LEVEL info', () => { |
|
process.env.LOG_LEVEL = LOG_LEVEL_INFO; |
|
const { info } = require('./logger'); |
|
|
|
info('test', 'test2'); |
|
expect(console.info).toBeCalledWith('test', 'test2'); |
|
}); |
|
|
|
it('with LOG_LEVEL_ERROR', () => { |
|
process.env.LOG_LEVEL = LOG_LEVEL_ERROR; |
|
const { info } = require('./logger'); |
|
|
|
info('test', 'test2'); |
|
expect(console.info).not.toBeCalled(); |
|
}); |
|
}); |
|
|
|
describe('warn()', () => { |
|
const originalLogger = console.warn; |
|
beforeEach(() => { |
|
console.warn = jest.fn(); |
|
}); |
|
|
|
afterEach(() => { |
|
console.warn = originalLogger; |
|
jest.clearAllMocks(); |
|
}); |
|
|
|
it('with LOG_LEVEL_DEBUG', () => { |
|
process.env.LOG_LEVEL = LOG_LEVEL_DEBUG; |
|
const { warn } = require('./logger'); |
|
|
|
warn('test', 'test2'); |
|
expect(console.warn).toBeCalledWith('test', 'test2'); |
|
}); |
|
|
|
it('with LOG_LEVEL_WARN', () => { |
|
process.env.LOG_LEVEL = LOG_LEVEL_WARN; |
|
const { warn } = require('./logger'); |
|
|
|
warn('test', 'test2'); |
|
expect(console.warn).toBeCalledWith('test', 'test2'); |
|
}); |
|
|
|
it('with LOG_LEVEL_ERROR', () => { |
|
process.env.LOG_LEVEL = LOG_LEVEL_ERROR; |
|
const warn = require('./logger').warn; |
|
warn('test', 'test2'); |
|
expect(console.warn).not.toBeCalled(); |
|
}); |
|
}); |
|
|
|
describe('error()', () => { |
|
const originalLogger = console.error; |
|
beforeEach(() => { |
|
console.error = jest.fn(); |
|
}); |
|
|
|
afterEach(() => { |
|
console.error = originalLogger; |
|
jest.clearAllMocks(); |
|
}); |
|
|
|
it('with LOG_LEVEL_DEBUG', () => { |
|
process.env.LOG_LEVEL = LOG_LEVEL_DEBUG; |
|
const { error } = require('./logger'); |
|
|
|
error('test', 'test2'); |
|
expect(console.error).toBeCalledWith('test', 'test2'); |
|
}); |
|
|
|
it('with LOG_LEVEL_ERROR', () => { |
|
process.env.LOG_LEVEL = LOG_LEVEL_ERROR; |
|
const { error } = require('./logger'); |
|
error('test', 'test2'); |
|
expect(console.error).toBeCalledWith('test', 'test2'); |
|
}); |
|
}); |
|
|
|
describe('time', () => { |
|
const originalLogger = console.time; |
|
beforeEach(() => { |
|
console.time = jest.fn(); |
|
}); |
|
|
|
afterEach(() => { |
|
console.time = originalLogger; |
|
jest.clearAllMocks(); |
|
}); |
|
|
|
it('with LOG_LEVEL_DEBUG', () => { |
|
process.env.LOG_LEVEL = LOG_LEVEL_DEBUG; |
|
const { time } = require('./logger'); |
|
|
|
time('test'); |
|
expect(console.time).toBeCalledWith('test'); |
|
}); |
|
|
|
it('with LOG_LEVEL_ERROR', () => { |
|
process.env.LOG_LEVEL = LOG_LEVEL_ERROR; |
|
const { time } = require('./logger'); |
|
time('test'); |
|
expect(console.time).not.toBeCalled(); |
|
}); |
|
}); |
|
|
|
describe('timeEnd', () => { |
|
const originalLogger = console.timeEnd; |
|
beforeEach(() => { |
|
console.timeEnd = jest.fn(); |
|
}); |
|
|
|
afterEach(() => { |
|
console.timeEnd = originalLogger; |
|
jest.clearAllMocks(); |
|
}); |
|
|
|
it('with LOG_LEVEL_DEBUG', () => { |
|
process.env.LOG_LEVEL = LOG_LEVEL_DEBUG; |
|
const { timeEnd } = require('./logger'); |
|
|
|
timeEnd('test'); |
|
expect(console.timeEnd).toBeCalledWith('test'); |
|
}); |
|
|
|
it('with LOG_LEVEL_ERROR', () => { |
|
process.env.LOG_LEVEL = LOG_LEVEL_ERROR; |
|
const { timeEnd } = require('./logger'); |
|
timeEnd('test'); |
|
expect(console.timeEnd).not.toBeCalled(); |
|
}); |
|
}) |
|
}); |