1
- /**
2
- * Test Logger formatter
3
- *
4
- * @group unit/logger/logFormatter
5
- */
6
1
import { 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' ;
15
3
import { EnvironmentVariablesService } from '../../src/config/EnvironmentVariablesService.js' ;
16
4
import { PowertoolsLogFormatter } from '../../src/formatter/PowertoolsLogFormatter.js' ;
17
5
import {
@@ -73,8 +61,6 @@ const unformattedAttributes: UnformattedAttributes = {
73
61
74
62
process . env . POWERTOOLS_DEV = 'true' ;
75
63
76
- const logSpy = jest . spyOn ( console , 'info' ) ;
77
-
78
64
const logger = new Logger ( ) ;
79
65
80
66
const jsonReplacerFn : CustomJsonReplacerFn = ( _ : string , value : unknown ) =>
@@ -120,14 +106,13 @@ describe('Formatters', () => {
120
106
beforeEach ( ( ) => {
121
107
process . env = { ...ENVIRONMENT_VARIABLES } ;
122
108
const mockDate = new Date ( 1466424490000 ) ;
123
- jest . useFakeTimers ( ) . setSystemTime ( mockDate ) ;
124
- jest . resetAllMocks ( ) ;
109
+ vi . useFakeTimers ( ) . setSystemTime ( mockDate ) ;
110
+ vi . resetAllMocks ( ) ;
125
111
unformattedAttributes . timestamp = mockDate ;
126
112
} ) ;
127
113
128
114
afterAll ( ( ) => {
129
- jest . useRealTimers ( ) ;
130
- unformattedAttributes . timestamp = new Date ( ) ;
115
+ vi . useRealTimers ( ) ;
131
116
} ) ;
132
117
133
118
// #region base log keys
@@ -477,7 +462,7 @@ describe('Formatters', () => {
477
462
Difference between UTC and `America/New_York`(GMT -04.00) is 240 minutes.
478
463
The positive value indicates that `America/New_York` is behind UTC.
479
464
*/
480
- jest . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( 240 ) ;
465
+ vi . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( 240 ) ;
481
466
482
467
// Act
483
468
const timestamp = formatterWithEnv . formatTimestamp ( new Date ( ) ) ;
@@ -493,7 +478,7 @@ describe('Formatters', () => {
493
478
Difference between UTC and `America/New_York`(GMT -04.00) is 240 minutes.
494
479
The positive value indicates that `America/New_York` is behind UTC.
495
480
*/
496
- jest . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( 240 ) ;
481
+ vi . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( 240 ) ;
497
482
498
483
// Act
499
484
const timestamp = formatterWithEnv . formatTimestamp ( new Date ( ) ) ;
@@ -509,7 +494,7 @@ describe('Formatters', () => {
509
494
Difference between UTC and `America/New_York`(GMT -04.00) is 240 minutes.
510
495
The positive value indicates that `America/New_York` is behind UTC.
511
496
*/
512
- jest . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( 240 ) ;
497
+ vi . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( 240 ) ;
513
498
514
499
// Act
515
500
const timestamp = formatterWithEnv . formatTimestamp ( new Date ( ) ) ;
@@ -521,12 +506,12 @@ describe('Formatters', () => {
521
506
it ( 'it formats the timestamp to ISO 8601 with correct milliseconds for `Asia/Dhaka` timezone' , ( ) => {
522
507
// Prepare
523
508
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' ) ) ;
525
510
/*
526
511
Difference between UTC and `Asia/Dhaka`(GMT +06.00) is 360 minutes.
527
512
The negative value indicates that `Asia/Dhaka` is ahead of UTC.
528
513
*/
529
- jest . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( - 360 ) ;
514
+ vi . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( - 360 ) ;
530
515
const formatter = new PowertoolsLogFormatter ( {
531
516
envVarsService : new EnvironmentVariablesService ( ) ,
532
517
} ) ;
@@ -542,12 +527,12 @@ describe('Formatters', () => {
542
527
// Prepare
543
528
process . env . TZ = 'Asia/Dhaka' ;
544
529
const mockDate = new Date ( '2016-06-20T20:08:10.910Z' ) ;
545
- jest . useFakeTimers ( ) . setSystemTime ( mockDate ) ;
530
+ vi . setSystemTime ( mockDate ) ;
546
531
/*
547
532
Difference between UTC and `Asia/Dhaka`(GMT +06.00) is 360 minutes.
548
533
The negative value indicates that `Asia/Dhaka` is ahead of UTC.
549
534
*/
550
- jest . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( - 360 ) ;
535
+ vi . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( - 360 ) ;
551
536
const formatter = new PowertoolsLogFormatter ( {
552
537
envVarsService : new EnvironmentVariablesService ( ) ,
553
538
} ) ;
@@ -566,7 +551,7 @@ describe('Formatters', () => {
566
551
Difference between UTC and `Asia/Dhaka`(GMT +06.00) is 360 minutes.
567
552
The negative value indicates that `Asia/Dhaka` is ahead of UTC.
568
553
*/
569
- jest . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( - 360 ) ;
554
+ vi . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( - 360 ) ;
570
555
const formatter = new PowertoolsLogFormatter ( ) ;
571
556
572
557
// Act
@@ -579,7 +564,7 @@ describe('Formatters', () => {
579
564
it ( 'defaults to :UTC when the TZ env variable is set to :/etc/localtime' , ( ) => {
580
565
// Prepare
581
566
process . env . TZ = ':/etc/localtime' ;
582
- jest . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( 0 ) ;
567
+ vi . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( 0 ) ;
583
568
const formatter = new PowertoolsLogFormatter ( {
584
569
envVarsService : new EnvironmentVariablesService ( ) ,
585
570
} ) ;
@@ -676,8 +661,9 @@ describe('Formatters', () => {
676
661
logger . info ( 'foo' , { circularObject } ) ;
677
662
678
663
// 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 ,
681
667
expect . objectContaining ( {
682
668
level : 'INFO' ,
683
669
message : 'foo' ,
@@ -696,8 +682,9 @@ describe('Formatters', () => {
696
682
logger . info ( 'foo' , bigIntValue ) ;
697
683
698
684
// 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 ,
701
688
expect . objectContaining ( {
702
689
level : 'INFO' ,
703
690
message : 'foo' ,
@@ -720,8 +707,9 @@ describe('Formatters', () => {
720
707
logger . info ( 'foo' , values ) ;
721
708
722
709
// 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 ,
725
713
expect . objectContaining ( {
726
714
level : 'INFO' ,
727
715
message : 'foo' ,
@@ -738,8 +726,9 @@ describe('Formatters', () => {
738
726
loggerWithReplacer . info ( 'foo' , valueWithSet ) ;
739
727
740
728
// 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 ,
743
732
expect . objectContaining ( {
744
733
level : 'INFO' ,
745
734
message : 'foo' ,
@@ -759,8 +748,9 @@ describe('Formatters', () => {
759
748
loggerWithReplacer . info ( 'foo' , valueWithSetAndBigInt ) ;
760
749
761
750
// 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 ,
764
754
expect . objectContaining ( {
765
755
level : 'INFO' ,
766
756
message : 'foo' ,
@@ -778,8 +768,9 @@ describe('Formatters', () => {
778
768
childLogger . info ( 'foo' , { foo : new Set ( [ 1 , 2 ] ) } ) ;
779
769
780
770
// 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 ,
783
774
expect . objectContaining ( {
784
775
level : 'INFO' ,
785
776
message : 'foo' ,
@@ -795,8 +786,8 @@ describe('Formatters', () => {
795
786
loggerWithCustomLogFormatter . info ( 'foo' ) ;
796
787
797
788
// 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 , {
800
791
logLevel : 12 ,
801
792
message : 'foo' ,
802
793
timestamp : expect . any ( String ) ,
@@ -811,8 +802,8 @@ describe('Formatters', () => {
811
802
childLogger . info ( 'foo' ) ;
812
803
813
804
// 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 , {
816
807
logLevel : 12 ,
817
808
message : 'foo' ,
818
809
timestamp : expect . any ( String ) ,
0 commit comments