pino-test is a utility library designed to simplify the process of testing applications that use the Pino logger. It provides a `sink()` method to create a transform stream that captures Pino's JSON log output, allowing for easy inspection and assertion within test suites. The library also offers `once()` and `consecutive()` helpers to assert single or multiple log entries against expected values, optionally with custom assertion functions. The current stable version is 2.0.0, which includes code modernization and drops support for older Node.js versions. While its release cadence isn't strictly fixed, major versions appear to be released as significant updates or breaking changes warrant, with v2.0.0 following v1.0.0 after some time. It differentiates itself by tightly integrating with Pino's stream-based logging for efficient and precise log verification without complex mock setups.
npm install pino-testVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to create a `pino-test` sink, configure a Pino logger to use it, and assert single log messages using `pinoTest.once`, including an example with a custom assertion function.
Upgrade your Node.js environment to a supported LTS version (e.g., Node.js 16, 18, or 20).
Ensure your `expected` object in `once()` or `consecutive()` includes realistic values for `time`, `pid`, and `hostname` if you intend to assert them, or provide a custom `is` function to ignore these fields for comparison.
Always ensure data written to `pinoTest.sink()` is valid JSON. For robustness, configure `sink({ destroyOnError: false, emitErrorEvent: true })` to handle parsing errors gracefully via an 'error' event without terminating the stream.Ensure all data sent to the `pinoTest.sink()` stream is a valid JSON string, as Pino's default output format is JSON Lines.
Adjust your `expected` object to accurately reflect the full log structure, including auto-generated fields, or pass a custom `is` function to `once` or `consecutive` to define specific comparison logic for only the fields you care about.
Install `pino` explicitly: `npm install pino` or `npm install pino --save-dev`.