Registry / testing / winston-spy

winston-spy

JSON →
library0.2.0jsnpmunverified

winston-spy is a small module that provides a custom Winston transport designed for testing Winston logging interactions using spy functions. It allows developers to intercept log messages and assert their content and calls within test suites. The package is currently at version 0.2.0 and has not seen active development since 2014, indicating it is abandoned. A key design choice is that winston-spy does not list winston as a direct dependency in its package.json, requiring the consuming application to install winston itself. This approach aims to ensure that winston-spy operates on the exact same winston module instance used by the application, mitigating Node.js module caching issues. However, this also means it relies on older winston APIs (winston.add, winston.remove) and is not compatible with modern winston v3.x+ versions.

npm install winston-spy
INSTALL
IMPORT
SIG · WINSTON-SPY
W
winston-spy
testingjavascriptv0.2.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.

SpyLogger
const SpyLogger = require('winston-spy');
import SpyLogger from 'winston-spy';
This package is CommonJS-only and relies on `require` for module loading, reflecting its age and lack of ESM support.
winston.add (transport method)
winston.add(winston.transports.SpyLogger, { spy: mySpy });
logger.add(new SpyLogger({ spy: mySpy }));
The `winston-spy` package uses the old global `winston.add` and `winston.remove` API to attach transports. This API is deprecated in Winston v3+ which uses `winston.createLogger` with a `transports` array.

Demonstrates how to integrate `winston-spy` with `sinon` to intercept and assert Winston global logger calls in a test environment, reflecting the package's intended usage with older Winston APIs.

const winston = require('winston'); const sinon = require('sinon'); const SpyLogger = require('winston-spy'); // Create a Sinon spy function to capture log calls const logSpy = sinon.spy(); // Remove the default Console transport (if any) from the global logger. // This API is for older Winston versions (pre-v3). winston.remove(winston.transports.Console); // Add the SpyLogger transport, passing the spy function to capture logs. winston.add(SpyLogger, { spy: logSpy }); // Define test message and metadata const testMessage = 'Hello, test world!'; const testMeta = { service: 'test-service', requestId: 'abc-123' }; // Log messages using the Winston global logger winston.log('info', testMessage, testMeta); winston.debug('debug', 'Another message for the spy.'); // Assertions (using Node.js built-in assert for demonstration) const assert = require('assert'); assert.ok(logSpy.calledTwice, 'Spy should have been called twice.'); assert.ok(logSpy.calledWith('info', testMessage, testMeta), 'First call should match info log.'); assert.ok(logSpy.calledWith('debug', 'Another message for the spy.'), 'Second call should match debug log.'); console.log('All assertions passed: logSpy successfully captured Winston calls.');
Debug
Known issues
breakingThis package is incompatible with Winston v3.x and newer versions. It relies on the global `winston.add()` and `winston.remove()` API for transport management, which was deprecated and replaced by `winston.createLogger()` and transport arrays in v3.x.
fix
Consider using a different testing strategy for modern Winston versions, such as directly mocking the `winston` logger instance or implementing a simple in-memory custom transport. If `winston-spy` is strictly required, downgrade `winston` to a 2.x version or earlier.
affects: >=0.2.0 (when used with Winston >=3.0.0)
gotchaThe `winston-spy` package does not list `winston` as a direct dependency in its `package.json`. You *must* explicitly include `winston` in your application's `package.json` dependencies for `winston-spy` to work correctly due to Node.js module caching behavior.
fix
Ensure `winston` is explicitly listed and installed as a dependency in your project: `npm install winston` or `yarn add winston`.
affects: >=0.1.0
deprecatedThe `winston-spy` package has not been updated since October 2014 and is considered abandoned. It uses old JavaScript syntax (`var`) and relies on an outdated `winston` API. While a fork exists, it also appears to be inactive for several years.
fix
Evaluate modern alternatives for testing Winston logging, such as directly mocking logger instances or implementing a custom in-memory transport that is compatible with current Winston versions.
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: winston.transports.SpyLogger is not a constructor
Attempting to use `winston-spy` with `winston` v3.x or later, which has a different API for transport registration and logger instantiation.
fix
This package is incompatible with Winston v3+. You need to either downgrade Winston to v2.x or earlier, or refactor your tests to directly mock logger instances or use a modern custom transport, as `winston-spy` does not support the `winston.createLogger` API.
Cannot find module 'winston'
The `winston` package is not installed as a direct dependency in your project, which `winston-spy` expects as a peer dependency for correct module resolution.
fix
Install `winston` explicitly in your project: `npm install winston` or `yarn add winston`.
Upgrade
Version history
0.2.0latest on npm
Audit
Dependencies
winstonrequiredRequired as a peer dependency by the application due to module caching; `winston-spy` relies on the host application's `winston` instance.
sinonrequiredUsed for creating spy functions to intercept log calls, as demonstrated in the package's usage examples.
Agent activity
42 hits · last 30 days
node
36
OpenAI (training)
1
Resources
winston-spy — npm install winston-spy · libregistry