|
1 | | -import {describe, it, expect} from "vitest" |
| 1 | +import { describe, it, expect, beforeEach, afterEach } from "vitest" |
| 2 | +import debug from "../src" |
2 | 3 |
|
3 | | -import debug from "../src"; |
| 4 | +describe("debug", () => { |
| 5 | + // Mock console.log |
| 6 | + const originalConsoleLog = console.log |
| 7 | + let consoleLogCalls: any[][] = [] |
4 | 8 |
|
5 | | -describe("Debug", () => { |
6 | | - it("should be defined", () => { |
7 | | - expect(debug).toBeDefined(); |
8 | | - }); |
9 | | -}); |
| 9 | + beforeEach(() => { |
| 10 | + // Reset mock calls before each test |
| 11 | + consoleLogCalls = [] |
| 12 | + console.log = (...args: any[]) => { |
| 13 | + consoleLogCalls.push(args) |
| 14 | + return originalConsoleLog(...args) |
| 15 | + } |
| 16 | + }) |
| 17 | + |
| 18 | + afterEach(() => { |
| 19 | + // Restore original console.log |
| 20 | + console.log = originalConsoleLog |
| 21 | + // Clear process.env.DEBUG |
| 22 | + delete process.env.DEBUG |
| 23 | + }) |
| 24 | + |
| 25 | + it("should log only if we add DEBUG={namespace} as env var", () => { |
| 26 | + // Test when DEBUG equals the namespace |
| 27 | + process.env.DEBUG = "test-namespace" |
| 28 | + const testDebug = debug("test-namespace") |
| 29 | + testDebug("Test message") |
| 30 | + expect(consoleLogCalls.length).toBe(1) |
| 31 | + expect(consoleLogCalls[0][1]).toBe("Test message") |
| 32 | + |
| 33 | + // Test when DEBUG equals * |
| 34 | + process.env.DEBUG = "*" |
| 35 | + const testDebugWildcard = debug("any-namespace") |
| 36 | + testDebugWildcard("Wildcard test") |
| 37 | + expect(consoleLogCalls.length).toBe(2) |
| 38 | + expect(consoleLogCalls[1][1]).toBe("Wildcard test") |
| 39 | + |
| 40 | + // Test when DEBUG uses wildcard prefix (namespace:*) |
| 41 | + process.env.DEBUG = "prefix:*" |
| 42 | + const testDebugPrefix = debug("prefix:something") |
| 43 | + testDebugPrefix("Prefix test") |
| 44 | + expect(consoleLogCalls.length).toBe(3) |
| 45 | + expect(consoleLogCalls[2][1]).toBe("Prefix test") |
| 46 | + |
| 47 | + // Test when DEBUG doesn't match |
| 48 | + process.env.DEBUG = "different-namespace" |
| 49 | + const testNoDebug = debug("test-namespace") |
| 50 | + testNoDebug("Should not log") |
| 51 | + expect(consoleLogCalls.length).toBe(3) // Count shouldn't increase |
| 52 | + }) |
| 53 | + |
| 54 | + it("should log only logs from a spectific namespace", () => { |
| 55 | + // Create multiple debug loggers with different namespaces |
| 56 | + const debug1 = debug("namespace1") |
| 57 | + const debug2 = debug("namespace2") |
| 58 | + const debug3 = debug("namespace3") |
| 59 | + |
| 60 | + // Set DEBUG to only match one namespace |
| 61 | + process.env.DEBUG = "namespace2" |
| 62 | + |
| 63 | + // Call all loggers |
| 64 | + debug1("Message from namespace1") |
| 65 | + debug2("Message from namespace2") |
| 66 | + debug3("Message from namespace3") |
| 67 | + |
| 68 | + // Verify only namespace2 logged a message |
| 69 | + expect(consoleLogCalls.length).toBe(1) |
| 70 | + expect(consoleLogCalls[0][1]).toBe("Message from namespace2") |
| 71 | + }) |
| 72 | + |
| 73 | + it("should not handle undefined namespace", () => { |
| 74 | + process.env.DEBUG = "*" |
| 75 | + const testDebug = debug(undefined) |
| 76 | + testDebug("Test with undefined namespace") |
| 77 | + expect(consoleLogCalls.length).toBe(1) |
| 78 | + }) |
| 79 | + |
| 80 | + it("should log multiple arguments correctly", () => { |
| 81 | + process.env.DEBUG = "test-namespace" |
| 82 | + const testDebug = debug("test-namespace") |
| 83 | + testDebug("First argument", "Second argument", { key: "value" }, 123) |
| 84 | + expect(consoleLogCalls.length).toBe(1) |
| 85 | + expect(consoleLogCalls[0][1]).toBe("First argument") |
| 86 | + expect(consoleLogCalls[0][2]).toBe("Second argument") |
| 87 | + expect(consoleLogCalls[0][3]).toEqual({ key: "value" }) |
| 88 | + expect(consoleLogCalls[0][4]).toBe(123) |
| 89 | + }) |
| 90 | + |
| 91 | + it("should not log when DEBUG is not set", () => { |
| 92 | + // DEBUG is already deleted in afterEach |
| 93 | + const testDebug = debug("test-namespace") |
| 94 | + testDebug("Should not log") |
| 95 | + expect(consoleLogCalls.length).toBe(0) |
| 96 | + }) |
| 97 | +}) |
0 commit comments