Registry / testing / pino-test

pino-test

JSON →
library2.0.0jsnpmunverified

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-test
INSTALL
IMPORT
SIG · PINO-TEST
P
pino-test
testingjavascriptv2.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

pinoTest
const pinoTest = require('pino-test');
import pinoTest from 'pino-test';
For CommonJS, the module exports an object containing all utilities. For ESM, use `import * as pinoTest from 'pino-test'` or named imports for specific functions.
sink
import { sink } from 'pino-test';
const sink = require('pino-test').sink;
This is the recommended way to import individual functions in ESM. In CommonJS, access `sink` as a property of the main `pinoTest` object.
once
import { once } from 'pino-test';
Used to assert a single log entry. Access as `pinoTest.once` in CommonJS environments.

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.

import test from 'node:test'; import pino from 'pino'; import { sink, once } from 'pino-test'; test('pino should log an info message', async () => { const pinoSink = sink(); const logger = pino(pinoSink); logger.info('hello world'); const expected = { msg: 'hello world', level: 30 }; await once(pinoSink, expected); }); // Example with a custom assertion function function customAssert (received, expected, msg) { if (received.msg !== expected.msg) { throw new Error(`Expected msg '${expected.msg}' but received '${received.msg}'`); } } test('pino should log an info message using a custom assert function', async () => { const pinoSink = sink(); const logger = pino(pinoSink); logger.info('hello world from custom'); const expected = { msg: 'hello world from custom', level: 30 }; await once(pinoSink, expected, customAssert); });
Debug
Known issues
breakingVersion 2.0.0 drops support for older Node.js versions. Applications running on Node.js versions prior to the minimum supported by v2.0.0 (typically Node.js 16 or higher for modern Pino ecosystem libraries) will fail.
fix
Upgrade your Node.js environment to a supported LTS version (e.g., Node.js 16, 18, or 20).
affects: >=2.0.0
gotchaThe `once()` and `consecutive()` assertion helpers include default assertions for `time`, `pid`, and `hostname`. These fields are automatically added by Pino and can cause unexpected assertion failures if not explicitly accounted for or overwritten in your expected log object.
fix
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.
affects: >=1.0.0
gotchaPino log streams expect valid JSON objects (JSON Lines). Writing malformed or non-JSON data directly to a `pinoTest.sink()` stream will result in parsing errors and potentially stream destruction, depending on configuration.
fix
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.
affects: >=1.0.0
Errors
Common errors & fixes
Unexpected token h in JSON at position 0
Non-JSON data was written directly to the `pinoTest.sink()` stream, which expects JSON Lines input.
fix
Ensure all data sent to the `pinoTest.sink()` stream is a valid JSON string, as Pino's default output format is JSON Lines.
AssertionError: Input A is different from Input B
The expected log object provided to `pinoTest.once()` or `pinoTest.consecutive()` does not exactly match the received log, potentially due to automatically added fields like `time`, `pid`, or `hostname`.
fix
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.
Cannot find module 'pino'
`pino` is a peer dependency of `pino-test` and was not installed in the project's `node_modules`.
fix
Install `pino` explicitly: `npm install pino` or `npm install pino --save-dev`.
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies
pinorequiredPeer dependency. `pino-test` is designed to work with Pino logger streams, so Pino itself is required for any practical use.
Agent activity
14 hits · last 30 days
node
12
OpenAI (training)
2
Resources
pino-test — npm install pino-test · libregistry