pinojs / pino-pretty

🌲Basic prettifier for Pino log lines

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

pino-pretty transport worker keeps Jest handles open

vicusbass opened this issue · comments

commented

While using pino with pino-pretty, Jest tests are throwing a lot of warnings about open handles. This does not happen when disabling pino-pretty.

The implementation:

import pino from 'pino';
const transport = pino.transport({
  target: 'pino-pretty',
  options: {
    colorize: true,
    translateTime: true,
    ignore: 'hostname,pid',
  },
});
const log = pino(transport);

export default log;

Warnings:

Jest has detected the following 7 open handles potentially keeping Jest from exiting:
 ●  WORKER
1 | import pino from 'pino';
> 2 | const transport = pino.transport({
 |                        ^
3 |   target: 'pino-pretty',
4 |   options: {
5 |     colorize: true,
at createWorker (../../node_modules/thread-stream/index.js:50:18)
at new ThreadStream (../../node_modules/thread-stream/index.js:204:19)
at buildStream (../../node_modules/pino/lib/transport.js:39:18)
at Function.transport (../../node_modules/pino/lib/transport.js:112:10)
at Object.<anonymous> (src/log.ts:2:24)
at Object.<anonymous> (src/index.ts:13:1)
at Object.<anonymous> (__tests__/unit/generate-data.spec.ts:1:1)

Would you like to send a Pull Request to address this issue? Remember to add unit tests.

You should close the transport when the jest test ends.

commented

@mcollina Thanks for the suggestion. Not sure how to catch Jest tests end and close transports, the logger is not under test.

@mcollina Thanks for the suggestion. Not sure how to catch Jest tests end and close transports, the logger is not under test.

I might not have the exact same issue as you but I found my solution by taking advantage of the fact that the environment variable NODE_ENV is set to "test" when jest is running. As a result I did this little trick:

// NODE_ENV is set to "test" when jest is running.
const inTest = process.env.NODE_ENV === "test";

const log = logger({
  enabled: !inTest,
  transport: !inTest ? {
    target: "pino-pretty",
    options: {
      colorize: true,
    },
  } : undefined,

[...]

Obviously, make adjustments to your own code.

That's.... a very poor practice. You should not adjust your application code to account for the test environment.

@jsumners I don't know how to "close the transport" when testing. All I was doing was trying to take care of the deprecation warning for prettyPrint which forced me to use this different option which gave me this warning that @vicusbass also had.

A wild guess, given the lack of any reproduction code, would be:

Your application needs to handle SIGTERM to clean up resources on shutdown.