-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_test.ts
159 lines (143 loc) · 3.69 KB
/
mod_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import {
assert,
assertEquals,
} from "https://deno.land/std@0.51.0/testing/asserts.ts";
import { Spy, spy } from "https://deno.land/x/mock@v0.3.0/spy.ts";
import { createLogger, textFormat, jsonFormat } from "./mod.ts";
import { SinkFunction, LogEntry, LogLevel } from "./types.ts";
Deno.test("addSink", () => {
const logger = createLogger({ outputFormat: "[{level}] {message}" });
const sink: Spy<void> = spy();
logger.addSink(sink);
logger.info("foo");
// The sink has been run once
assertEquals(sink.calls, [
{
args: [{
level: LogLevel.INFO,
format: "foo",
message: "foo",
formattedMessage: "[INFO] foo",
variables: {},
}],
self: { sinkFunc: sink, outputFormat: "[{level}] {message}" },
},
]);
});
Deno.test("addSink with overriding format", () => {
const logger = createLogger({ outputFormat: "[{level}] {message}" });
const sink: Spy<void> = spy();
logger.addSink(sink);
logger.addSink(sink, "{message} [{level}]");
logger.info("foo");
// The sink has been run once
assertEquals(sink.calls, [
{
args: [{
level: LogLevel.INFO,
format: "foo",
message: "foo",
formattedMessage: "[INFO] foo",
variables: {},
}],
self: { sinkFunc: sink, outputFormat: "[{level}] {message}" },
},
{
args: [{
level: LogLevel.INFO,
format: "foo",
message: "foo",
formattedMessage: "foo [INFO]",
variables: {},
}],
self: { sinkFunc: sink, outputFormat: "{message} [{level}]" },
},
]);
});
Deno.test("minimumLevel", () => {
const logger = createLogger(
{ minimumLevel: LogLevel.INFO, outputFormat: "[{level}] {message}" },
);
const sink: Spy<void> = spy();
logger.addSink(sink);
logger.debug("foo");
logger.info("foo");
// The sink has been run once
assertEquals(sink.calls, [
{
args: [{
level: LogLevel.INFO,
format: "foo",
message: "foo",
formattedMessage: "[INFO] foo",
variables: {},
}],
self: { sinkFunc: sink, outputFormat: "[{level}] {message}" },
},
]);
});
Deno.test("textFormat", () => {
const logger = createLogger({ outputFormat: textFormat });
const sink: Spy<void> = spy();
const NativeDate = globalThis.Date;
class FakeDate {
toISOString() {
return "0000-00-00T-00:00:00.0000Z";
}
}
globalThis.Date = FakeDate as DateConstructor;
//
// Date is now mocked
//
logger.addSink(sink);
logger.info("foo");
//
// Date is now restored
//
globalThis.Date = NativeDate;
assertEquals(sink.calls, [
{
args: [{
level: LogLevel.INFO,
format: "foo",
message: "foo",
formattedMessage: "[0000-00-00T-00:00:00.0000Z INFO] foo",
variables: {},
}],
self: { sinkFunc: sink, outputFormat: textFormat },
},
]);
});
Deno.test("jsonFormat", () => {
const logger = createLogger({ outputFormat: jsonFormat });
const sink: Spy<void> = spy();
const NativeDate = globalThis.Date;
class FakeDate {
toISOString() {
return "0000-00-00T-00:00:00.0000Z";
}
}
globalThis.Date = FakeDate as DateConstructor;
//
// Date is now mocked
//
logger.addSink(sink);
logger.info("{foo}", "bar");
//
// Date is now restored
//
globalThis.Date = NativeDate;
assertEquals(sink.calls, [
{
args: [{
level: LogLevel.INFO,
format: "{foo}",
message: "bar",
formattedMessage:
'{"message":"bar","level":"INFO","timestamp":"0000-00-00T-00:00:00.0000Z","data":{"foo":"bar"}}',
variables: { "foo": "bar" },
}],
self: { sinkFunc: sink, outputFormat: jsonFormat },
},
]);
});