/
timestamp.test.js
101 lines (91 loc) 路 3.15 KB
/
timestamp.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
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
'use strict'
const { test } = require('tap')
const { sink, once } = require('./helper')
const pino = require('../')
test('pino exposes standard time functions', async ({ ok }) => {
ok(pino.stdTimeFunctions)
ok(pino.stdTimeFunctions.epochTime)
ok(pino.stdTimeFunctions.unixTime)
ok(pino.stdTimeFunctions.nullTime)
})
test('pino accepts external time functions', async ({ is }) => {
const opts = {
timestamp: () => ',"time":"none"'
}
const stream = sink()
const instance = pino(opts, stream)
instance.info('foobar')
const result = await once(stream, 'data')
is(result.hasOwnProperty('time'), true)
is(result.time, 'none')
})
test('pino accepts external time functions with custom label', async ({ is }) => {
const opts = {
timestamp: () => ',"custom-time-label":"none"'
}
const stream = sink()
const instance = pino(opts, stream)
instance.info('foobar')
const result = await once(stream, 'data')
is(result.hasOwnProperty('custom-time-label'), true)
is(result['custom-time-label'], 'none')
})
test('inserts timestamp by default', async ({ ok, is }) => {
const stream = sink()
const instance = pino(stream)
instance.info('foobar')
const result = await once(stream, 'data')
is(result.hasOwnProperty('time'), true)
ok(new Date(result.time) <= new Date(), 'time is greater than timestamp')
is(result.msg, 'foobar')
})
test('omits timestamp when timestamp option is false', async ({ is }) => {
const stream = sink()
const instance = pino({ timestamp: false }, stream)
instance.info('foobar')
const result = await once(stream, 'data')
is(result.hasOwnProperty('time'), false)
is(result.msg, 'foobar')
})
test('inserts timestamp when timestamp option is true', async ({ ok, is }) => {
const stream = sink()
const instance = pino({ timestamp: true }, stream)
instance.info('foobar')
const result = await once(stream, 'data')
is(result.hasOwnProperty('time'), true)
ok(new Date(result.time) <= new Date(), 'time is greater than timestamp')
is(result.msg, 'foobar')
})
test('child inserts timestamp by default', async ({ ok, is }) => {
const stream = sink()
const logger = pino(stream)
const instance = logger.child({ component: 'child' })
instance.info('foobar')
const result = await once(stream, 'data')
is(result.hasOwnProperty('time'), true)
ok(new Date(result.time) <= new Date(), 'time is greater than timestamp')
is(result.msg, 'foobar')
})
test('child omits timestamp with option', async ({ is }) => {
const stream = sink()
const logger = pino({ timestamp: false }, stream)
const instance = logger.child({ component: 'child' })
instance.info('foobar')
const result = await once(stream, 'data')
is(result.hasOwnProperty('time'), false)
is(result.msg, 'foobar')
})
test('pino.stdTimeFunctions.unixTime returns seconds based timestamps', async ({ is }) => {
const opts = {
timestamp: pino.stdTimeFunctions.unixTime
}
const stream = sink()
const instance = pino(opts, stream)
const now = Date.now
Date.now = () => 1531069919686
instance.info('foobar')
const result = await once(stream, 'data')
is(result.hasOwnProperty('time'), true)
is(result.time, 1531069920)
Date.now = now
})