From 8b7f570b7f0cdab655eb9bac8b9a7ec75ae9094c Mon Sep 17 00:00:00 2001 From: Aleksei Iatsiuk Date: Sun, 10 Sep 2023 20:16:08 +0300 Subject: [PATCH 1/2] Apply suggestions from code review Co-authored-by: teddddd --- test/logger.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/logger.js b/test/logger.js index 70cb8cb..e74e14b 100644 --- a/test/logger.js +++ b/test/logger.js @@ -18,13 +18,13 @@ exports.logger = { next(); }, - "is default logger is console object": function(test) { + "defaults to console logger": function(test) { const loggerName = Object.prototype.toString.call(this.mixpanel.config.logger); test.deepEqual(loggerName, '[object console]', "default logger is incorrect"); test.done(); }, - "is throws an error on incorrect logger object": function(test) { + "throws an error on incorrect logger object": function(test) { test.throws( () => this.mixpanel.set_config({logger: false}), TypeError, @@ -38,7 +38,7 @@ exports.logger = { test.done(); }, - "is write log for track() method": function(test) { + "writes log for track() method": function(test) { this.mixpanel.set_config({debug: true}); this.mixpanel.track('test', {foo: 'bar'}); @@ -58,7 +58,7 @@ exports.logger = { test.done(); }, - "is write log for increment() method": function(test) { + "writes log for increment() method": function(test) { this.mixpanel.set_config({debug: true}); this.mixpanel.people.increment('bob', 'page_views', 1); @@ -78,7 +78,7 @@ exports.logger = { test.done(); }, - "is write log for remove() method": function(test) { + "writes log for remove() method": function(test) { this.mixpanel.set_config({debug: true}); this.mixpanel.people.remove('bob', {'browsers': 'firefox'}); From f8e7f74ba6c1999755fd1db8fd7d1944ef186348 Mon Sep 17 00:00:00 2001 From: Aleksei Iatsiuk Date: Sun, 10 Sep 2023 20:50:58 +0300 Subject: [PATCH 2/2] add tests for custom logger --- test/logger.js | 247 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 178 insertions(+), 69 deletions(-) diff --git a/test/logger.js b/test/logger.js index e74e14b..7b7b374 100644 --- a/test/logger.js +++ b/test/logger.js @@ -2,99 +2,208 @@ const Sinon = require('sinon'); const Mixpanel = require('../lib/mixpanel-node'); exports.logger = { - setUp: function(cb) { - this.consoleDebugFn = Sinon.stub(console, 'debug'); + 'console logger': { + setUp: function(cb) { + this.consoleDebugFn = Sinon.stub(console, 'debug'); - this.mixpanel = Mixpanel.init('test token'); + this.mixpanel = Mixpanel.init('test token'); - this.mixpanel.send_request = () => {}; + this.mixpanel.send_request = () => {}; - cb(); - }, + cb(); + }, - tearDown: function(next) { - this.consoleDebugFn.restore(); + tearDown: function(next) { + this.consoleDebugFn.restore(); - next(); - }, + next(); + }, - "defaults to console logger": function(test) { - const loggerName = Object.prototype.toString.call(this.mixpanel.config.logger); - test.deepEqual(loggerName, '[object console]', "default logger is incorrect"); - test.done(); - }, + "defaults to console logger": function(test) { + const loggerName = Object.prototype.toString.call(this.mixpanel.config.logger); + test.deepEqual(loggerName, '[object console]', "default logger is incorrect"); + test.done(); + }, - "throws an error on incorrect logger object": function(test) { - test.throws( - () => this.mixpanel.set_config({logger: false}), - TypeError, - "logger object must be a valid Logger object" - ); - test.throws( - () => this.mixpanel.set_config({logger: {log: () => {}}}), - TypeError, - "logger object must be a valid Logger object" - ); - test.done(); - }, + "throws an error on incorrect logger object": function(test) { + test.throws( + () => this.mixpanel.set_config({logger: false}), + TypeError, + "logger object must be a valid Logger object" + ); + test.throws( + () => this.mixpanel.set_config({logger: {log: () => {}}}), + TypeError, + "logger object must be a valid Logger object" + ); + test.done(); + }, - "writes log for track() method": function(test) { - this.mixpanel.set_config({debug: true}); + "writes log for track() method": function(test) { + this.mixpanel.set_config({debug: true}); - this.mixpanel.track('test', {foo: 'bar'}); + this.mixpanel.track('test', {foo: 'bar'}); - test.ok( - this.consoleDebugFn.calledOnce, - `debug() method wasn't called on default logger` - ); + test.ok( + this.consoleDebugFn.calledOnce, + `debug() method wasn't called on default logger` + ); - const [message] = this.consoleDebugFn.lastCall.args; + const [message] = this.consoleDebugFn.lastCall.args; - test.ok( - message.startsWith('Sending the following event'), - 'incorrect argument was passed to debug() method' - ); + test.ok( + message.startsWith('Sending the following event'), + 'incorrect argument was passed to debug() method' + ); - test.done(); - }, + test.done(); + }, - "writes log for increment() method": function(test) { - this.mixpanel.set_config({debug: true}); + "writes log for increment() method": function(test) { + this.mixpanel.set_config({debug: true}); - this.mixpanel.people.increment('bob', 'page_views', 1); + this.mixpanel.people.increment('bob', 'page_views', 1); - test.ok( - this.consoleDebugFn.calledOnce, - `debug() method wasn't called on default logger` - ); + test.ok( + this.consoleDebugFn.calledOnce, + `debug() method wasn't called on default logger` + ); - const [message] = this.consoleDebugFn.lastCall.args; + const [message] = this.consoleDebugFn.lastCall.args; - test.ok( - message.startsWith('Sending the following data'), - 'incorrect argument was passed to debug() method' - ); + test.ok( + message.startsWith('Sending the following data'), + 'incorrect argument was passed to debug() method' + ); - test.done(); - }, + test.done(); + }, - "writes log for remove() method": function(test) { - this.mixpanel.set_config({debug: true}); + "writes log for remove() method": function(test) { + this.mixpanel.set_config({debug: true}); - this.mixpanel.people.remove('bob', {'browsers': 'firefox'}); + this.mixpanel.people.remove('bob', {'browsers': 'firefox'}); - test.ok( - this.consoleDebugFn.calledOnce, - `debug() method wasn't called on default logger` - ); + test.ok( + this.consoleDebugFn.calledOnce, + `debug() method wasn't called on default logger` + ); - const [message] = this.consoleDebugFn.lastCall.args; + const [message] = this.consoleDebugFn.lastCall.args; - test.ok( - message.startsWith('Sending the following data'), - 'incorrect argument was passed to debug() method' - ); + test.ok( + message.startsWith('Sending the following data'), + 'incorrect argument was passed to debug() method' + ); - test.done(); + test.done(); + }, + }, + 'custom logger': { + setUp: function(cb) { + /** + * Custom logger must be an object with the following methods: + * + * interface CustomLogger { + * trace(message?: any, ...optionalParams: any[]): void; + * debug(message?: any, ...optionalParams: any[]): void; + * info(message?: any, ...optionalParams: any[]): void; + * warn(message?: any, ...optionalParams: any[]): void; + * error(message?: any, ...optionalParams: any[]): void; + * } + */ + this.customLogger = { + trace: Sinon.stub(), + debug: Sinon.stub(), + info: Sinon.stub(), + warn: Sinon.stub(), + error: Sinon.stub(), + }; + this.consoleDebugFn = Sinon.stub(console, 'debug'); + + this.mixpanel = Mixpanel.init('test token', {logger: this.customLogger}); + + this.mixpanel.send_request = () => {}; + + cb(); + }, + + tearDown: function(next) { + this.consoleDebugFn.restore(); + + next(); + }, + + "writes log for track() method": function(test) { + this.mixpanel.set_config({debug: true}); + + this.mixpanel.track('test', {foo: 'bar'}); + + test.ok( + this.customLogger.debug.calledOnce, + `debug() method wasn't called on default logger` + ); + test.ok( + !this.consoleDebugFn.calledOnce, + `console.debug() method was called while it shouldn't` + ); + + const [message] = this.customLogger.debug.lastCall.args; + + test.ok( + message.startsWith('Sending the following event'), + 'incorrect argument was passed to debug() method' + ); + + test.done(); + }, + + "writes log for increment() method": function(test) { + this.mixpanel.set_config({debug: true}); + + this.mixpanel.people.increment('bob', 'page_views', 1); + + test.ok( + this.customLogger.debug.calledOnce, + `debug() method wasn't called on default logger` + ); + test.ok( + !this.consoleDebugFn.calledOnce, + `console.debug() method was called while it shouldn't` + ); + + const [message] = this.customLogger.debug.lastCall.args; + + test.ok( + message.startsWith('Sending the following data'), + 'incorrect argument was passed to debug() method' + ); + + test.done(); + }, + + "writes log for remove() method": function(test) { + this.mixpanel.set_config({debug: true}); + + this.mixpanel.people.remove('bob', {'browsers': 'firefox'}); + + test.ok( + this.customLogger.debug.calledOnce, + `debug() method wasn't called on default logger` + ); + test.ok( + !this.consoleDebugFn.calledOnce, + `console.debug() method was called while it shouldn't` + ); + + const [message] = this.customLogger.debug.lastCall.args; + + test.ok( + message.startsWith('Sending the following data'), + 'incorrect argument was passed to debug() method' + ); + + test.done(); + }, }, };