1
1
import { AssertionError } from 'node:assert' ;
2
- import { afterAll , beforeEach , describe , expect , it , vi } from 'vitest' ;
2
+ import {
3
+ afterAll ,
4
+ afterEach ,
5
+ beforeEach ,
6
+ describe ,
7
+ expect ,
8
+ it ,
9
+ vi ,
10
+ } from 'vitest' ;
3
11
import { PowertoolsLogFormatter } from '../../src/formatter/PowertoolsLogFormatter.js' ;
4
12
import {
5
13
LogFormatter ,
@@ -53,13 +61,8 @@ const unformattedAttributes: UnformattedAttributes = {
53
61
} ,
54
62
} ;
55
63
56
- process . env . POWERTOOLS_DEV = 'true' ;
57
-
58
- const logger = new Logger ( ) ;
59
-
60
64
const jsonReplacerFn : CustomJsonReplacerFn = ( _ : string , value : unknown ) =>
61
65
value instanceof Set ? [ ...value ] : value ;
62
- const loggerWithReplacer = new Logger ( { jsonReplacerFn } ) ;
63
66
64
67
/**
65
68
* A custom log formatter that formats logs using only the message, log level as a number, and timestamp.
@@ -90,15 +93,19 @@ class CustomFormatter extends LogFormatter {
90
93
) ;
91
94
}
92
95
}
93
- const loggerWithCustomLogFormatter = new Logger ( {
94
- logFormatter : new CustomFormatter ( ) ,
95
- } ) ;
96
96
97
97
describe ( 'Formatters' , ( ) => {
98
- const ENVIRONMENT_VARIABLES = process . env ;
98
+ // Ensure dev mode is on for logger initialization
99
+ vi . stubEnv ( 'POWERTOOLS_DEV' , 'true' ) ;
100
+ const logger = new Logger ( ) ;
101
+ const loggerWithReplacer = new Logger ( { jsonReplacerFn } ) ;
102
+ const loggerWithCustomLogFormatter = new Logger ( {
103
+ logFormatter : new CustomFormatter ( ) ,
104
+ } ) ;
105
+ vi . unstubAllEnvs ( ) ;
99
106
100
107
beforeEach ( ( ) => {
101
- process . env = { ... ENVIRONMENT_VARIABLES } ;
108
+ vi . stubEnv ( 'POWERTOOLS_DEV' , 'true' ) ;
102
109
const mockDate = new Date ( 1466424490000 ) ;
103
110
vi . useFakeTimers ( ) . setSystemTime ( mockDate ) ;
104
111
vi . clearAllMocks ( ) ;
@@ -109,6 +116,10 @@ describe('Formatters', () => {
109
116
vi . useRealTimers ( ) ;
110
117
} ) ;
111
118
119
+ afterEach ( ( ) => {
120
+ vi . unstubAllEnvs ( ) ;
121
+ } ) ;
122
+
112
123
// #region base log keys
113
124
114
125
it ( 'formats the base log keys' , ( ) => {
@@ -451,8 +462,7 @@ describe('Formatters', () => {
451
462
452
463
it ( 'formats stack as string when not in dev mode' , ( ) => {
453
464
// Prepare
454
- const originalDevMode = process . env . POWERTOOLS_DEV ;
455
- delete process . env . POWERTOOLS_DEV ; // Ensure dev mode is off
465
+ vi . stubEnv ( 'POWERTOOLS_DEV' , 'false' ) ; // Ensure dev mode is off
456
466
457
467
const error = new Error ( 'Test error' ) ;
458
468
const formatter = new PowertoolsLogFormatter ( ) ;
@@ -463,9 +473,6 @@ describe('Formatters', () => {
463
473
// Assess
464
474
expect ( formattedError . stack ) . toEqual ( expect . any ( String ) ) ;
465
475
expect ( Array . isArray ( formattedError . stack ) ) . toBe ( false ) ;
466
-
467
- // Cleanup
468
- process . env . POWERTOOLS_DEV = originalDevMode ;
469
476
} ) ;
470
477
471
478
it ( 'formats custom errors by including only enumerable properties' , ( ) => {
@@ -528,7 +535,7 @@ describe('Formatters', () => {
528
535
529
536
it ( 'formats the timestamp to ISO 8601, accounting for the `America/New_York` timezone offset' , ( ) => {
530
537
// Prepare
531
- process . env . TZ = 'America/New_York' ;
538
+ vi . stubEnv ( 'TZ' , 'America/New_York' ) ;
532
539
/*
533
540
Difference between UTC and `America/New_York`(GMT -04.00) is 240 minutes.
534
541
The positive value indicates that `America/New_York` is behind UTC.
@@ -544,7 +551,7 @@ describe('Formatters', () => {
544
551
545
552
it ( 'formats the timestamp to ISO 8601 with correct milliseconds for `America/New_York` timezone' , ( ) => {
546
553
// Prepare
547
- process . env . TZ = 'America/New_York' ;
554
+ vi . stubEnv ( 'TZ' , 'America/New_York' ) ;
548
555
/*
549
556
Difference between UTC and `America/New_York`(GMT -04.00) is 240 minutes.
550
557
The positive value indicates that `America/New_York` is behind UTC.
@@ -560,7 +567,7 @@ describe('Formatters', () => {
560
567
561
568
it ( 'formats the timestamp to ISO 8601, adjusting for `America/New_York` timezone, preserving milliseconds and accounting for date change' , ( ) => {
562
569
// Prepare
563
- process . env . TZ = 'America/New_York' ;
570
+ vi . stubEnv ( 'TZ' , 'America/New_York' ) ;
564
571
/*
565
572
Difference between UTC and `America/New_York`(GMT -04.00) is 240 minutes.
566
573
The positive value indicates that `America/New_York` is behind UTC.
@@ -576,7 +583,7 @@ describe('Formatters', () => {
576
583
577
584
it ( 'it formats the timestamp to ISO 8601 with correct milliseconds for `Asia/Dhaka` timezone' , ( ) => {
578
585
// Prepare
579
- process . env . TZ = 'Asia/Dhaka' ;
586
+ vi . stubEnv ( 'TZ' , 'Asia/Dhaka' ) ;
580
587
vi . setSystemTime ( new Date ( '2016-06-20T12:08:10.910Z' ) ) ;
581
588
/*
582
589
Difference between UTC and `Asia/Dhaka`(GMT +06.00) is 360 minutes.
@@ -594,7 +601,7 @@ describe('Formatters', () => {
594
601
595
602
it ( 'formats the timestamp to ISO 8601, adjusting for `Asia/Dhaka` timezone, preserving milliseconds and accounting for date change' , ( ) => {
596
603
// Prepare
597
- process . env . TZ = 'Asia/Dhaka' ;
604
+ vi . stubEnv ( 'TZ' , 'Asia/Dhaka' ) ;
598
605
const mockDate = new Date ( '2016-06-20T20:08:10.910Z' ) ;
599
606
vi . setSystemTime ( mockDate ) ;
600
607
/*
@@ -613,7 +620,7 @@ describe('Formatters', () => {
613
620
614
621
it ( 'returns defaults to :UTC when an env variable service is not set' , ( ) => {
615
622
// Prepare
616
- process . env . TZ = undefined ;
623
+ vi . stubEnv ( 'TZ' , undefined ) ;
617
624
618
625
vi . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( - 360 ) ;
619
626
const formatter = new PowertoolsLogFormatter ( ) ;
@@ -627,7 +634,7 @@ describe('Formatters', () => {
627
634
628
635
it ( 'defaults to :UTC when the TZ env variable is set to :/etc/localtime' , ( ) => {
629
636
// Prepare
630
- process . env . TZ = ':/etc/localtime' ;
637
+ vi . stubEnv ( 'TZ' , ':/etc/localtime' ) ;
631
638
vi . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( 0 ) ;
632
639
const formatter = new PowertoolsLogFormatter ( ) ;
633
640
0 commit comments