Skip to content

Commit

Permalink
test(utils/logger): adds tests for logger
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed May 9, 2019
1 parent 2bace5a commit 5de16eb
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions test/utils/logger.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/* eslint-disable no-console */
import logger, { setLevel } from '~/utils/logger';
import chalk from 'chalk';

chalk.enabled = false;
console.trace = jest.fn();
console.info = jest.fn();
console.warn = jest.fn();
console.error = jest.fn();
const mocks: { [key: string]: jest.Mock } = {
trace: console.trace,
info: console.info,
warn: console.warn,
error: console.error
} as any;

beforeEach(() => Object.values(mocks).forEach((mock) => mock.mockClear()));

test(`has default level`, () => {
expect(logger.getLevel()).toBe(2);
});
test(`sets level`, () => {
expect(setLevel('debug')).toBeUndefined();
expect(logger.getLevel()).toBe(1);
});
test(`doesn't prefix over info`, () => {
setLevel('info');

logger.info('message');
logger.error('message');
logger.warn('message');
expect(mocks.info).toHaveBeenCalledTimes(1);
expect(mocks.info.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"message",
]
`);
expect(mocks.warn).toHaveBeenCalledTimes(1);
expect(mocks.warn.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"WARN: message",
]
`);
expect(mocks.error).toHaveBeenCalledTimes(1);
expect(mocks.error.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"ERROR: message",
]
`);
});

test(`doesn't prefixes under info`, () => {
setLevel('trace');

logger.trace('message');
logger.info('message');
logger.error('message');
logger.warn('message');
expect(mocks.trace).toHaveBeenCalledTimes(1);
expect(mocks.trace.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"[TRACE] kpo: message",
]
`);
expect(mocks.info).toHaveBeenCalledTimes(1);
expect(mocks.info.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"[INFO] kpo: message",
]
`);
expect(mocks.warn).toHaveBeenCalledTimes(1);
expect(mocks.warn.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"[WARN] kpo: message",
]
`);
expect(mocks.error).toHaveBeenCalledTimes(1);
expect(mocks.error.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"[ERROR] kpo: message",
]
`);
});

0 comments on commit 5de16eb

Please sign in to comment.