Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logs missing in Jest tests #480

Open
OliverJAsh opened this issue Nov 24, 2023 · 5 comments
Open

Logs missing in Jest tests #480

OliverJAsh opened this issue Nov 24, 2023 · 5 comments
Labels
bug Something isn't working documentation

Comments

@OliverJAsh
Copy link
Contributor

I am following the recommendation here: https://github.com/pinojs/pino-pretty/tree/db278755318e2f1c565df736bbedd7eb7a2fd73f#usage-with-jest

Reduced test case:

package.json:

{
  "dependencies": {
    "jest": "^29.7.0",
    "pino": "^8.16.2",
    "pino-pretty": "^10.2.3"
  }
}

test.js:

const pino = require('pino')
const pretty = require('pino-pretty')

test('test pino-pretty', () => {
  const logger = pino(pretty({ sync: true }));
  logger.info('Info');
  logger.error('Error');
});

When I run jest test.js I get this output:

 RUNS  ./test.js
 PASS  ./test.jsNFO (81668): Info
  ✓ test pino-pretty (6 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.129 s, estimated 1 s
Ran all test suites matching /test.js/i.
✨  Done in 0.37s.
  • The info message is partially truncated.
  • The error message is missing.
@jsumners
Copy link
Member

All I can suggest is that if you need to verify the final NDJSON of individual logs, you should supply a stream that collects the logs and then inspect that collection. See pinojs/pino#1274 (comment).

@mcollina mcollina added bug Something isn't working documentation labels Nov 25, 2023
@mcollina
Copy link
Member

I think we have a bug in our docs, I don't think Jest would ever render things correctly.

@FiveOFive
Copy link

FiveOFive commented Jan 30, 2024

I think we have a bug in our docs, I don't think Jest would ever render things correctly.

Is that confirmed that Jest won't ever render correctly? I'm also trying to follow those docs but it seems like even with sync: true the log entries are still showing up after tests

@Ugikie
Copy link

Ugikie commented Apr 17, 2024

Confirming this because I'm also using sync: true and jest is still complaining about open handles

@mostlylikeable
Copy link

mostlylikeable commented Aug 6, 2024

I was able to workaround this by using prettyFactory directly in tests. I've tried configuring { sync: true }, using as transport, destination and other things and wasn't able to get all log messages to show up. It seems like there's still something stuck in the buffer even when using { sync: true }, and i'm not sure if it has something to do with the event-based nature of sonic-boom not playing well with jest or what, but this is working (well enough) for me.

// foo.ts
import pino from 'pino';
import PinoPretty, { prettyFactory } from 'pino-pretty';

const getPino = () => {
  const isJest = !!process.env.JEST_WORKER_ID;
  if (isJest) {
    const prettify = prettyFactory({ sync: true });
    // configure custom stream that forwards pretty message to console
    return pino({}, {
      write(data: unknown) {
        console.log(prettify(data));
      },
    });
  }
  return pino({}, PinoPretty());
};

export const foo = () => {
  const logger = getPino();
  console.log('console test');
  logger.info('pino test');
};
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation
Projects
None yet
Development

No branches or pull requests

6 participants