wip
This commit is contained in:
6
packages/shared/logger/ILogger.ts
Normal file
6
packages/shared/logger/ILogger.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export interface ILogger {
|
||||
debug(message: string, context?: Record<string, any>): void;
|
||||
info(message: string, context?: Record<string, any>): void;
|
||||
warn(message: string, context?: Record<string, any>): void;
|
||||
error(message: string, error?: Error, context?: Record<string, any>): void;
|
||||
}
|
||||
20
packages/shared/logging/ConsoleLogger.ts
Normal file
20
packages/shared/logging/ConsoleLogger.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { ILogger } from './ILogger';
|
||||
|
||||
export class ConsoleLogger implements ILogger {
|
||||
debug(message: string, ...args: any[]): void {
|
||||
console.debug(message, ...args);
|
||||
}
|
||||
|
||||
info(message: string, ...args: any[]): void {
|
||||
console.info(message, ...args);
|
||||
}
|
||||
|
||||
warn(message: string, ...args: any[]): void {
|
||||
console.warn(message, ...args);
|
||||
}
|
||||
|
||||
error(message: string, ...args: any[]): void {
|
||||
console.error(message, ...args);
|
||||
}
|
||||
|
||||
}
|
||||
7
packages/shared/logging/ILogger.ts
Normal file
7
packages/shared/logging/ILogger.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
|
||||
export interface ILogger {
|
||||
debug(message: string, ...args: any[]): void;
|
||||
info(message: string, ...args: any[]): void;
|
||||
warn(message: string, ...args: any[]): void;
|
||||
error(message: string, ...args: any[]): void;
|
||||
}
|
||||
69
packages/shared/tests/unit/logging/ConsoleLogger.test.ts
Normal file
69
packages/shared/tests/unit/logging/ConsoleLogger.test.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import { vi } from 'vitest';
|
||||
import { ConsoleLogger } from '../../../logging/ConsoleLogger'; // Assuming ConsoleLogger is here
|
||||
|
||||
describe('ConsoleLogger', () => {
|
||||
let logger: ConsoleLogger;
|
||||
let consoleDebugSpy: vi.SpyInstance;
|
||||
let consoleInfoSpy: vi.SpyInstance;
|
||||
let consoleWarnSpy: vi.SpyInstance;
|
||||
let consoleErrorSpy: vi.SpyInstance;
|
||||
let consoleLogSpy: vi.SpyInstance;
|
||||
|
||||
beforeEach(() => {
|
||||
logger = new ConsoleLogger();
|
||||
consoleDebugSpy = vi.spyOn(console, 'debug').mockImplementation(() => {});
|
||||
consoleInfoSpy = vi.spyOn(console, 'info').mockImplementation(() => {});
|
||||
consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
||||
consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
||||
consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
consoleDebugSpy.mockRestore();
|
||||
consoleInfoSpy.mockRestore();
|
||||
consoleWarnSpy.mockRestore();
|
||||
consoleErrorSpy.mockRestore();
|
||||
consoleLogSpy.mockRestore();
|
||||
});
|
||||
|
||||
it('should call console.debug with the correct arguments when debug is called', () => {
|
||||
const message = 'Debug message';
|
||||
const context = { key: 'value' };
|
||||
logger.debug(message, context);
|
||||
expect(consoleDebugSpy).toHaveBeenCalledTimes(1);
|
||||
expect(consoleDebugSpy).toHaveBeenCalledWith(message, context);
|
||||
});
|
||||
|
||||
it('should call console.info with the correct arguments when info is called', () => {
|
||||
const message = 'Info message';
|
||||
const context = { key: 'value' };
|
||||
logger.info(message, context);
|
||||
expect(consoleInfoSpy).toHaveBeenCalledTimes(1);
|
||||
expect(consoleInfoSpy).toHaveBeenCalledWith(message, context);
|
||||
});
|
||||
|
||||
it('should call console.warn with the correct arguments when warn is called', () => {
|
||||
const message = 'Warn message';
|
||||
const context = { key: 'value' };
|
||||
logger.warn(message, context);
|
||||
expect(consoleWarnSpy).toHaveBeenCalledTimes(1);
|
||||
expect(consoleWarnSpy).toHaveBeenCalledWith(message, context);
|
||||
});
|
||||
|
||||
it('should call console.error with the correct arguments when error is called', () => {
|
||||
const message = 'Error message';
|
||||
const error = new Error('Something went wrong');
|
||||
const context = { key: 'value' };
|
||||
logger.error(message, error, context);
|
||||
expect(consoleErrorSpy).toHaveBeenCalledTimes(1);
|
||||
expect(consoleErrorSpy).toHaveBeenCalledWith(message, error, context);
|
||||
});
|
||||
|
||||
it('should call console.log with the correct arguments when log is called', () => {
|
||||
const message = 'Log message';
|
||||
const context = { key: 'value' };
|
||||
logger.log(message, context);
|
||||
expect(consoleLogSpy).toHaveBeenCalledTimes(1);
|
||||
expect(consoleLogSpy).toHaveBeenCalledWith(message, context);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user