Skip to content

Commit

Permalink
[fix refactor test] Fight mocha and "allowUncaught" (see: mochajs/moc…
Browse files Browse the repository at this point in the history
…ha#1985). Move some tests into `test/logger-legacy.test.js`
  • Loading branch information
indexzero committed Oct 2, 2017
1 parent 3d8f51a commit c96bc2e
Show file tree
Hide file tree
Showing 4 changed files with 284 additions and 95 deletions.
102 changes: 86 additions & 16 deletions test/exception-handler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,98 @@
*
*/

var winston = require('../lib/winston'),
var stream = require('stream'),
assume = require('assume'),
mocha = require('mocha'),
winston = require('../lib/winston'),
helpers = require('./helpers');

//
// This is an awful and fragile hack that
// needs to be changed ASAP.
// https://github.com/mochajs/mocha/issues/1985
//
var _runTest = mocha.Runner.prototype.runTest;
mocha.Runner.prototype.runTest = function () {
this.allowUncaught = true;
_runTest.apply(this, arguments);
};

describe('ExceptionHandler', function () {
var logger = new winston.Logger();
var handler = new winston.ExceptionHandler(logger);
// it('has expected methods', function () {
// var handler = helpers.exceptionHandler();
// assume(handler.handle).is.a('function');
// assume(handler.unhandle).is.a('function');
// assume(handler.getAllInfo).is.a('function');
// assume(handler.getProcessInfo).is.a('function');
// assume(handler.getOsInfo).is.a('function');
// assume(handler.getTrace).is.a('function');
// });

it('.getProcessInfo()', function () {
helpers.assertProcessInfo(
handler.getProcessInfo()
);
});
// it('new ExceptionHandler()', function () {
// assume(function () {
// new winston.ExceptionHandler();
// }).throws(/Logger is required/);
// });

// it('new ExceptionHandler(logger)', function () {
// var logger = new winston.Logger();
// var handler = new winston.ExceptionHandler(logger);
// assume(handler.logger).equals(logger);
// });

// it('.getProcessInfo()', function () {
// var handler = helpers.exceptionHandler();
// helpers.assertProcessInfo(
// handler.getProcessInfo()
// );
// });

// it('.getOsInfo()', function () {
// var handler = helpers.exceptionHandler();
// helpers.assertOsInfo(
// handler.getOsInfo()
// );
// });

// it('.getTrace(new Error)', function () {
// var handler = helpers.exceptionHandler();
// helpers.assertTrace(
// handler.getTrace(new Error())
// );
// });

it('.handle()', function (done) {
var existing = helpers.clearExceptions();
var writeable = new stream.Writable({
objectMode: true,
write: function (info) {
console.dir(info);
existing.restore();
done();
}
});

var transport = new winston.transports.Stream({ stream: writeable });
var handler = helpers.exceptionHandler({ transports: [transport] });

assume(handler.catcher).equals(undefined);

transport.handleExceptions = true;
handler.handle();

assume(handler.catcher).is.a('function');
assume(process.listeners('uncaughtException'))
.deep.equals([handler.catcher]);

it('.getOsInfo()', function () {
helpers.assertOsInfo(
handler.getOsInfo()
);
helpers.throw('wtf this error');
});

it('.getTrace()', function () {
helpers.assertTrace(
handler.getTrace(new Error())
);
after(function () {
//
// Restore normal `runTest` functionality
// so that we only affect the current suite.
//
mocha.Runner.prototype.runTest = _runTest;
});
});
60 changes: 60 additions & 0 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,71 @@ var assume = require('assume'),
fs = require('fs'),
path = require('path'),
spawn = require('child_process').spawn,
stream = require('stream'),
util = require('util'),
winston = require('../lib/winston');

var helpers = exports;

/**
* Returns a new winston.Logger instance which will invoke
* the `write` method on each call to `.log`
*
* @param {function} write Write function for the specified stream
* @returns {Logger} A winston.Logger instance
*/
helpers.createLogger = function (write) {
var writeable = new stream.Writable({
objectMode: true,
write: write
});

return new winston.Logger({
transports: [
new winston.transports.Stream({ stream: writeable })
]
});
};

/**
* Creates a new ExceptionHandler instance with a new
* winston.Logger instance with the specified options
*
* @param {Object} opts Options for the logger associated
* with the ExceptionHandler
* @returns {ExceptionHandler} A new ExceptionHandler instance
*/
helpers.exceptionHandler = function (opts) {
var logger = new winston.Logger(opts);
return new winston.ExceptionHandler(logger);
};

/**
* Removes all listeners to `process.on('uncaughtException')`
* and returns an object that allows you to restore them later.
*
* @returns {Object} Facade to restore uncaughtException handlers.
*/
helpers.clearExceptions = function () {
var listeners = process.listeners('uncaughtException');
process.removeAllListeners('uncaughtException');

return {
restore: function () {
listeners.forEach(function (fn) {
process.on('uncaughtException', fn);
});
}
};
};

/**
* Throws an exception with the specified `msg`
*/
helpers.throw = function (msg) {
throw new Error(msg);
};

helpers.tryUnlink = function (file) {
try { fs.unlinkSync(file) }
catch (ex) { }
Expand Down
119 changes: 119 additions & 0 deletions test/logger-legacy.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* logger-legacy.test.js: Tests for Legacy APIs of winston < 3.0.0
*
* (C) 2010 Charlie Robbins
* MIT LICENSE
*
*/

'use strict';

var assume = require('assume'),
path = require('path'),
stream = require('stream'),
util = require('util'),
isStream = require('isstream'),
stdMocks = require('std-mocks'),
winston = require('../lib/winston'),
LegacyTransport = require('./mocks/legacy-transport'),
TransportStream = require('winston-transport'),
format = require('../lib/winston/formats/format'),
helpers = require('./helpers');

describe('Logger (legacy API)', function () {
it('new Logger({ DEPRECATED })', function () {
var deprecated = [
{ colors: true },
{ emitErrs: true },
{ formatters: [] },
{ padLevels: true },
{ rewriters: [] },
{ stripColors: true }
];

deprecated.forEach(function (opts) {
assume(function () {
var logger = new winston.Logger(opts)
}).throws(/Use a custom/);
});
});

it('.add(LegacyTransport)', function () {
stdMocks.use();
var logger = new winston.Logger();
var transport = new LegacyTransport();
logger.add(transport);
stdMocks.restore();
var output = stdMocks.flush();

assume(logger._readableState.pipesCount).equals(1);
assume(logger._readableState.pipes.transport).is.an('object');
assume(logger._readableState.pipes.transport).equals(transport);
assume(output.stderr).deep.equals(['legacy-test is a Legacy winston transport. Consider upgrading\n']);
});

it('.add(LegacyTransport) multiple', function () {
stdMocks.use();
var logger = new winston.Logger({
transports: [
new LegacyTransport(),
new LegacyTransport(),
new LegacyTransport()
]
});

stdMocks.restore();
var output = stdMocks.flush();

assume(logger._readableState.pipesCount).equals(3);
var errorMsg = 'legacy-test is a Legacy winston transport. Consider upgrading\n';
assume(output.stderr).deep.equals([errorMsg, errorMsg, errorMsg]);
});

it('.remove() [LegacyTransportStream]', function () {
var transports = [
new (winston.transports.Console)(),
new (LegacyTransport)()
];

var logger = new (winston.Logger)({ transports: transports });

assume(logger.transports.length).equals(2);
logger.remove(transports[1]);
assume(logger.transports.length).equals(1);
assume(logger.transports[0]).equals(transports[0]);
});

it('log(level, msg)', function (done) {
var logger = helpers.createLogger(function (info) {
assume(info).is.an('object');
assume(info.level).equals('info');
assume(info.message).equals('Some super awesome log message');
assume(info.raw).is.a('string');
done();
});

logger.log('info', 'Some super awesome log message')
});

it('log(level, msg, meta)', function (done) {
var meta = { one: 2 };
var logger = helpers.createLogger(function (info) {
assume(info).is.an('object');
assume(info).equals(meta);
assume(info.level).equals('info');
assume(info.message).equals('Some super awesome log message');
assume(info.one).equals(2);
assume(info.raw).is.a('string');
done();
});

logger.log('info', 'Some super awesome log message', meta);
});

it('.cli() throws', function () {
var logger = new winston.Logger();

assume(logger.cli).throws(/Use a custom/);
});
});
Loading

0 comments on commit c96bc2e

Please sign in to comment.