Skip to content

Commit

Permalink
Simple console message formatting (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
surol authored Jun 2, 2021
1 parent 4545ff0 commit 0ba0d13
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 12 deletions.
8 changes: 4 additions & 4 deletions src/console-logger.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Logger } from './logger';

const consoleLogger$log = (log: (...args: unknown[]) => void) => (...args: unknown[]) => {
if (!args.length) {
log();
} else {
if (args.length && typeof args[0] === 'string') {
// Avoid formatting.
log('%O', ...args);
log('%s', ...args);
} else {
log(...args);
}
};

Expand Down
60 changes: 52 additions & 8 deletions src/console.logger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,20 @@ describe('consoleLogger', () => {
logSpy = jest.spyOn(console, 'error').mockImplementation(() => { /* do not log */ });
});

it('logs to console', () => {
it('logs message to console', () => {
consoleLogger.error('message', 1, 2, 3);
expect(logSpy).toHaveBeenCalledWith('%O', 'message', 1, 2, 3);
expect(logSpy).toHaveBeenCalledWith('%s', 'message', 1, 2, 3);
});
it('logs object to console', () => {

const object = { name: 'test' };

consoleLogger.error(object, 1, 2, 3);
expect(logSpy).toHaveBeenCalledWith(object, 1, 2, 3);
});
it('logs `undefined` to console', () => {
consoleLogger.error(undefined);
expect(logSpy).toHaveBeenCalledWith(undefined);
});
it('logs empty message to console', () => {
consoleLogger.error();
Expand All @@ -32,9 +43,20 @@ describe('consoleLogger', () => {
logSpy = jest.spyOn(console, 'warn').mockImplementation(() => { /* do not log */ });
});

it('logs to console', () => {
it('logs message to console', () => {
consoleLogger.warn('message', 1, 2, 3);
expect(logSpy).toHaveBeenCalledWith('%O', 'message', 1, 2, 3);
expect(logSpy).toHaveBeenCalledWith('%s', 'message', 1, 2, 3);
});
it('logs object to console', () => {

const object = { name: 'test' };

consoleLogger.warn(object, 1, 2, 3);
expect(logSpy).toHaveBeenCalledWith(object, 1, 2, 3);
});
it('logs `undefined` to console', () => {
consoleLogger.warn(undefined);
expect(logSpy).toHaveBeenCalledWith(undefined);
});
it('logs empty message to console', () => {
consoleLogger.warn();
Expand All @@ -48,9 +70,20 @@ describe('consoleLogger', () => {
logSpy = jest.spyOn(console, 'info').mockImplementation(() => { /* do not log */ });
});

it('logs to console', () => {
it('logs message to console', () => {
consoleLogger.info('message', 1, 2, 3);
expect(logSpy).toHaveBeenCalledWith('%O', 'message', 1, 2, 3);
expect(logSpy).toHaveBeenCalledWith('%s', 'message', 1, 2, 3);
});
it('logs object to console', () => {

const object = { name: 'test' };

consoleLogger.info(object, 1, 2, 3);
expect(logSpy).toHaveBeenCalledWith(object, 1, 2, 3);
});
it('logs `undefined` to console', () => {
consoleLogger.info(undefined);
expect(logSpy).toHaveBeenCalledWith(undefined);
});
it('logs empty message to console', () => {
consoleLogger.info();
Expand All @@ -64,9 +97,20 @@ describe('consoleLogger', () => {
logSpy = jest.spyOn(console, 'debug').mockImplementation(() => { /* do not log */ });
});

it('logs to console', () => {
it('logs message to console', () => {
consoleLogger.debug('message', 1, 2, 3);
expect(logSpy).toHaveBeenCalledWith('%O', 'message', 1, 2, 3);
expect(logSpy).toHaveBeenCalledWith('%s', 'message', 1, 2, 3);
});
it('logs object to console', () => {

const object = { name: 'test' };

consoleLogger.debug(object, 1, 2, 3);
expect(logSpy).toHaveBeenCalledWith(object, 1, 2, 3);
});
it('logs `undefined` to console', () => {
consoleLogger.debug(undefined);
expect(logSpy).toHaveBeenCalledWith(undefined);
});
it('logs empty message to console', () => {
consoleLogger.debug();
Expand Down

0 comments on commit 0ba0d13

Please sign in to comment.