@chrisalderson/winston-spy

Test winston logging with spies

Stats

stars 🌟issues ⚠️updated 🛠created 🐣size 🏋️‍♀️
21Mar 2, 2021Apr 29, 2018Minified + gzip package size for @chrisalderson/winston-spy in KB

Readme

winston-spy

Build Status Coverage Status Dependency Status devDependency Status

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);
  });
});

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.