UNPKG

kliedz

Version:

Dead-simple, stateless logging utility for JavaScript and TypeScript. Pure functions. No dependencies. Just log.

86 lines (67 loc) 2.27 kB
import { colorFormatter, plainFormatter } from "../../src/logger/formatters.js"; import * as coreMod from "../../src/logger/log_core.js"; import * as logger from "../../src/logger/loggers.js"; import type { LogParams } from "../../src/types/log_params.js"; import { afterEach, beforeEach, describe, expect, it, vi } from "../setup.js"; const userParams: LogParams = { level: "warn", threshold: "debug" }; const msgArgs = ["hello", "world"]; let coreSpy!: ReturnType<typeof vi.spyOn>; beforeEach(() => { coreSpy = vi.spyOn(coreMod, "logCore").mockImplementation(() => {}); }); afterEach(() => vi.restoreAllMocks()); describe("createLogger - allows to create custom loggers", () => { it("creates a logger with custom formatter", () => { const params = { level: "info", threshold: "debug" }; const customFormatter = vi.fn(() => "[CUSTOM] hello"); const customLogger = logger.createLogger(customFormatter); customLogger(params, "hello"); expect(coreSpy).toHaveBeenCalledWith(params, customFormatter, "hello"); customLogger("foo", 42); expect(coreSpy).toHaveBeenCalledWith( { level: "info", threshold: "info" }, customFormatter, "foo", 42, ); }); }); describe("logWithColor – overload resolution", () => { it("delegates to logCore with DEFAULT params when no LogParams given", () => { logger.logWithColor("quick", "msg"); expect(coreSpy).toHaveBeenCalledTimes(1); expect(coreSpy).toHaveBeenCalledWith( { level: "info", threshold: "info" }, colorFormatter, "quick", "msg", ); }); it("uses caller-supplied LogParams when provided", () => { logger.logWithColor(userParams, ...msgArgs); expect(coreSpy).toHaveBeenCalledWith( userParams, colorFormatter, ...msgArgs, ); }); }); describe("logWithLevel – overload resolution", () => { it("delegates with defaults when first arg is NOT LogParams", () => { logger.logWithLevel("just", "text"); expect(coreSpy).toHaveBeenCalledWith( { level: "info", threshold: "info" }, plainFormatter, "just", "text", ); }); it("forwards custom LogParams correctly", () => { logger.logWithLevel({ level: "warn" }, ...msgArgs); expect(coreSpy).toHaveBeenCalledWith( { level: "warn" }, plainFormatter, ...msgArgs, ); }); });