generated from kawarimidoll/deno-dev-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.ts
140 lines (127 loc) · 3.57 KB
/
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { assertEquals } from "@std/assert";
import { Log, LogLevel } from "./mod.ts";
const levels = ["debug", "info", "warn", "error"] as LogLevel[];
const date = new Date(2020, 9, 6, 8, 2, 45);
function generateLogPrefixes(log: Log) {
// deno-lint-ignore no-explicit-any
return levels.map((level) => (log as any).prefix(date, level));
}
Deno.test("normal", () => {
assertEquals(
generateLogPrefixes(new Log()),
[
["\x1b[0m2020-10-06T08:02:45+09:00 ✔\x1b[0m"],
["\x1b[34m2020-10-06T08:02:45+09:00 ℹ\x1b[39m"],
["\x1b[33m2020-10-06T08:02:45+09:00 ⚠\x1b[39m"],
["\x1b[31m2020-10-06T08:02:45+09:00 ✖\x1b[39m"],
],
);
});
Deno.test("no color", () => {
assertEquals(
generateLogPrefixes(new Log({ color: false })),
[
["2020-10-06T08:02:45+09:00 ✔"],
["2020-10-06T08:02:45+09:00 ℹ"],
["2020-10-06T08:02:45+09:00 ⚠"],
["2020-10-06T08:02:45+09:00 ✖"],
],
);
});
Deno.test("no levels", () => {
assertEquals(
generateLogPrefixes(new Log({ levelIndicator: "none" })),
[
["\x1b[0m2020-10-06T08:02:45+09:00\x1b[0m"],
["\x1b[34m2020-10-06T08:02:45+09:00\x1b[39m"],
["\x1b[33m2020-10-06T08:02:45+09:00\x1b[39m"],
["\x1b[31m2020-10-06T08:02:45+09:00\x1b[39m"],
],
);
});
Deno.test("initial", () => {
assertEquals(
generateLogPrefixes(new Log({ levelIndicator: "initial" })),
[
["\x1b[0m2020-10-06T08:02:45+09:00 D\x1b[0m"],
["\x1b[34m2020-10-06T08:02:45+09:00 I\x1b[39m"],
["\x1b[33m2020-10-06T08:02:45+09:00 W\x1b[39m"],
["\x1b[31m2020-10-06T08:02:45+09:00 E\x1b[39m"],
],
);
});
Deno.test("full", () => {
assertEquals(
generateLogPrefixes(new Log({ levelIndicator: "full" })),
[
["\x1b[0m2020-10-06T08:02:45+09:00 DEBUG\x1b[0m"],
["\x1b[34m2020-10-06T08:02:45+09:00 INFO \x1b[39m"],
["\x1b[33m2020-10-06T08:02:45+09:00 WARN \x1b[39m"],
["\x1b[31m2020-10-06T08:02:45+09:00 ERROR\x1b[39m"],
],
);
});
Deno.test("change datetime format", () => {
assertEquals(
generateLogPrefixes(new Log({ datetimeFormat: "MMM, d, YYYY, h:m a" })),
[
["\x1b[0mOct, 6, 2020, 8:2 AM ✔\x1b[0m"],
["\x1b[34mOct, 6, 2020, 8:2 AM ℹ\x1b[39m"],
["\x1b[33mOct, 6, 2020, 8:2 AM ⚠\x1b[39m"],
["\x1b[31mOct, 6, 2020, 8:2 AM ✖\x1b[39m"],
],
);
});
Deno.test("no timestamp", () => {
assertEquals(
generateLogPrefixes(new Log({ datetimeFormat: "" })),
[
["\x1b[0m✔\x1b[0m"],
["\x1b[34mℹ\x1b[39m"],
["\x1b[33m⚠\x1b[39m"],
["\x1b[31m✖\x1b[39m"],
],
);
});
function collectConsoleArgs(log: Log) {
const result: unknown[] = [];
levels.forEach((level) => {
// stub console
console[level] = (...args: unknown[]) => {
result.push(args);
};
log[level](level);
});
return result;
}
Deno.test("add newline", () => {
assertEquals(
collectConsoleArgs(new Log({ datetimeFormat: "", addNewLine: true })),
[
["\x1b[0m✔\x1b[0m", "debug", "\n"],
["\x1b[34mℹ\x1b[39m", "info", "\n"],
["\x1b[33m⚠\x1b[39m", "warn", "\n"],
["\x1b[31m✖\x1b[39m", "error", "\n"],
],
);
});
Deno.test("minimum level", () => {
assertEquals(
collectConsoleArgs(new Log({ datetimeFormat: "", minLogLevel: "warn" })),
[
["\x1b[33m⚠\x1b[39m", "warn"],
["\x1b[31m✖\x1b[39m", "error"],
],
);
});
Deno.test("no prefix", () => {
assertEquals(
collectConsoleArgs(new Log({ datetimeFormat: "", levelIndicator: "none" })),
[
["debug"],
["info"],
["warn"],
["error"],
],
);
});