-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.test.js
72 lines (63 loc) · 1.5 KB
/
logger.test.js
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
import {setTimeout} from 'node:timers/promises'
import test from 'ava'
import Logger from './logger.js'
test('logging', async (t) => {
const logger = new Logger()
const events = []
const lines = []
logger.on('log', (event, line) => {
events.push(event)
lines.push(line)
})
logger.log({
level: 'INFO',
topic: 'Test',
message: 'Hello World!',
})
t.like(events[0], {
level: 'INFO',
topic: 'Test',
message: 'Hello World!',
})
t.true(lines[0].includes('[INFO] [Test] Hello World!'))
t.true(Date.now() - events[0].time < 100)
const [, timestamp] = lines[0].match(/^\[(.*?)]/)
t.true(Date.now() - Date.parse(timestamp) < 100)
const finish = logger.measure({
level: 'INFO',
topic: 'HTTP',
message: 'GET /',
})
t.like(events[1], {
level: 'INFO',
topic: 'HTTP',
message: 'GET /',
})
t.true(lines[1].includes('[INFO] [HTTP] GET /'))
await setTimeout(100)
finish({
message: '200',
})
t.like(events[2], {
level: 'INFO',
topic: 'HTTP',
message: 'GET / › 200',
})
t.true(events[2].duration > 100)
t.true(events[2].duration < 150)
t.true(lines[2].includes('[INFO] [HTTP] GET / › 200'))
const error = new Error('Something went wrong')
logger.log({
level: 'ERROR',
topic: 'Test',
message: 'An error happened',
error,
})
t.like(events[3], {
level: 'ERROR',
topic: 'Test',
message: 'An error happened',
error,
})
t.true(lines[3].includes('Error: Something went wrong'))
})