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

How to expect pino to not be called? #18

Open
AviBueno opened this issue Oct 29, 2024 · 5 comments
Open

How to expect pino to not be called? #18

AviBueno opened this issue Oct 29, 2024 · 5 comments
Labels
good first issue Good for newcomers

Comments

@AviBueno
Copy link

AviBueno commented Oct 29, 2024

Side note: Finally! Great addition to pino!

Question: How could I check that pino was not called at all when calling func(false, logger)?

const func = (b, logger) => {
  if (b === true) {
    logger.info("b is true");
  }
}
@mcollina
Copy link
Member

I would try with consecutive with an empty array. However, this is not covered by tests.

@AviBueno
Copy link
Author

Thanks @mcollina, it failes on time out.

I think there's room to support something like:

  1. The nonintuitive: await pinoTest.zero(stream)
  2. The more intuitive: await expect(pinoTest).toHaveBeenCalledTimes(0)
  3. A third option
  • Just a suggestion (don't have bandwidth to introduce a PR)

@mcollina mcollina added the good first issue Good for newcomers label Oct 30, 2024
@mcollina
Copy link
Member

It's a good PR to make!

@ruddenchaux
Copy link
Contributor

A new never(stream) API may be useful for this; in the meantime, you could check the length of the readable stream

const func = (b, logger) => {
  if (b === true) {
    logger.info("b is true");
  }
}

test('should never log', () => {
  const stream = pinoTest.sink()
  const instance = pino(stream)

  func(false, instance)

  assert.strictEqual(stream.readableLength, 0, 'Stream should not have received any logs')
})

@AviBueno
Copy link
Author

Sounds awesome @ruddenchaux!
Thanks for the workaround. That works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants