winston-spy
This tiny module defines a winston transport that can be used to test winston logging with spies.
Installation
$ npm install --save-dev @chrisalderson/winston-spy sinon
$ npm install --save winston@next # If you haven't already ;)
Usage
Pass a function to the transport as the spy
option. This function will be
called whenever winston.log()
is called.
'use strict';
const winston = require('winston');
// Initialize the transport.
const spyTransport = new winston.transports.SpyTransport();
// Or setup your own spy and pass it down to the constructor like this:
// const spyTransport = new winston.transports.SpyTransport({
// spy: require('sinon').spy()
// });
// Add the transport the the default winston logger. Or a logger created with
// `winston.createLogger`.
winston.add(spyTransport);
// Access the `spy` via:
spyTransport.spy
Example
Here is an example of how to use the SpyTransport
in a test-case with
mocha
.
'use strict';
const sinon = require('sinon');
const SpyTransport = require('winston-spy');
const winston = require('winston');
describe('SpyTransport', () => {
let consoleTransport;
let transport;
before(() => {
consoleTransport = new winston.transports.Console({
silent: true
});
transport = new winston.transports.SpyTransport({ spy });
// This example uses the default logger of winston, but you can also use
// your own configured logger with `winston.createLogger`.
winston.add(consoleTransport);
winston.add(spyTransport);
});
it('should call spy', () => {
const info = {
message: 'foo',
level: 'info'
};
winston.log(info);
assume(spy.calledOnce).true();
assume(.spy.calledWith(info)).true();
// Or with the default spy of the `SpyTransport` instance.
// assume(spyTransport.spy.calledOnce).true();
// assume(spyTransport.spy.calledWith(info)).true();
});
after(() => {
winston.remove(consoleTransport);
winston.remove(spyTransport);
});
});