-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.spec.ts
131 lines (96 loc) · 2.43 KB
/
log.spec.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
import { afterEach, expect, test } from 'bun:test';
import { InfluxWriteStream } from './influx';
import logger, { assert, todo } from './log.js';
afterEach(() => {
process.env.JS_LOG = 'debug';
globalThis.JS_LOG = process.env.JS_LOG;
});
test('logging', () => {
const log = logger().prefix('Test').trace();
log.info('Hello, world!');
log.error('This is an error!');
});
test('no prefix', () => {
const log = logger();
log.warn('This has no prefix!');
});
test('js objects', () => {
const log = logger().prefix('Test');
log.info('event', new Event('test'));
});
test('boolean', () => {
const log = logger().prefix('Test');
log.info('bool', true);
log.info('bool', 'value', false);
});
test('arguments', () => {
const log = logger().trace();
log.warn('Text here', 'one', 2, 'hello', 'world');
log.warn('Text here', 'obj', { one: 2 });
});
test('invalid arguments', () => {
const log = logger().trace();
log.warn('Text here', 'and another text here');
log.warn('Text here', { one: 2 });
});
test('json', () => {
const logJson = logger().prefix('Test').json().trace();
logJson.info('Hello, world!');
});
test('influx', () => {
const log = logger()
.prefix('Influx')
.time(false)
.pipeTo(
new InfluxWriteStream({
org: 'test',
bucket: 'test',
db: 'test',
url: 'https://test',
token: 'test',
})
);
log.info('Hello, world!');
});
test('log format', () => {
const log = logger().trace().time(false);
const str = log.sprint('error', 'Hello, world!');
const expected =
'\u001B[31mERROR\u001B[0m \u001B[90m<log.spec.ts:47:47>\u001B[0m Hello, world!';
expect(str).toBe(expected);
});
test('log errors', () => {
const log = logger().trace();
log.error('testing', 'err', new SyntaxError('Test error'));
});
test('log level env', () => {
process.env.JS_LOG = 'error';
const log = logger().trace();
// @ts-ignore
const print = log.checkLevel('info');
expect(print).toBe(false);
});
test('log level globalThis', () => {
process.env.JS_LOG = undefined;
globalThis.JS_LOG = 'error';
const log = logger().trace();
// @ts-ignore
const print = log.checkLevel('info');
expect(print).toBe(false);
});
test('assertion', () => {
try {
assert(false, "This shouldn't be true");
} catch (err) {
return;
}
throw new Error('Assertion failed to throw');
});
test('todo', () => {
try {
todo('There is a TODO here');
} catch (err) {
return;
}
throw new Error('Assertion failed to throw');
});