@@ -6,7 +6,7 @@ const LogMessage = require('../lib/LogMessage');
66
77describe ( 'LambdaLog' , function ( ) {
88 describe ( 'Constructor' , function ( ) {
9- it ( 'should have default options' , ( ) => {
9+ it ( 'should have default options' , ( ) => {
1010 let log = new LambdaLog ( ) ;
1111
1212 assert ( typeof log . options . meta === 'object' ) ;
@@ -19,7 +19,7 @@ describe('LambdaLog', function() {
1919 assert ( log . options . logHandler === console ) ;
2020 } ) ;
2121
22- it ( 'should override default options' , ( ) => {
22+ it ( 'should override default options' , ( ) => {
2323 let log = new LambdaLog ( {
2424 meta : { test : true } ,
2525 tags : [ 'test' ] ,
@@ -34,18 +34,18 @@ describe('LambdaLog', function() {
3434 } ) ;
3535
3636 describe ( 'Log Levels' , function ( ) {
37- describe ( 'Default' , ( ) => {
37+ describe ( 'Default' , ( ) => {
3838 let log = new LambdaLog ( ) ;
3939
40- it ( 'should have default log levels' , ( ) => {
40+ it ( 'should have default log levels' , ( ) => {
4141 assert ( typeof log . _logLevels === 'object' ) ;
4242 assert ( log . _logLevels . info === 'info' ) ;
4343 assert ( log . _logLevels . warn === 'warn' ) ;
4444 assert ( log . _logLevels . error === 'error' ) ;
4545 assert ( typeof log . _logLevels . debug === 'function' ) ;
4646 } ) ;
4747
48- it ( 'should have array of log levels' , ( ) => {
48+ it ( 'should have array of log levels' , ( ) => {
4949 assert ( Array . isArray ( log . _levels ) ) ;
5050 assert ( log . _levels . length === 4 ) ;
5151 } ) ;
@@ -56,7 +56,7 @@ describe('LambdaLog', function() {
5656 } ) ;
5757 } ) ;
5858
59- describe ( 'Custom' , ( ) => {
59+ describe ( 'Custom' , ( ) => {
6060 let log = new LambdaLog ( { } , {
6161 info : 'log' ,
6262 fatal : 'error' ,
@@ -115,9 +115,7 @@ describe('LambdaLog', function() {
115115 let log = new LambdaLog ( {
116116 meta : { test : true } ,
117117 dynamicMeta : function ( ) {
118- return {
119- timestamp : new Date ( ) . toISOString ( )
120- } ;
118+ return { timestamp : new Date ( ) . toISOString ( ) } ;
121119 } ,
122120 tags : [ 'test' ] ,
123121 silent : true
@@ -287,6 +285,64 @@ describe('LambdaLog', function() {
287285 assert . equal ( logData . level , 'error' ) ;
288286 } ) ;
289287 } ) ;
288+
289+ describe ( 'result()' , function ( ) {
290+ it ( 'should have result method' , function ( ) {
291+ assert . equal ( typeof log . result , 'function' ) ;
292+ } ) ;
293+
294+ it ( 'should return a promise' , function ( ) {
295+ let promise = new Promise ( resolve => resolve ( 'test' ) ) ,
296+ res = log . result ( promise ) ;
297+ assert . equal ( typeof res . then , 'function' ) ;
298+ } ) ;
299+
300+ it ( 'should throw an error when a promise is not provided' , function ( ) {
301+ assert . throws ( function ( ) {
302+ log . result ( ) ;
303+ } , function ( err ) {
304+ if ( ( err instanceof Error ) && err . message === 'A promise must be provided as the first argument' ) return true ;
305+ } ) ;
306+ } ) ;
307+
308+ it ( 'should throw an error when a non-valid promise is provided' , function ( ) {
309+ assert . throws ( function ( ) {
310+ log . result ( { } ) ;
311+ } , function ( err ) {
312+ if ( ( err instanceof Error ) && err . message === 'A promise must be provided as the first argument' ) return true ;
313+ } ) ;
314+ } ) ;
315+
316+ it ( 'should return a promise the resolves with log message' , function ( done ) {
317+ let promise = new Promise ( resolve => resolve ( 'test' ) ) ,
318+ res = log . result ( promise ) ;
319+
320+ res . then ( logData => {
321+ assert . equal ( logData . msg , 'test' ) ;
322+ done ( ) ;
323+ } ) ;
324+ } ) ;
325+
326+ it ( 'should set the level to info on resolve' , function ( done ) {
327+ let promise = new Promise ( resolve => resolve ( 'test' ) ) ,
328+ res = log . result ( promise ) ;
329+
330+ res . then ( logData => {
331+ assert . equal ( logData . level , 'info' ) ;
332+ done ( ) ;
333+ } ) ;
334+ } ) ;
335+
336+ it ( 'should set the level to error on rejection' , function ( done ) {
337+ let promise = new Promise ( ( resolve , reject ) => reject ( 'test' ) ) ,
338+ res = log . result ( promise ) ;
339+
340+ res . then ( logData => {
341+ assert . equal ( logData . level , 'error' ) ;
342+ done ( ) ;
343+ } ) ;
344+ } ) ;
345+ } ) ;
290346 } ) ;
291347
292348 describe ( 'Events' , function ( ) {
0 commit comments