Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unified Handler Tests #2020

Merged
merged 11 commits into from
Sep 28, 2024
31 changes: 14 additions & 17 deletions test/exception-handler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*
*/

const stream = require('stream');
const assume = require('assume');
const mocha = require('mocha');
const winston = require('../lib/winston');
Expand Down Expand Up @@ -38,6 +37,7 @@ describe('ExceptionHandler', function () {

it('new ExceptionHandler()', function () {
assume(function () {
// eslint-disable-next-line no-new
new winston.ExceptionHandler();
}).throws(/Logger is required/);
});
Expand Down Expand Up @@ -70,21 +70,18 @@ describe('ExceptionHandler', function () {

it('.handle()', function (done) {
var existing = helpers.clearExceptions();
var writeable = new stream.Writable({
objectMode: true,
write: function (info) {
assume(info).is.an('object');
assume(info.error).is.an('error');
assume(info.error.message).equals('wtf this error');
assume(info.message).includes('uncaughtException: wtf this error');
assume(info.stack).is.a('string');
assume(info.process).is.an('object');
assume(info.os).is.an('object');
assume(info.trace).is.an('array');

existing.restore();
done();
}
var writeable = helpers.writeable(function (info) {
assume(info).is.an('object');
assume(info.error).is.an('error');
assume(info.error.message).equals('wtf this error');
assume(info.message).includes('uncaughtException: wtf this error');
assume(info.stack).is.a('string');
assume(info.process).is.an('object');
assume(info.os).is.an('object');
assume(info.trace).is.an('array');

existing.restore();
done();
});

var transport = new winston.transports.Stream({ stream: writeable });
Expand All @@ -93,7 +90,7 @@ describe('ExceptionHandler', function () {
transports: [transport]
});

assume(handler.catcher).equals(undefined);
assume(handler.catcher).is.a('undefined');

transport.handleExceptions = true;
handler.handle();
Expand Down
6 changes: 1 addition & 5 deletions test/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@

const assume = require('assume'),
fs = require('fs'),
path = require('path'),
through = require('through2'),
spawn = require('child_process').spawn,
stream = require('stream'),
util = require('util'),
winston = require('../../lib/winston'),
mockTransport = require('./mocks/mock-transport');

Expand Down Expand Up @@ -125,9 +123,7 @@ helpers.throw = function (msg) {
* @param {String} msg Error mesage to use
*/
helpers.reject = function (msg) {
return new Promise((resolve, reject) => {
reject(msg);
});
return Promise.reject(msg);
};

/**
Expand Down
41 changes: 23 additions & 18 deletions test/rejection-handler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*
*/

const stream = require('stream');
const assume = require('assume');
const mocha = require('mocha');
const winston = require('../lib/winston');
Expand Down Expand Up @@ -38,6 +37,7 @@ describe('UnhandledRejectionHandler', function () {

it('new RejectionHandler()', function () {
assume(function () {
// eslint-disable-next-line no-new
new winston.RejectionHandler();
}).throws(/Logger is required/);
});
Expand Down Expand Up @@ -70,21 +70,19 @@ describe('UnhandledRejectionHandler', function () {

it('.handle()', function (done) {
var existing = helpers.clearRejections();
var writeable = new stream.Writable({
objectMode: true,
write: function (info) {
assume(info).is.an('object');
assume(info.error).is.an('error');
assume(info.error.message).equals('wtf this rejection');
assume(info.message).includes('unhandledRejection: wtf this rejection');
assume(info.stack).is.a('string');
assume(info.process).is.an('object');
assume(info.os).is.an('object');
assume(info.trace).is.an('array');

existing.restore();
done();
}
var writeable = helpers.writeable(function (info) {
console.log('in writeable', info);
fearphage marked this conversation as resolved.
Show resolved Hide resolved
assume(info).is.an('object');
assume(info.error).is.an('error');
assume(info.error.message).equals('wtf this rejection');
assume(info.message).includes('unhandledRejection: wtf this rejection');
assume(info.stack).is.a('string');
assume(info.process).is.an('object');
assume(info.os).is.an('object');
assume(info.trace).is.an('array');

existing.restore();
done();
});

var transport = new winston.transports.Stream({ stream: writeable });
Expand All @@ -93,7 +91,7 @@ describe('UnhandledRejectionHandler', function () {
transports: [transport]
});

assume(handler.catcher).equals(undefined);
assume(handler.catcher).is.a('undefined');

transport.handleRejections = true;
handler.handle();
Expand All @@ -103,7 +101,14 @@ describe('UnhandledRejectionHandler', function () {
handler.catcher
]);

helpers.reject('wtf this rejection').then(done());
const meh = (e) => console.log('my handler', e);
fearphage marked this conversation as resolved.
Show resolved Hide resolved
process.on('unhandledRejection', meh);
assume(process.listeners('unhandledRejection')).deep.equals([
handler.catcher,
meh
]);

process.emit('unhandledRejection', helpers.reject('wtf this rejection'));
});

it('.getAllInfo(undefined)', function () {
Expand Down