11import { 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' ;
311import { PowertoolsLogFormatter } from '../../src/formatter/PowertoolsLogFormatter.js' ;
412import {
513 LogFormatter ,
@@ -53,13 +61,8 @@ const unformattedAttributes: UnformattedAttributes = {
5361 } ,
5462} ;
5563
56- process . env . POWERTOOLS_DEV = 'true' ;
57-
58- const logger = new Logger ( ) ;
59-
6064const jsonReplacerFn : CustomJsonReplacerFn = ( _ : string , value : unknown ) =>
6165 value instanceof Set ? [ ...value ] : value ;
62- const loggerWithReplacer = new Logger ( { jsonReplacerFn } ) ;
6366
6467/**
6568 * 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 {
9093 ) ;
9194 }
9295}
93- const loggerWithCustomLogFormatter = new Logger ( {
94- logFormatter : new CustomFormatter ( ) ,
95- } ) ;
9696
9797describe ( '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 ( ) ;
99106
100107 beforeEach ( ( ) => {
101- process . env = { ... ENVIRONMENT_VARIABLES } ;
108+ vi . stubEnv ( 'POWERTOOLS_DEV' , 'true' ) ;
102109 const mockDate = new Date ( 1466424490000 ) ;
103110 vi . useFakeTimers ( ) . setSystemTime ( mockDate ) ;
104111 vi . clearAllMocks ( ) ;
@@ -109,6 +116,10 @@ describe('Formatters', () => {
109116 vi . useRealTimers ( ) ;
110117 } ) ;
111118
119+ afterEach ( ( ) => {
120+ vi . unstubAllEnvs ( ) ;
121+ } ) ;
122+
112123 // #region base log keys
113124
114125 it ( 'formats the base log keys' , ( ) => {
@@ -451,8 +462,7 @@ describe('Formatters', () => {
451462
452463 it ( 'formats stack as string when not in dev mode' , ( ) => {
453464 // 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
456466
457467 const error = new Error ( 'Test error' ) ;
458468 const formatter = new PowertoolsLogFormatter ( ) ;
@@ -463,9 +473,6 @@ describe('Formatters', () => {
463473 // Assess
464474 expect ( formattedError . stack ) . toEqual ( expect . any ( String ) ) ;
465475 expect ( Array . isArray ( formattedError . stack ) ) . toBe ( false ) ;
466-
467- // Cleanup
468- process . env . POWERTOOLS_DEV = originalDevMode ;
469476 } ) ;
470477
471478 it ( 'formats custom errors by including only enumerable properties' , ( ) => {
@@ -528,7 +535,7 @@ describe('Formatters', () => {
528535
529536 it ( 'formats the timestamp to ISO 8601, accounting for the `America/New_York` timezone offset' , ( ) => {
530537 // Prepare
531- process . env . TZ = 'America/New_York' ;
538+ vi . stubEnv ( 'TZ' , 'America/New_York' ) ;
532539 /*
533540 Difference between UTC and `America/New_York`(GMT -04.00) is 240 minutes.
534541 The positive value indicates that `America/New_York` is behind UTC.
@@ -544,7 +551,7 @@ describe('Formatters', () => {
544551
545552 it ( 'formats the timestamp to ISO 8601 with correct milliseconds for `America/New_York` timezone' , ( ) => {
546553 // Prepare
547- process . env . TZ = 'America/New_York' ;
554+ vi . stubEnv ( 'TZ' , 'America/New_York' ) ;
548555 /*
549556 Difference between UTC and `America/New_York`(GMT -04.00) is 240 minutes.
550557 The positive value indicates that `America/New_York` is behind UTC.
@@ -560,7 +567,7 @@ describe('Formatters', () => {
560567
561568 it ( 'formats the timestamp to ISO 8601, adjusting for `America/New_York` timezone, preserving milliseconds and accounting for date change' , ( ) => {
562569 // Prepare
563- process . env . TZ = 'America/New_York' ;
570+ vi . stubEnv ( 'TZ' , 'America/New_York' ) ;
564571 /*
565572 Difference between UTC and `America/New_York`(GMT -04.00) is 240 minutes.
566573 The positive value indicates that `America/New_York` is behind UTC.
@@ -576,7 +583,7 @@ describe('Formatters', () => {
576583
577584 it ( 'it formats the timestamp to ISO 8601 with correct milliseconds for `Asia/Dhaka` timezone' , ( ) => {
578585 // Prepare
579- process . env . TZ = 'Asia/Dhaka' ;
586+ vi . stubEnv ( 'TZ' , 'Asia/Dhaka' ) ;
580587 vi . setSystemTime ( new Date ( '2016-06-20T12:08:10.910Z' ) ) ;
581588 /*
582589 Difference between UTC and `Asia/Dhaka`(GMT +06.00) is 360 minutes.
@@ -594,7 +601,7 @@ describe('Formatters', () => {
594601
595602 it ( 'formats the timestamp to ISO 8601, adjusting for `Asia/Dhaka` timezone, preserving milliseconds and accounting for date change' , ( ) => {
596603 // Prepare
597- process . env . TZ = 'Asia/Dhaka' ;
604+ vi . stubEnv ( 'TZ' , 'Asia/Dhaka' ) ;
598605 const mockDate = new Date ( '2016-06-20T20:08:10.910Z' ) ;
599606 vi . setSystemTime ( mockDate ) ;
600607 /*
@@ -613,7 +620,7 @@ describe('Formatters', () => {
613620
614621 it ( 'returns defaults to :UTC when an env variable service is not set' , ( ) => {
615622 // Prepare
616- process . env . TZ = undefined ;
623+ vi . stubEnv ( 'TZ' , undefined ) ;
617624
618625 vi . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( - 360 ) ;
619626 const formatter = new PowertoolsLogFormatter ( ) ;
@@ -627,7 +634,7 @@ describe('Formatters', () => {
627634
628635 it ( 'defaults to :UTC when the TZ env variable is set to :/etc/localtime' , ( ) => {
629636 // Prepare
630- process . env . TZ = ':/etc/localtime' ;
637+ vi . stubEnv ( 'TZ' , ':/etc/localtime' ) ;
631638 vi . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( 0 ) ;
632639 const formatter = new PowertoolsLogFormatter ( ) ;
633640
0 commit comments