I am using pino as a logger with output sent to stdout destination. I am trying to test that a debug message is written to stdout but cannot see anything written to process.stdout in the test....can anyone help?
Pino logger
'use strict'
const pino = require('pino')
const log = pino({ name: 'TensorFlow-WithFiltering-And-MQTT', level: 'debug' }, pino.destination(1))
module.exports = {
parentLogger: log
}
The code under test
const { parentLogger, Process } = require('./libs/utils/src')
const log = parentLogger.child({ module: 'child_process' })
/** main function that spawns process is here */
/**
Want to test that the promise resolves and the debug message is written to stdout when it resolves.
In VSCode it is hitting the breakpoint in the then block and can see that it resolves
but the test included below fails
**/
main()
.then(() => {
log.debug('notify.js process finished initialising')
})
.catch((error) => {
log.error(`notify.js process failed to initialise := ${error}`)
})
Current Jest test
test.only('main function resolves promise and logs debug message', () => {
const args = [
'node',
'notify.js',
`mqtts://${global.config.user}:${global.config.user}@${global.config.host}:8883`,
`${__dirname}/../../docker/certs/localCA.crt`,
5000,
'false'
]
/** pino logger writes to stdout - trying to check what is written to stdout stream **/
let logged = ''
process.stdout._orig_write = process.stdout.write
process.stdout.write = (data) => {
logged = logged + data
process.stdout._orig_write(data)
}
const oldArgs = process.argv
process.argv = args
const notify = require('../../../src/notify')
try {
expect(logged).toEqual('notify.js process finished initialising')
} finally {
process.argv = oldArgs
process.stdout.write = process.stdout._orig_write
}
})