Skip to content

Commit be1be3f

Browse files
authored
Refactor (#71)
* use kebab-case * make imports "less" strange * fix test, improve style * fix test * improve test * replace benchmark with tinybench * update tsd * put types node to devDependencies
1 parent 7f43133 commit be1be3f

File tree

10 files changed

+63
-62
lines changed

10 files changed

+63
-62
lines changed

benchmark/format-date.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
'use strict'
22

3-
const benchmark = require('benchmark')
4-
const formatDate = require('../lib/formatDate')
3+
const { Bench } = require('tinybench')
4+
const formatDate = require('../lib/format-date')
55

66
const now = Date.now()
77

8-
new benchmark.Suite()
9-
.add('formatDate', function () { formatDate(now) }, { minSamples: 100 })
10-
.on('cycle', function onCycle (event) { console.log(String(event.target)) })
11-
.run()
8+
const benchmark = new Bench()
9+
10+
benchmark.add('formatDate', function () { formatDate(now) })
11+
12+
benchmark.run().then(() => {
13+
console.table(benchmark.table())
14+
})

benchmark/format-message.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

3-
const benchmark = require('benchmark')
4-
const messageFormatFactory = require('../lib/messageFormatFactory')
3+
const { Bench } = require('tinybench')
4+
const messageFormatFactory = require('../lib/message-format-factory')
55

66
const colors = {
77
60: 'red',
@@ -22,7 +22,10 @@ const log = {
2222
}
2323
}
2424

25-
new benchmark.Suite()
26-
.add('formatMessageColorized', function () { formatMessageColorized(log, 'message') }, { minSamples: 100 })
27-
.on('cycle', function onCycle (event) { console.log(String(event.target)) })
28-
.run()
25+
const benchmark = new Bench()
26+
27+
benchmark.add('formatMessageColorized', function () { formatMessageColorized(log, 'message') })
28+
29+
benchmark.run().then(() => {
30+
console.table(benchmark.table())
31+
})

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const pretty = require('pino-pretty')
4-
const messageFormatFactory = require('./lib/messageFormatFactory')
4+
const { messageFormatFactory } = require('./lib/message-format-factory')
55

66
const oneLineLogger = (opts = {}) => {
77
const { levels, colors, ...rest } = opts

lib/formatDate.js renamed to lib/format-date.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ function formatDate (timestamp) {
2323
}
2424

2525
module.exports = formatDate
26+
module.exports.formatDate = formatDate

lib/messageFormatFactory.js renamed to lib/message-format-factory.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
2-
const formatDate = require('./formatDate')
2+
3+
const formatDate = require('./format-date')
34
const colorizerFactory = require('pino-pretty').colorizerFactory
45

56
const messageFormatFactory = (levels, colors, useColors) => {
@@ -46,3 +47,4 @@ const messageFormatFactory = (levels, colors, useColors) => {
4647
}
4748

4849
module.exports = messageFormatFactory
50+
module.exports.messageFormatFactory = messageFormatFactory

package.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@
1414
"pino-pretty": "^13.0.0"
1515
},
1616
"devDependencies": {
17-
"benchmark": "^2.1.4",
17+
"@types/node": "^20.19.19",
1818
"eslint": "^9.17.0",
1919
"fastify": "^5.0.0",
2020
"neostandard": "^0.12.0",
21-
"tsd": "^0.32.0"
22-
},
23-
"resolutions": {
24-
"@types/node": "^20.12.7"
21+
"tinybench": "^5.0.1",
22+
"tsd": "^0.33.0"
2523
},
2624
"scripts": {
2725
"lint": "eslint",

test/fastify.test.js

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
'use strict'
22

3-
const { serverFactory, TIME, unmockTime, mockTime } = require('./helpers')
3+
const { before, beforeEach, after, test, describe, afterEach } = require('node:test')
44
const pretty = require('pino-pretty')
5-
const { before, beforeEach, after, test } = require('node:test')
6-
7-
const messages = []
8-
let server = serverFactory(messages, { colorize: false })
5+
const { serverFactory, TIME, unmockTime, mockTime } = require('./helpers')
96

107
before(() => {
118
mockTime()
@@ -15,30 +12,22 @@ after(() => {
1512
unmockTime()
1613
})
1714

18-
beforeEach(() => {
19-
// empty messages array
20-
messages.splice(0, messages.length)
15+
describe('should log server started messages', () => {
16+
let messages
17+
let server
2118

22-
server = serverFactory(messages)
23-
})
24-
25-
test('should log server started messages', async (t) => {
26-
t.beforeEach(async () => {
27-
await server.listen({ port: 63995 })
28-
t.afterEach(async () => await server.close())
19+
beforeEach(async () => {
20+
server = serverFactory(messages = [])
21+
await server.listen({ host: '127.0.0.1', port: 63995 })
2922
})
23+
afterEach(async () => { await server.close() })
3024

31-
await t.test('colors supported in TTY', { skip: !pretty.isColorSupported }, (t) => {
32-
const messagesExpected = [
33-
`${TIME} - \x1B[32minfo\x1B[39m - \x1B[36mServer listening at http://127.0.0.1:63995\x1B[39m\n`,
34-
`${TIME} - \x1B[32minfo\x1B[39m - \x1B[36mServer listening at http://[::1]:63995\x1B[39m\n`
35-
]
36-
25+
test('colors supported in TTY', { skip: !pretty.isColorSupported }, (t) => {
3726
// sort because the order of the messages is not guaranteed
38-
t.assert.deepStrictEqual(messages.sort(), messagesExpected.sort())
27+
t.assert.deepStrictEqual(messages[0], `${TIME} - \x1B[32minfo\x1B[39m - \x1B[36mServer listening at http://127.0.0.1:63995\x1B[39m\n`)
3928
})
4029

41-
await t.test(
30+
test(
4231
'colors not supported in TTY',
4332
{ skip: pretty.isColorSupported },
4433
(t) => {
@@ -51,12 +40,16 @@ test('should log server started messages', async (t) => {
5140
t.assert.deepStrictEqual(messages.sort(), messagesExpected.sort())
5241
}
5342
)
54-
})
43+
});
44+
45+
['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD'].forEach((method) => {
46+
describe(`should log request and response messages for ${method}`, () => {
47+
let messages
48+
let server
49+
50+
beforeEach(async () => {
51+
server = serverFactory(messages = [])
5552

56-
const methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD']
57-
methods.forEach((method) => {
58-
test('should log request and response messages for %p', async (t) => {
59-
t.beforeEach(async () => {
6053
const serverMethod = method === 'HEAD' ? 'GET' : method
6154
server[serverMethod.toLowerCase()]('/path', (_, req) => {
6255
req.send()
@@ -68,7 +61,7 @@ methods.forEach((method) => {
6861
})
6962
})
7063

71-
await t.test(
64+
test(
7265
'colors supported in TTY',
7366
{ skip: !pretty.isColorSupported },
7467
(t) => {
@@ -80,7 +73,7 @@ methods.forEach((method) => {
8073
}
8174
)
8275

83-
await t.test(
76+
test(
8477
'colors not supported in TTY',
8578
{ skip: pretty.isColorSupported },
8679
(t) => {
@@ -94,9 +87,11 @@ methods.forEach((method) => {
9487
})
9588
})
9689

97-
test('should handle user defined log', async (t) => {
98-
t.beforeEach(async () => {
99-
server = serverFactory(messages, { minimumLevel: 'trace' })
90+
describe('should handle user defined log', () => {
91+
let messages
92+
let server
93+
beforeEach(async () => {
94+
server = serverFactory(messages = [], { minimumLevel: 'trace' })
10095

10196
server.get('/a-path-with-user-defined-log', (res, req) => {
10297
res.log.fatal('a user defined fatal log')
@@ -112,7 +107,7 @@ test('should handle user defined log', async (t) => {
112107
await server.inject('/a-path-with-user-defined-log')
113108
})
114109

115-
await t.test('colors supported in TTY', { skip: !pretty.isColorSupported }, (t) => {
110+
test('colors supported in TTY', { skip: !pretty.isColorSupported }, (t) => {
116111
const messagesExpected = [
117112
`${TIME} - \x1B[32minfo\x1B[39m - GET /a-path-with-user-defined-log - \x1B[36mincoming request\x1B[39m\n`,
118113
`${TIME} - \x1B[41mfatal\x1B[49m - \x1B[36ma user defined fatal log\x1B[39m\n`,
@@ -126,7 +121,7 @@ test('should handle user defined log', async (t) => {
126121
t.assert.deepStrictEqual(messages, messagesExpected)
127122
})
128123

129-
await t.test(
124+
test(
130125
'colors not supported in TTY',
131126
{ skip: pretty.isColorSupported },
132127
(t) => {

test/formatDate.test.js renamed to test/format-date.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

3-
const formatDate = require('../lib/formatDate')
43
const { test } = require('node:test')
4+
const { formatDate } = require('../lib/format-date')
55

66
const timeFormatRE = /^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d[+-]\d\d\d\d$/
77

test/helpers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const { Writable } = require('node:stream')
44
const fastify = require('fastify')
55
const pino = require('pino')
6-
const target = require('..')
6+
const { oneLineLogger } = require('..')
77

88
const HOUR = 20
99
const TIME = `2017-02-14 ${HOUR}:51:48.000+0800`
@@ -16,7 +16,7 @@ const pinoFactory = (opts) => {
1616

1717
return pino(
1818
{ level },
19-
target({
19+
oneLineLogger({
2020
...opts
2121
})
2222
)

test/index.test.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
'use strict'
22

3-
const { EPOCH, TIME, MESSAGE_KEY, mockTime, unmockTime } = require('./helpers')
4-
const target = require('..')
53
const { before, after, test } = require('node:test')
64
const pretty = require('pino-pretty')
7-
const { messageFormatFactory } = target
5+
const { messageFormatFactory, oneLineLogger } = require('..')
6+
const { EPOCH, TIME, MESSAGE_KEY, mockTime, unmockTime } = require('./helpers')
87

98
const messageFormat = messageFormatFactory(
109
undefined,
@@ -20,8 +19,8 @@ after(() => {
2019
unmockTime()
2120
})
2221

23-
test('able to instantiate target without arguments', () => {
24-
target()
22+
test('able to instantiate oneLineLogger without arguments', () => {
23+
oneLineLogger()
2524
})
2625

2726
test('format log correctly with different logDescriptor', async (t) => {

0 commit comments

Comments
 (0)