|
| 1 | +import { mock } from "jest-mock-extended"; |
| 2 | + |
1 | 3 | import * as lib from "./index"; |
| 4 | +import { SemanticLogger } from "./index"; |
| 5 | + |
| 6 | +describe("logging module", () => { |
| 7 | + describe("public API", () => { |
| 8 | + it("should export LogService", () => { |
| 9 | + expect(lib.LogService).toBeDefined(); |
| 10 | + }); |
| 11 | + |
| 12 | + it("should export LogLevel", () => { |
| 13 | + expect(lib.LogLevel).toBeDefined(); |
| 14 | + }); |
| 15 | + |
| 16 | + it("should export ConsoleLogService", () => { |
| 17 | + expect(lib.ConsoleLogService).toBeDefined(); |
| 18 | + }); |
| 19 | + }); |
| 20 | + |
| 21 | + describe("SemanticLogger", () => { |
| 22 | + let logger: SemanticLogger; |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + logger = mock<SemanticLogger>(); |
| 26 | + }); |
| 27 | + |
| 28 | + describe("logging methods", () => { |
| 29 | + it("should accept a message string", () => { |
| 30 | + logger.debug("debug message"); |
| 31 | + logger.info("info message"); |
| 32 | + logger.warn("warn message"); |
| 33 | + logger.error("error message"); |
| 34 | + |
| 35 | + expect(logger.debug).toHaveBeenCalledWith("debug message"); |
| 36 | + expect(logger.info).toHaveBeenCalledWith("info message"); |
| 37 | + expect(logger.warn).toHaveBeenCalledWith("warn message"); |
| 38 | + expect(logger.error).toHaveBeenCalledWith("error message"); |
| 39 | + }); |
| 40 | + |
| 41 | + it("should accept content object and optional message", () => { |
| 42 | + logger.debug({ step: 1 }, "processing step"); |
| 43 | + logger.info({ count: 42 }, "items processed"); |
| 44 | + logger.warn({ threshold: 100 }, "approaching limit"); |
| 45 | + logger.error({ code: 500 }, "server error"); |
| 46 | + |
| 47 | + expect(logger.debug).toHaveBeenCalledWith({ step: 1 }, "processing step"); |
| 48 | + expect(logger.info).toHaveBeenCalledWith({ count: 42 }, "items processed"); |
| 49 | + expect(logger.warn).toHaveBeenCalledWith({ threshold: 100 }, "approaching limit"); |
| 50 | + expect(logger.error).toHaveBeenCalledWith({ code: 500 }, "server error"); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + describe("panic", () => { |
| 55 | + beforeEach(() => { |
| 56 | + logger.panic = jest.fn((content: any, msg?: string) => { |
| 57 | + const errorMsg = msg || (typeof content === "string" ? content : "panic"); |
| 58 | + throw new Error(errorMsg); |
| 59 | + }) as any; |
| 60 | + }); |
| 61 | + |
| 62 | + it("should throw when called with a message", () => { |
| 63 | + expect(() => logger.panic("critical error")).toThrow("critical error"); |
| 64 | + }); |
2 | 65 |
|
3 | | -describe("logging", () => { |
4 | | - // This test will fail until something is exported from index.ts |
5 | | - it("should work", () => { |
6 | | - expect(lib).toBeDefined(); |
| 66 | + it("should throw when called with content and message", () => { |
| 67 | + expect(() => logger.panic({ reason: "invalid state" }, "system panic")).toThrow( |
| 68 | + "system panic", |
| 69 | + ); |
| 70 | + }); |
| 71 | + }); |
7 | 72 | }); |
8 | 73 | }); |
0 commit comments