Skip to content

Commit ebbd4a6

Browse files
authored
test: migrate logger instantiation to node test runner (#6061)
1 parent 2ce1ae1 commit ebbd4a6

File tree

1 file changed

+89
-96
lines changed

1 file changed

+89
-96
lines changed

test/logger/instantiation.test.js

Lines changed: 89 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const stream = require('node:stream')
44
const os = require('node:os')
55
const fs = require('node:fs')
66

7-
const t = require('tap')
7+
const t = require('node:test')
88
const split = require('split2')
99

1010
const { streamSym } = require('pino/lib/symbols')
@@ -14,10 +14,9 @@ const helper = require('../helper')
1414
const { FST_ERR_LOG_INVALID_LOGGER } = require('../../lib/errors')
1515
const { once, on } = stream
1616
const { createTempFile, request } = require('./logger-test-utils')
17+
const { partialDeepStrictEqual } = require('../toolkit')
1718

18-
t.test('logger instantiation', (t) => {
19-
t.setTimeout(60000)
20-
19+
t.test('logger instantiation', { timeout: 60000 }, async (t) => {
2120
let localhost
2221
let localhostForURL
2322

@@ -26,7 +25,7 @@ t.test('logger instantiation', (t) => {
2625
[localhost, localhostForURL] = await helper.getLoopbackHost()
2726
})
2827

29-
t.test('can use external logger instance', async (t) => {
28+
await t.test('can use external logger instance', async (t) => {
3029
const lines = [/^Server listening at /, /^incoming request$/, /^log success$/, /^request completed$/]
3130
t.plan(lines.length + 1)
3231

@@ -35,10 +34,10 @@ t.test('logger instantiation', (t) => {
3534
const loggerInstance = require('pino')(stream)
3635

3736
const fastify = Fastify({ loggerInstance })
38-
t.teardown(fastify.close.bind(fastify))
37+
t.after(() => fastify.close())
3938

4039
fastify.get('/foo', function (req, reply) {
41-
t.ok(req.log)
40+
t.assert.ok(req.log)
4241
req.log.info('log success')
4342
reply.send({ hello: 'world' })
4443
})
@@ -49,30 +48,30 @@ t.test('logger instantiation', (t) => {
4948

5049
for await (const [line] of on(stream, 'data')) {
5150
const regex = lines.shift()
52-
t.ok(regex.test(line.msg), '"' + line.msg + '" does not match "' + regex + '"')
51+
t.assert.ok(regex.test(line.msg), '"' + line.msg + '" does not match "' + regex + '"')
5352
if (lines.length === 0) break
5453
}
5554
})
5655

57-
t.test('should create a default logger if provided one is invalid', (t) => {
56+
await t.test('should create a default logger if provided one is invalid', (t) => {
5857
t.plan(8)
5958

6059
const logger = new Date()
6160

6261
const fastify = Fastify({ logger })
63-
t.teardown(fastify.close.bind(fastify))
64-
65-
t.equal(typeof fastify.log, 'object')
66-
t.equal(typeof fastify.log.fatal, 'function')
67-
t.equal(typeof fastify.log.error, 'function')
68-
t.equal(typeof fastify.log.warn, 'function')
69-
t.equal(typeof fastify.log.info, 'function')
70-
t.equal(typeof fastify.log.debug, 'function')
71-
t.equal(typeof fastify.log.trace, 'function')
72-
t.equal(typeof fastify.log.child, 'function')
62+
t.after(() => fastify.close())
63+
64+
t.assert.strictEqual(typeof fastify.log, 'object')
65+
t.assert.strictEqual(typeof fastify.log.fatal, 'function')
66+
t.assert.strictEqual(typeof fastify.log.error, 'function')
67+
t.assert.strictEqual(typeof fastify.log.warn, 'function')
68+
t.assert.strictEqual(typeof fastify.log.info, 'function')
69+
t.assert.strictEqual(typeof fastify.log.debug, 'function')
70+
t.assert.strictEqual(typeof fastify.log.trace, 'function')
71+
t.assert.strictEqual(typeof fastify.log.child, 'function')
7372
})
7473

75-
t.test('expose the logger', async (t) => {
74+
await t.test('expose the logger', async (t) => {
7675
t.plan(2)
7776
const stream = split(JSON.parse)
7877
const fastify = Fastify({
@@ -81,49 +80,45 @@ t.test('logger instantiation', (t) => {
8180
level: 'info'
8281
}
8382
})
84-
t.teardown(fastify.close.bind(fastify))
83+
t.after(() => fastify.close())
8584

8685
await fastify.ready()
8786

88-
t.ok(fastify.log)
89-
t.same(typeof fastify.log, 'object')
87+
t.assert.ok(fastify.log)
88+
t.assert.strictEqual(typeof fastify.log, 'object')
9089
})
9190

92-
t.test('Wrap IPv6 address in listening log message', async (t) => {
91+
const interfaces = os.networkInterfaces()
92+
const ipv6 = Object.keys(interfaces)
93+
.filter(name => name.substr(0, 2) === 'lo')
94+
.map(name => interfaces[name])
95+
.reduce((list, set) => list.concat(set), [])
96+
.filter(info => info.family === 'IPv6')
97+
.map(info => info.address)
98+
.shift()
99+
100+
await t.test('Wrap IPv6 address in listening log message', { skip: !ipv6 }, async (t) => {
93101
t.plan(1)
94102

95-
const interfaces = os.networkInterfaces()
96-
const ipv6 = Object.keys(interfaces)
97-
.filter(name => name.substr(0, 2) === 'lo')
98-
.map(name => interfaces[name])
99-
.reduce((list, set) => list.concat(set), [])
100-
.filter(info => info.family === 'IPv6')
101-
.map(info => info.address)
102-
.shift()
103-
104-
if (ipv6 === undefined) {
105-
t.pass('No IPv6 loopback interface')
106-
} else {
107-
const stream = split(JSON.parse)
108-
const fastify = Fastify({
109-
logger: {
110-
stream,
111-
level: 'info'
112-
}
113-
})
114-
t.teardown(fastify.close.bind(fastify))
103+
const stream = split(JSON.parse)
104+
const fastify = Fastify({
105+
logger: {
106+
stream,
107+
level: 'info'
108+
}
109+
})
110+
t.after(() => fastify.close())
115111

116-
await fastify.ready()
117-
await fastify.listen({ port: 0, host: ipv6 })
112+
await fastify.ready()
113+
await fastify.listen({ port: 0, host: ipv6 })
118114

119-
{
120-
const [line] = await once(stream, 'data')
121-
t.same(line.msg, `Server listening at http://[${ipv6}]:${fastify.server.address().port}`)
122-
}
115+
{
116+
const [line] = await once(stream, 'data')
117+
t.assert.strictEqual(line.msg, `Server listening at http://[${ipv6}]:${fastify.server.address().port}`)
123118
}
124119
})
125120

126-
t.test('Do not wrap IPv4 address', async (t) => {
121+
await t.test('Do not wrap IPv4 address', async (t) => {
127122
t.plan(1)
128123
const stream = split(JSON.parse)
129124
const fastify = Fastify({
@@ -132,24 +127,18 @@ t.test('logger instantiation', (t) => {
132127
level: 'info'
133128
}
134129
})
135-
t.teardown(fastify.close.bind(fastify))
130+
t.after(() => fastify.close())
136131

137132
await fastify.ready()
138133
await fastify.listen({ port: 0, host: '127.0.0.1' })
139134

140135
{
141136
const [line] = await once(stream, 'data')
142-
t.same(line.msg, `Server listening at http://127.0.0.1:${fastify.server.address().port}`)
137+
t.assert.strictEqual(line.msg, `Server listening at http://127.0.0.1:${fastify.server.address().port}`)
143138
}
144139
})
145140

146-
t.test('file option', async (t) => {
147-
const lines = [
148-
{ msg: /Server listening at/ },
149-
{ reqId: /req-/, req: { method: 'GET', url: '/' }, msg: 'incoming request' },
150-
{ reqId: /req-/, res: { statusCode: 200 }, msg: 'request completed' }
151-
]
152-
141+
await t.test('file option', async (t) => {
153142
const { file, cleanup } = createTempFile(t)
154143
// 0600 permissions (read/write for owner only)
155144
if (process.env.CITGM) { fs.writeFileSync(file, '', { mode: 0o600 }) }
@@ -158,7 +147,7 @@ t.test('logger instantiation', (t) => {
158147
logger: { file }
159148
})
160149

161-
t.teardown(async () => {
150+
t.after(async () => {
162151
await helper.sleep(250)
163152
// may fail on win
164153
try {
@@ -174,15 +163,20 @@ t.test('logger instantiation', (t) => {
174163
console.warn(err)
175164
}
176165
})
177-
t.teardown(fastify.close.bind(fastify))
166+
t.after(() => fastify.close())
178167

179168
fastify.get('/', function (req, reply) {
180-
t.ok(req.log)
169+
t.assert.ok(req.log)
181170
reply.send({ hello: 'world' })
182171
})
183172

184173
await fastify.ready()
185-
await fastify.listen({ port: 0, host: localhost })
174+
const server = await fastify.listen({ port: 0, host: localhost })
175+
const lines = [
176+
{ msg: `Server listening at ${server}` },
177+
{ req: { method: 'GET', url: '/' }, msg: 'incoming request' },
178+
{ res: { statusCode: 200 }, msg: 'request completed' }
179+
]
186180
await request(`http://${localhostForURL}:` + fastify.server.address().port)
187181

188182
await helper.sleep(250)
@@ -195,26 +189,26 @@ t.test('logger instantiation', (t) => {
195189
for (let line of log) {
196190
line = JSON.parse(line)
197191
if (id === undefined && line.reqId) id = line.reqId
198-
if (id !== undefined && line.reqId) t.equal(line.reqId, id)
199-
t.match(line, lines.shift())
192+
if (id !== undefined && line.reqId) t.assert.strictEqual(line.reqId, id)
193+
t.assert.ok(partialDeepStrictEqual(line, lines.shift()))
200194
}
201195
})
202196

203-
t.test('should be able to use a custom logger', (t) => {
197+
await t.test('should be able to use a custom logger', (t) => {
204198
t.plan(7)
205199

206200
const loggerInstance = {
207-
fatal: (msg) => { t.equal(msg, 'fatal') },
208-
error: (msg) => { t.equal(msg, 'error') },
209-
warn: (msg) => { t.equal(msg, 'warn') },
210-
info: (msg) => { t.equal(msg, 'info') },
211-
debug: (msg) => { t.equal(msg, 'debug') },
212-
trace: (msg) => { t.equal(msg, 'trace') },
201+
fatal: (msg) => { t.assert.strictEqual(msg, 'fatal') },
202+
error: (msg) => { t.assert.strictEqual(msg, 'error') },
203+
warn: (msg) => { t.assert.strictEqual(msg, 'warn') },
204+
info: (msg) => { t.assert.strictEqual(msg, 'info') },
205+
debug: (msg) => { t.assert.strictEqual(msg, 'debug') },
206+
trace: (msg) => { t.assert.strictEqual(msg, 'trace') },
213207
child: () => loggerInstance
214208
}
215209

216210
const fastify = Fastify({ loggerInstance })
217-
t.teardown(fastify.close.bind(fastify))
211+
t.after(() => fastify.close())
218212

219213
fastify.log.fatal('fatal')
220214
fastify.log.error('error')
@@ -223,25 +217,25 @@ t.test('logger instantiation', (t) => {
223217
fastify.log.debug('debug')
224218
fastify.log.trace('trace')
225219
const child = fastify.log.child()
226-
t.equal(child, loggerInstance)
220+
t.assert.strictEqual(child, loggerInstance)
227221
})
228222

229-
t.test('should throw in case a partially matching logger is provided', async (t) => {
223+
await t.test('should throw in case a partially matching logger is provided', async (t) => {
230224
t.plan(1)
231225

232226
try {
233227
const fastify = Fastify({ logger: console })
234228
await fastify.ready()
235229
} catch (err) {
236-
t.equal(
230+
t.assert.strictEqual(
237231
err instanceof FST_ERR_LOG_INVALID_LOGGER,
238232
true,
239233
"Invalid logger object provided. The logger instance should have these functions(s): 'fatal,child'."
240234
)
241235
}
242236
})
243237

244-
t.test('can use external logger instance with custom serializer', async (t) => {
238+
await t.test('can use external logger instance with custom serializer', async (t) => {
245239
const lines = [['level', 30], ['req', { url: '/foo' }], ['level', 30], ['res', { statusCode: 200 }]]
246240
t.plan(lines.length + 1)
247241

@@ -260,10 +254,10 @@ t.test('logger instantiation', (t) => {
260254
const fastify = Fastify({
261255
loggerInstance
262256
})
263-
t.teardown(fastify.close.bind(fastify))
257+
t.after(() => fastify.close())
264258

265259
fastify.get('/foo', function (req, reply) {
266-
t.ok(req.log)
260+
t.assert.ok(req.log)
267261
req.log.info('log success')
268262
reply.send({ hello: 'world' })
269263
})
@@ -277,20 +271,12 @@ t.test('logger instantiation', (t) => {
277271
const check = lines.shift()
278272
const key = check[0]
279273
const value = check[1]
280-
t.same(line[key], value)
274+
t.assert.deepStrictEqual(line[key], value)
281275
if (lines.length === 0) break
282276
}
283277
})
284278

285-
t.test('The logger should accept custom serializer', async (t) => {
286-
const lines = [
287-
{ msg: /^Server listening at / },
288-
{ req: { url: '/custom' }, msg: 'incoming request' },
289-
{ res: { statusCode: 500 }, msg: 'kaboom' },
290-
{ res: { statusCode: 500 }, msg: 'request completed' }
291-
]
292-
t.plan(lines.length + 1)
293-
279+
await t.test('The logger should accept custom serializer', async (t) => {
294280
const stream = split(JSON.parse)
295281
const fastify = Fastify({
296282
logger: {
@@ -305,25 +291,32 @@ t.test('logger instantiation', (t) => {
305291
}
306292
}
307293
})
308-
t.teardown(fastify.close.bind(fastify))
294+
t.after(() => fastify.close())
309295

310296
fastify.get('/custom', function (req, reply) {
311-
t.ok(req.log)
297+
t.assert.ok(req.log)
312298
reply.send(new Error('kaboom'))
313299
})
314300

315301
await fastify.ready()
316-
await fastify.listen({ port: 0, host: localhost })
302+
const server = await fastify.listen({ port: 0, host: localhost })
303+
const lines = [
304+
{ msg: `Server listening at ${server}` },
305+
{ req: { url: '/custom' }, msg: 'incoming request' },
306+
{ res: { statusCode: 500 }, msg: 'kaboom' },
307+
{ res: { statusCode: 500 }, msg: 'request completed' }
308+
]
309+
t.plan(lines.length + 1)
317310

318311
await request(`http://${localhostForURL}:` + fastify.server.address().port + '/custom')
319312

320313
for await (const [line] of on(stream, 'data')) {
321-
t.match(line, lines.shift())
314+
t.assert.ok(partialDeepStrictEqual(line, lines.shift()))
322315
if (lines.length === 0) break
323316
}
324317
})
325318

326-
t.test('should throw in case the external logger provided does not have a child method', async (t) => {
319+
await t.test('should throw in case the external logger provided does not have a child method', async (t) => {
327320
t.plan(1)
328321
const loggerInstance = {
329322
info: console.info,
@@ -338,7 +331,7 @@ t.test('logger instantiation', (t) => {
338331
const fastify = Fastify({ logger: loggerInstance })
339332
await fastify.ready()
340333
} catch (err) {
341-
t.equal(
334+
t.assert.strictEqual(
342335
err instanceof FST_ERR_LOG_INVALID_LOGGER,
343336
true,
344337
"Invalid logger object provided. The logger instance should have these functions(s): 'child'."

0 commit comments

Comments
 (0)