Skip to content

Commit

Permalink
test: refactor logger tests for consistency and readability
Browse files Browse the repository at this point in the history
  • Loading branch information
drazisil committed Dec 16, 2024
1 parent 18e1ae0 commit 22cc8c1
Showing 1 changed file with 63 additions and 65 deletions.
128 changes: 63 additions & 65 deletions utilities/logger/Logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,80 +2,78 @@ import { describe, it, expect, beforeEach, vi } from "vitest";
import { logger } from "./Logger.js";

const enum LogLevel {
debug = 'debug',
info = 'info',
warn = 'warn',
error = 'error',
fatal = 'fatal',
trace = 'trace',
}
debug = "debug",
info = "info",
warn = "warn",
error = "error",
fatal = "fatal",
trace = "trace",
}

describe("Logger", () => {
describe("logging methods", () => {
let logOutput: any;
beforeEach(() => {
logOutput = [];
// Mock the underlying logger to capture output
vi.spyOn(logger, 'info').mockImplementation((msg) => {
logOutput.push(msg);
});
});
describe("logging methods", () => {
let logOutput: any;
beforeEach(() => {
logOutput = [];
// Mock the underlying logger to capture output
vi.spyOn(logger, "info").mockImplementation((msg) => {
logOutput.push(msg);
});
});

["debug", "info", "warn", "error", "fatal", "trace"].forEach((level) => {
it(`should have ${level} method`, () => {
expect(logger[level as LogLevel]).toBeInstanceOf(Function);
});

it(`should log ${level} messages`, () => {
const log = logger.child({ name: "test" });
expect(() => log[level as LogLevel]("test message")).not.toThrow();
});
["debug", "info", "warn", "error", "fatal", "trace"].forEach((level) => {
it(`should have ${level} method`, () => {
expect(logger[level as LogLevel]).toBeInstanceOf(Function);
});

it(`should handle empty messages for ${level}`, () => {
expect(() => logger[level as LogLevel]("")).not.toThrow();
});
it(`should log ${level} messages`, () => {
const log = logger.child({ name: "test" });
expect(() => log[level as LogLevel]("test message")).not.toThrow();
});

it(`should handle object logging for ${level}`, () => {
expect(() => logger[level as LogLevel]({ test: 'value' })).not.toThrow();
});
it(`should handle empty messages for ${level}`, () => {
expect(() => logger[level as LogLevel]("")).not.toThrow();
});

it(`should handle multiple arguments for ${level}`, () => {
expect(() => logger[level as LogLevel]("msg", { data: 1 })).not.toThrow();
});
});
});
it(`should handle object logging for ${level}`, () => {
expect(() =>
logger[level as LogLevel]({ test: "value" }),
).not.toThrow();
});

describe("child loggers", () => {
it("should create child logger with custom name", () => {
const childLogger = logger.child({ name: "custom" });
expect(childLogger).toHaveProperty("info");
expect(childLogger).toHaveProperty("debug");
expect(childLogger).toHaveProperty("warn");
expect(childLogger).toHaveProperty("error");
});
it(`should handle multiple arguments for ${level}`, () => {
expect(() =>
logger[level as LogLevel]("msg", { data: 1 }),
).not.toThrow();
});
});
});

it("should create child logger with custom level", () => {
const childLogger = logger.child({ name: "custom", level: "error" });
expect(childLogger).toHaveProperty("error");
});
describe("child loggers", () => {
it("should create child logger with custom name", () => {
const childLogger = logger.child({ name: "custom" });
expect(childLogger).toHaveProperty("info");
expect(childLogger).toHaveProperty("debug");
expect(childLogger).toHaveProperty("warn");
expect(childLogger).toHaveProperty("error");
});

it("should inherit parent logger methods", () => {
const childLogger = logger.child({ name: "custom" });
expect(() => childLogger.info("test")).not.toThrow();
expect(() => childLogger.error("test")).not.toThrow();
});
it("should create child logger with custom level", () => {
const childLogger = logger.child({ name: "custom", level: "error" });
expect(childLogger).toHaveProperty("error");
});

it("should throw for invalid level", () => {
expect(() =>
logger.child({ name: "custom", level: "invalid" as any })
).toThrow();
});
});
it("should inherit parent logger methods", () => {
const childLogger = logger.child({ name: "custom" });
expect(() => childLogger.info("test")).not.toThrow();
expect(() => childLogger.error("test")).not.toThrow();
});
});

describe("error handling", () => {
it("should handle Error objects", () => {
const error = new Error("test error");
expect(() => logger.error(error.message)).not.toThrow();
});
});
describe("error handling", () => {
it("should handle Error objects", () => {
const error = new Error("test error");
expect(() => logger.error(error.message)).not.toThrow();
});
});
});

0 comments on commit 22cc8c1

Please sign in to comment.