pinojs / pino-test

MIT License
2 stars 1 forks source link

How to expect pino to not be called? #18

Open AviBueno opened 2 weeks ago

AviBueno commented 2 weeks ago

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 commented 2 weeks ago

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

AviBueno commented 2 weeks ago

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
mcollina commented 2 weeks ago

It's a good PR to make!

ruddenchaux commented 2 weeks ago

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 commented 2 weeks ago

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