diff --git a/lib/winston/profiler.js b/lib/winston/profiler.js index edcc5a65a..d632fcee5 100644 --- a/lib/winston/profiler.js +++ b/lib/winston/profiler.js @@ -6,13 +6,12 @@ */ 'use strict'; - /** * TODO: add class description. * @type {Profiler} * @private */ -module.exports = class Profiler { +class Profiler { /** * Constructor function for the Profiler instance used by * `Logger.prototype.startTimer`. When done is called the timer will finish @@ -21,12 +20,13 @@ module.exports = class Profiler { * @private */ constructor(logger) { - if (!logger) { - throw new Error('Logger is required for profiling.'); + const Logger = require('./logger'); + if (typeof logger !== 'object' || Array.isArray(logger) || !(logger instanceof Logger)) { + throw new Error('Logger is required for profiling'); + } else { + this.logger = logger; + this.start = Date.now(); } - - this.logger = logger; - this.start = Date.now(); } /** @@ -49,3 +49,5 @@ module.exports = class Profiler { return this.logger.write(info); } }; + +module.exports = Profiler; diff --git a/test/unit/winston/profiler.test.js b/test/unit/winston/profiler.test.js index b0f86e95b..d4d993c05 100644 --- a/test/unit/winston/profiler.test.js +++ b/test/unit/winston/profiler.test.js @@ -7,27 +7,27 @@ */ const assume = require('assume'); +const Logger = require('../../../lib/winston/logger'); const Profiler = require('../../../lib/winston/profiler'); - +const { PassThrough } = require('stream'); describe('Profiler', function () { it('new Profiler()', function () { assume(function () { - var profiler = new Profiler(); - }).throws(); + new Profiler(); + }).throws('Logger is required for profiling'); }); it('.done({ info })', function (done) { - var profiler = new Profiler({ - write: function (info) { - assume(info).is.an('object'); - assume(info.something).equals('ok'); - assume(info.level).equals('info'); - assume(info.durationMs).is.a('number'); - assume(info.message).equals('testing1'); - done(); - } - }); - + const logger = new Logger(); + logger.write = function (info) { + assume(info).is.an('object'); + assume(info.something).equals('ok'); + assume(info.level).equals('info'); + assume(info.durationMs).is.a('number'); + assume(info.message).equals('testing1'); + done(); + }; + var profiler = new Profiler(logger); setTimeout(function () { profiler.done({ something: 'ok', @@ -36,4 +36,30 @@ describe('Profiler', function () { }); }, 200); }); + + it('non logger object', function(){ + assume(function() { + new Profiler(new Error('Unknown error')); + }).throws('Logger is required for profiling'); + + assume(function () { + new Profiler({a:'b'}); + }).throws('Logger is required for profiling'); + + assume(function(){ + new Profiler([1,2,3,4]); + }).throws('Logger is required for profiling'); + + assume(function () { + new Profiler(new PassThrough()); + }).throws('Logger is required for profiling'); + + assume(function () { + new Profiler(2); + }).throws('Logger is required for profiling'); + + assume(function () { + new Profiler('1'); + }).throws('Logger is required for profiling'); + }) });