Skip to content

Commit f5fdad5

Browse files
committed
Create unit tests
1 parent 4a12cf3 commit f5fdad5

2 files changed

Lines changed: 104 additions & 13 deletions

File tree

src/helpers.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
/**
22
* Check if is browser env
33
*/
4-
export const isBrowser: boolean = !!(typeof window != "undefined" && window.document)
4+
export const isBrowser: boolean = !!(
5+
typeof window != "undefined" && window.document
6+
)
57

68
/**
79
* Transform string to RGB
810
* @param str
911
*/
10-
export function stringToRgb(str: string):[number, number, number] {
12+
export function stringToRgb(str: string): [number, number, number] {
13+
if (!str) return [128, 128, 128]
1114
let hash = 0
1215
for (let i = 0; i < str.length; i++) {
1316
let character = str.charCodeAt(i)
14-
hash = ((hash << 5) - hash) + character
17+
hash = (hash << 5) - hash + character
1518
hash = Math.abs(hash & hash)
1619
}
17-
const r = (hash & 0xFF0000) >> 16
18-
const g = (hash & 0x00FF00) >> 8
19-
const b = hash & 0x0000FF
20+
const r = (hash & 0xff0000) >> 16
21+
const g = (hash & 0x00ff00) >> 8
22+
const b = hash & 0x0000ff
2023
return [r, g, b]
2124
}

test/debug.test.ts

Lines changed: 95 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,97 @@
1-
import {describe, it, expect} from "vitest"
1+
import { describe, it, expect, beforeEach, afterEach } from "vitest"
2+
import debug from "../src"
23

3-
import debug from "../src";
4+
describe("debug", () => {
5+
// Mock console.log
6+
const originalConsoleLog = console.log
7+
let consoleLogCalls: any[][] = []
48

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

Comments
 (0)