pino/test/hooks.test.js
James Sumners 41d1706410
Convert tests to node:test (#2299)
* Convert tests to node:test

* broken-pipe.test.js

* browser-is-level-enabled.test.js

* complex-objects.test.js

* crlf.test.js

* custom-levels.test.js

* error.test.js

* error-key.test.js

* escaping.test.js

* exit.test.js

* formatters.test.js

* hooks.test.js

* http.test.js

* is-level-enabled.test.js

* levels.test.js

* metadata.test.js

* mixin.test.js

* mixin-merge-strategy.test.js

* multistream.test.js

* redact.test.js

* serializers.test.js

* stdout-protection.test.js

* syncfalse.test.js

* timestamp.test.js

* timestamp-nano.test.js

* transport-stream.test.js

* esm/*

* internals/version.test.js

* transport/big.test.js

* transport/bundlers-support.test.js

* transport/caller.test.js

* transport/core.test.js

* transport/core.transpiled.test.js

* transport/module-link.test.js

* transport/pipeline.test.js

* transport/repl.test.js

* transport/sync-false.test.js

* transport/sync-true.test.js

* transport/targets.test.js

* transport/uses-pino-config.test.js

* clean helper

* finalize

* restore transport/core.test.js

* address feedback

* skip broken-pipe in CITGM

* remove unused package

* remove duplicate test file
2025-10-03 06:12:54 -04:00

115 lines
2.7 KiB
JavaScript

'use strict'
const { describe, test } = require('node:test')
const tspl = require('@matteo.collina/tspl')
const { sink, match, once } = require('./helper')
const pino = require('../')
describe('log method hook', () => {
test('gets invoked', async t => {
const plan = tspl(t, { plan: 7 })
const stream = sink()
const logger = pino({
hooks: {
logMethod (args, method, level) {
plan.equal(Array.isArray(args), true)
plan.equal(typeof level, 'number')
plan.equal(args.length, 3)
plan.equal(level, this.levels.values.info)
plan.deepEqual(args, ['a', 'b', 'c'])
plan.equal(typeof method, 'function')
plan.equal(method.name, 'LOG')
method.apply(this, [args.join('-')])
}
}
}, stream)
const o = once(stream, 'data')
logger.info('a', 'b', 'c')
match(await o, { msg: 'a-b-c' })
})
test('fatal method invokes hook', async t => {
const plan = tspl(t, { plan: 1 })
const stream = sink()
const logger = pino({
hooks: {
logMethod (args, method) {
plan.ok(true)
method.apply(this, [args.join('-')])
}
}
}, stream)
const o = once(stream, 'data')
logger.fatal('a')
match(await o, { msg: 'a' })
})
test('children get the hook', async t => {
const plan = tspl(t, { plan: 2 })
const stream = sink()
const root = pino({
hooks: {
logMethod (args, method) {
plan.ok(true)
method.apply(this, [args.join('-')])
}
}
}, stream)
const child = root.child({ child: 'one' })
const grandchild = child.child({ child: 'two' })
let o = once(stream, 'data')
child.info('a', 'b')
match(await o, { msg: 'a-b' })
o = once(stream, 'data')
grandchild.info('c', 'd')
match(await o, { msg: 'c-d' })
})
test('get log level', async t => {
const plan = tspl(t, { plan: 2 })
const stream = sink()
const logger = pino({
hooks: {
logMethod (args, method, level) {
plan.equal(typeof level, 'number')
plan.equal(level, this.levels.values.error)
method.apply(this, [args.join('-')])
}
}
}, stream)
const o = once(stream, 'data')
logger.error('a')
match(await o, { msg: 'a' })
})
})
describe('streamWrite hook', () => {
test('gets invoked', async () => {
const stream = sink()
const logger = pino({
hooks: {
streamWrite (s) {
return s.replaceAll('redact-me', 'XXX')
}
}
}, stream)
const o = once(stream, 'data')
logger.info('hide redact-me in this string')
match(await o, { msg: 'hide XXX in this string' })
})
})