1- /**
2- * Test Logger formatter
3- *
4- * @group unit/logger/logFormatter
5- */
61import { AssertionError } from 'node:assert' ;
7- import {
8- afterAll ,
9- beforeEach ,
10- describe ,
11- expect ,
12- it ,
13- jest ,
14- } from '@jest/globals' ;
2+ import { afterAll , beforeEach , describe , expect , it , vi } from 'vitest' ;
153import { EnvironmentVariablesService } from '../../src/config/EnvironmentVariablesService.js' ;
164import { PowertoolsLogFormatter } from '../../src/formatter/PowertoolsLogFormatter.js' ;
175import {
@@ -73,8 +61,6 @@ const unformattedAttributes: UnformattedAttributes = {
7361
7462process . env . POWERTOOLS_DEV = 'true' ;
7563
76- const logSpy = jest . spyOn ( console , 'info' ) ;
77-
7864const logger = new Logger ( ) ;
7965
8066const jsonReplacerFn : CustomJsonReplacerFn = ( _ : string , value : unknown ) =>
@@ -120,14 +106,13 @@ describe('Formatters', () => {
120106 beforeEach ( ( ) => {
121107 process . env = { ...ENVIRONMENT_VARIABLES } ;
122108 const mockDate = new Date ( 1466424490000 ) ;
123- jest . useFakeTimers ( ) . setSystemTime ( mockDate ) ;
124- jest . resetAllMocks ( ) ;
109+ vi . useFakeTimers ( ) . setSystemTime ( mockDate ) ;
110+ vi . resetAllMocks ( ) ;
125111 unformattedAttributes . timestamp = mockDate ;
126112 } ) ;
127113
128114 afterAll ( ( ) => {
129- jest . useRealTimers ( ) ;
130- unformattedAttributes . timestamp = new Date ( ) ;
115+ vi . useRealTimers ( ) ;
131116 } ) ;
132117
133118 // #region base log keys
@@ -477,7 +462,7 @@ describe('Formatters', () => {
477462 Difference between UTC and `America/New_York`(GMT -04.00) is 240 minutes.
478463 The positive value indicates that `America/New_York` is behind UTC.
479464 */
480- jest . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( 240 ) ;
465+ vi . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( 240 ) ;
481466
482467 // Act
483468 const timestamp = formatterWithEnv . formatTimestamp ( new Date ( ) ) ;
@@ -493,7 +478,7 @@ describe('Formatters', () => {
493478 Difference between UTC and `America/New_York`(GMT -04.00) is 240 minutes.
494479 The positive value indicates that `America/New_York` is behind UTC.
495480 */
496- jest . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( 240 ) ;
481+ vi . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( 240 ) ;
497482
498483 // Act
499484 const timestamp = formatterWithEnv . formatTimestamp ( new Date ( ) ) ;
@@ -509,7 +494,7 @@ describe('Formatters', () => {
509494 Difference between UTC and `America/New_York`(GMT -04.00) is 240 minutes.
510495 The positive value indicates that `America/New_York` is behind UTC.
511496 */
512- jest . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( 240 ) ;
497+ vi . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( 240 ) ;
513498
514499 // Act
515500 const timestamp = formatterWithEnv . formatTimestamp ( new Date ( ) ) ;
@@ -521,12 +506,12 @@ describe('Formatters', () => {
521506 it ( 'it formats the timestamp to ISO 8601 with correct milliseconds for `Asia/Dhaka` timezone' , ( ) => {
522507 // Prepare
523508 process . env . TZ = 'Asia/Dhaka' ;
524- jest . useFakeTimers ( ) . setSystemTime ( new Date ( '2016-06-20T12:08:10.910Z' ) ) ;
509+ vi . setSystemTime ( new Date ( '2016-06-20T12:08:10.910Z' ) ) ;
525510 /*
526511 Difference between UTC and `Asia/Dhaka`(GMT +06.00) is 360 minutes.
527512 The negative value indicates that `Asia/Dhaka` is ahead of UTC.
528513 */
529- jest . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( - 360 ) ;
514+ vi . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( - 360 ) ;
530515 const formatter = new PowertoolsLogFormatter ( {
531516 envVarsService : new EnvironmentVariablesService ( ) ,
532517 } ) ;
@@ -542,12 +527,12 @@ describe('Formatters', () => {
542527 // Prepare
543528 process . env . TZ = 'Asia/Dhaka' ;
544529 const mockDate = new Date ( '2016-06-20T20:08:10.910Z' ) ;
545- jest . useFakeTimers ( ) . setSystemTime ( mockDate ) ;
530+ vi . setSystemTime ( mockDate ) ;
546531 /*
547532 Difference between UTC and `Asia/Dhaka`(GMT +06.00) is 360 minutes.
548533 The negative value indicates that `Asia/Dhaka` is ahead of UTC.
549534 */
550- jest . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( - 360 ) ;
535+ vi . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( - 360 ) ;
551536 const formatter = new PowertoolsLogFormatter ( {
552537 envVarsService : new EnvironmentVariablesService ( ) ,
553538 } ) ;
@@ -566,7 +551,7 @@ describe('Formatters', () => {
566551 Difference between UTC and `Asia/Dhaka`(GMT +06.00) is 360 minutes.
567552 The negative value indicates that `Asia/Dhaka` is ahead of UTC.
568553 */
569- jest . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( - 360 ) ;
554+ vi . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( - 360 ) ;
570555 const formatter = new PowertoolsLogFormatter ( ) ;
571556
572557 // Act
@@ -579,7 +564,7 @@ describe('Formatters', () => {
579564 it ( 'defaults to :UTC when the TZ env variable is set to :/etc/localtime' , ( ) => {
580565 // Prepare
581566 process . env . TZ = ':/etc/localtime' ;
582- jest . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( 0 ) ;
567+ vi . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( 0 ) ;
583568 const formatter = new PowertoolsLogFormatter ( {
584569 envVarsService : new EnvironmentVariablesService ( ) ,
585570 } ) ;
@@ -676,8 +661,9 @@ describe('Formatters', () => {
676661 logger . info ( 'foo' , { circularObject } ) ;
677662
678663 // Assess
679- expect ( logSpy ) . toHaveBeenCalledTimes ( 1 ) ;
680- expect ( JSON . parse ( logSpy . mock . calls [ 0 ] [ 0 ] ) ) . toEqual (
664+ expect ( console . info ) . toHaveBeenCalledTimes ( 1 ) ;
665+ expect ( console . info ) . toHaveLoggedNth (
666+ 1 ,
681667 expect . objectContaining ( {
682668 level : 'INFO' ,
683669 message : 'foo' ,
@@ -696,8 +682,9 @@ describe('Formatters', () => {
696682 logger . info ( 'foo' , bigIntValue ) ;
697683
698684 // Assess
699- expect ( logSpy ) . toHaveBeenCalledTimes ( 1 ) ;
700- expect ( JSON . parse ( logSpy . mock . calls [ 0 ] [ 0 ] ) ) . toEqual (
685+ expect ( console . info ) . toHaveBeenCalledTimes ( 1 ) ;
686+ expect ( console . info ) . toHaveLoggedNth (
687+ 1 ,
701688 expect . objectContaining ( {
702689 level : 'INFO' ,
703690 message : 'foo' ,
@@ -720,8 +707,9 @@ describe('Formatters', () => {
720707 logger . info ( 'foo' , values ) ;
721708
722709 // Assess
723- expect ( logSpy ) . toHaveBeenCalledTimes ( 1 ) ;
724- expect ( JSON . parse ( logSpy . mock . calls [ 0 ] [ 0 ] ) ) . toEqual (
710+ expect ( console . info ) . toHaveBeenCalledTimes ( 1 ) ;
711+ expect ( console . info ) . toHaveLoggedNth (
712+ 1 ,
725713 expect . objectContaining ( {
726714 level : 'INFO' ,
727715 message : 'foo' ,
@@ -738,8 +726,9 @@ describe('Formatters', () => {
738726 loggerWithReplacer . info ( 'foo' , valueWithSet ) ;
739727
740728 // Assess
741- expect ( logSpy ) . toHaveBeenCalledTimes ( 1 ) ;
742- expect ( JSON . parse ( logSpy . mock . calls [ 0 ] [ 0 ] ) ) . toEqual (
729+ expect ( console . info ) . toHaveBeenCalledTimes ( 1 ) ;
730+ expect ( console . info ) . toHaveLoggedNth (
731+ 1 ,
743732 expect . objectContaining ( {
744733 level : 'INFO' ,
745734 message : 'foo' ,
@@ -759,8 +748,9 @@ describe('Formatters', () => {
759748 loggerWithReplacer . info ( 'foo' , valueWithSetAndBigInt ) ;
760749
761750 // Assess
762- expect ( logSpy ) . toHaveBeenCalledTimes ( 1 ) ;
763- expect ( JSON . parse ( logSpy . mock . calls [ 0 ] [ 0 ] ) ) . toEqual (
751+ expect ( console . info ) . toHaveBeenCalledTimes ( 1 ) ;
752+ expect ( console . info ) . toHaveLoggedNth (
753+ 1 ,
764754 expect . objectContaining ( {
765755 level : 'INFO' ,
766756 message : 'foo' ,
@@ -778,8 +768,9 @@ describe('Formatters', () => {
778768 childLogger . info ( 'foo' , { foo : new Set ( [ 1 , 2 ] ) } ) ;
779769
780770 // Assess
781- expect ( logSpy ) . toHaveBeenCalledTimes ( 1 ) ;
782- expect ( JSON . parse ( logSpy . mock . calls [ 0 ] [ 0 ] ) ) . toEqual (
771+ expect ( console . info ) . toHaveBeenCalledTimes ( 1 ) ;
772+ expect ( console . info ) . toHaveLoggedNth (
773+ 1 ,
783774 expect . objectContaining ( {
784775 level : 'INFO' ,
785776 message : 'foo' ,
@@ -795,8 +786,8 @@ describe('Formatters', () => {
795786 loggerWithCustomLogFormatter . info ( 'foo' ) ;
796787
797788 // Assess
798- expect ( logSpy ) . toHaveBeenCalledTimes ( 1 ) ;
799- expect ( JSON . parse ( logSpy . mock . calls [ 0 ] [ 0 ] ) ) . toEqual ( {
789+ expect ( console . info ) . toHaveBeenCalledTimes ( 1 ) ;
790+ expect ( console . info ) . toHaveLoggedNth ( 1 , {
800791 logLevel : 12 ,
801792 message : 'foo' ,
802793 timestamp : expect . any ( String ) ,
@@ -811,8 +802,8 @@ describe('Formatters', () => {
811802 childLogger . info ( 'foo' ) ;
812803
813804 // Assess
814- expect ( logSpy ) . toHaveBeenCalledTimes ( 1 ) ;
815- expect ( JSON . parse ( logSpy . mock . calls [ 0 ] [ 0 ] ) ) . toEqual ( {
805+ expect ( console . info ) . toHaveBeenCalledTimes ( 1 ) ;
806+ expect ( console . info ) . toHaveLoggedNth ( 1 , {
816807 logLevel : 12 ,
817808 message : 'foo' ,
818809 timestamp : expect . any ( String ) ,
0 commit comments