Skip to content

Commit

Permalink
test: add unit test for log metadata colorizing
Browse files Browse the repository at this point in the history
  • Loading branch information
pasha-vuiko committed Mar 25, 2024
1 parent 9d7c002 commit 8a4d736
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/utils/prettify-metadata.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const getColorizer = require('../colors')
const tap = require('tap')
const prettifyMetadata = require('./prettify-metadata')
const context = {
Expand Down Expand Up @@ -113,3 +114,36 @@ tap.test('uses prettifiers from passed prettifiers object', async t => {
})
t.equal(str, '(JOE/1234__ on BAR) <BAZ>')
})

tap.test('uses colorizer from passed context to colorize metadata', async t => {
const prettifiers = {
name (input, _key, log, { colors }) {
return colors.blue(input)
},
pid (input, _key, log, { colors }) {
return colors.red(input)
},
hostname (input, _key, log, { colors }) {
return colors.green(input)
},
caller (input, _key, log, { colors }) {
return colors.cyan(input)
}
}
const log = { name: 'foo', pid: '1234', hostname: 'bar', caller: 'baz' }
const colorizer = getColorizer(true)
const context = {
customPrettifiers: prettifiers,
colorizer
}

const result = prettifyMetadata({ log, context })

const colorizedName = colorizer.colors.blue(log.name)
const colorizedPid = colorizer.colors.red(log.pid)
const colorizedHostname = colorizer.colors.green(log.hostname)
const colorizedCaller = colorizer.colors.cyan(log.caller)
const expected = `(${colorizedName}/${colorizedPid} on ${colorizedHostname}) <${colorizedCaller}>`

t.equal(result, expected)
})

0 comments on commit 8a4d736

Please sign in to comment.