-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathlogger.test.ts
100 lines (80 loc) · 3 KB
/
logger.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { createLogger } from "../../src/logger";
describe("Logger", () => {
const consoleErrorSpy = jest.spyOn(console, "error").mockImplementation(() => undefined);
const consoleInfoSpy = jest.spyOn(console, "info").mockImplementation(() => undefined);
const consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation(() => undefined);
const consoleDebugSpy = jest.spyOn(console, "debug").mockImplementation(() => undefined);
afterEach(() => {
consoleErrorSpy.mockReset();
consoleInfoSpy.mockReset();
consoleWarnSpy.mockReset();
consoleDebugSpy.mockReset();
});
it.each([
["info", "Info", consoleInfoSpy],
["warn", "Warning", consoleWarnSpy],
["error", "Error", consoleErrorSpy],
] as const)(".%s() should log correctly", (loggerMethod, logLevel, consoleSpy) => {
const prefix = "[some-prefix]";
const logger = createLogger({ prefix, silent: false, debug: true });
logger[loggerMethod]("Hey!");
expect(consoleSpy).toHaveBeenCalledWith(`[some-prefix] ${logLevel}: Hey!`);
});
it.each([
["info", "Info", consoleInfoSpy],
["warn", "Warning", consoleWarnSpy],
["error", "Error", consoleErrorSpy],
] as const)(
".%s() should log multiple params correctly",
(loggerMethod, logLevel, consoleSpy) => {
const prefix = "[some-prefix]";
const logger = createLogger({ prefix, silent: false, debug: true });
logger[loggerMethod]("Hey!", "this", "is", "a test with", 5, "params");
expect(consoleSpy).toHaveBeenCalledWith(
`[some-prefix] ${logLevel}: Hey!`,
"this",
"is",
"a test with",
5,
"params"
);
}
);
it(".debug() should log correctly", () => {
const prefix = "[some-prefix]";
const logger = createLogger({ prefix, silent: false, debug: true });
logger.debug("Hey!");
expect(consoleDebugSpy).toHaveBeenCalledWith(`[some-prefix] Debug: Hey!`);
});
it(".debug() should log multiple params correctly", () => {
const prefix = "[some-prefix]";
const logger = createLogger({ prefix, silent: false, debug: true });
logger.debug("Hey!", "this", "is", "a test with", 5, "params");
expect(consoleDebugSpy).toHaveBeenCalledWith(
`[some-prefix] Debug: Hey!`,
"this",
"is",
"a test with",
5,
"params"
);
});
describe("doesn't log when `silent` option is `true`", () => {
it.each([
["info", consoleInfoSpy],
["warn", consoleWarnSpy],
["error", consoleErrorSpy],
] as const)(".%s()", (loggerMethod, consoleSpy) => {
const prefix = "[some-prefix]";
const logger = createLogger({ prefix, silent: true, debug: true });
logger[loggerMethod]("Hey!");
expect(consoleSpy).not.toHaveBeenCalled();
});
});
it(".debug() doesn't log when `silent` option is `true`", () => {
const prefix = "[some-prefix]";
const logger = createLogger({ prefix, silent: true, debug: true });
logger.debug("Hey!");
expect(consoleDebugSpy).not.toHaveBeenCalled();
});
});