@@ -8,13 +8,20 @@ import { PowertoolsLogFormatter } from '../../../src/formatter/PowertoolsLogForm
8
8
import { LogItem } from '../../../src/index.js' ;
9
9
import type { UnformattedAttributes } from '../../../src/types/Logger.js' ;
10
10
import type { LogAttributes } from '../../../src/types/Log.js' ;
11
+ import { EnvironmentVariablesService } from '../../../src/config/EnvironmentVariablesService.js' ;
11
12
12
13
describe ( 'Class: PowertoolsLogFormatter' , ( ) => {
13
- const mockDate = new Date ( 1466424490000 ) ;
14
- const dateSpy = jest . spyOn ( global , 'Date' ) . mockImplementation ( ( ) => mockDate ) ;
14
+ const ENVIRONMENT_VARIABLES = process . env ;
15
15
16
16
beforeEach ( ( ) => {
17
- dateSpy . mockClear ( ) ;
17
+ const mockDate = new Date ( 1466424490000 ) ;
18
+ jest . useFakeTimers ( ) . setSystemTime ( mockDate ) ;
19
+ process . env = { ...ENVIRONMENT_VARIABLES } ;
20
+ } ) ;
21
+
22
+ afterAll ( ( ) => {
23
+ process . env = ENVIRONMENT_VARIABLES ;
24
+ jest . useRealTimers ( ) ;
18
25
} ) ;
19
26
20
27
describe ( 'Method: formatAttributes' , ( ) => {
@@ -309,6 +316,161 @@ describe('Class: PowertoolsLogFormatter', () => {
309
316
// Assess
310
317
expect ( timestamp ) . toEqual ( '2016-06-20T12:08:10.000Z' ) ;
311
318
} ) ;
319
+
320
+ test ( 'it formats the timestamp to ISO 8601, accounting for the `America/New_York` timezone offset' , ( ) => {
321
+ // Prepare
322
+ process . env . TZ = 'America/New_York' ;
323
+ /*
324
+ Difference between UTC and `America/New_York`(GMT -04.00) is 240 minutes.
325
+ The positive value indicates that `America/New_York` is behind UTC.
326
+ */
327
+ jest . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( 240 ) ;
328
+ const formatter = new PowertoolsLogFormatter ( {
329
+ envVarsService : new EnvironmentVariablesService ( ) ,
330
+ } ) ;
331
+
332
+ // Act
333
+ const timestamp = formatter . formatTimestamp ( new Date ( ) ) ;
334
+
335
+ // Assess
336
+ expect ( timestamp ) . toEqual ( '2016-06-20T08:08:10.000-04:00' ) ;
337
+ } ) ;
338
+
339
+ test ( 'it formats the timestamp to ISO 8601 with correct milliseconds for `America/New_York` timezone' , ( ) => {
340
+ // Prepare
341
+ process . env . TZ = 'America/New_York' ;
342
+ const mockDate = new Date ( '2016-06-20T12:08:10.910Z' ) ;
343
+ jest . useFakeTimers ( ) . setSystemTime ( mockDate ) ;
344
+ /*
345
+ Difference between UTC and `America/New_York`(GMT -04.00) is 240 minutes.
346
+ The positive value indicates that `America/New_York` is behind UTC.
347
+ */
348
+ jest . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( 240 ) ;
349
+ const formatter = new PowertoolsLogFormatter ( {
350
+ envVarsService : new EnvironmentVariablesService ( ) ,
351
+ } ) ;
352
+
353
+ // Act
354
+ const timestamp = formatter . formatTimestamp ( new Date ( ) ) ;
355
+
356
+ // Assess
357
+ expect ( timestamp ) . toEqual ( '2016-06-20T08:08:10.910-04:00' ) ;
358
+ } ) ;
359
+
360
+ test ( 'it formats the timestamp to ISO 8601, adjusting for `America/New_York` timezone, preserving milliseconds and accounting for date change' , ( ) => {
361
+ // Prepare
362
+ process . env . TZ = 'America/New_York' ;
363
+ const mockDate = new Date ( '2016-06-20T00:08:10.910Z' ) ;
364
+ jest . useFakeTimers ( ) . setSystemTime ( mockDate ) ;
365
+ /*
366
+ Difference between UTC and `America/New_York`(GMT -04.00) is 240 minutes.
367
+ The positive value indicates that `America/New_York` is behind UTC.
368
+ */
369
+ jest . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( 240 ) ;
370
+ const formatter = new PowertoolsLogFormatter ( {
371
+ envVarsService : new EnvironmentVariablesService ( ) ,
372
+ } ) ;
373
+
374
+ // Act
375
+ const timestamp = formatter . formatTimestamp ( new Date ( ) ) ;
376
+
377
+ // Assess
378
+ expect ( timestamp ) . toEqual ( '2016-06-19T20:08:10.910-04:00' ) ;
379
+ } ) ;
380
+
381
+ test ( 'if `envVarsService` is not set, ensures timestamp is formatted to `UTC` even with `America/New_York` timezone' , ( ) => {
382
+ // Prepare
383
+ process . env . TZ = 'America/New_York' ;
384
+ /*
385
+ Difference between UTC and `America/New_York`(GMT -04.00) is 240 minutes.
386
+ The positive value indicates that `America/New_York` is behind UTC.
387
+ */
388
+ jest . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( 240 ) ;
389
+ const formatter = new PowertoolsLogFormatter ( ) ;
390
+
391
+ // Act
392
+ const timestamp = formatter . formatTimestamp ( new Date ( ) ) ;
393
+
394
+ // Assess
395
+ expect ( timestamp ) . toEqual ( '2016-06-20T12:08:10.000Z' ) ;
396
+ } ) ;
397
+
398
+ test ( 'it formats the timestamp to ISO 8601, accounting for the `Asia/Dhaka` timezone offset' , ( ) => {
399
+ // Prepare
400
+ process . env . TZ = 'Asia/Dhaka' ;
401
+ /*
402
+ Difference between UTC and `Asia/Dhaka`(GMT +06.00) is 360 minutes.
403
+ The negative value indicates that `Asia/Dhaka` is ahead of UTC.
404
+ */
405
+ jest . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( - 360 ) ;
406
+ const formatter = new PowertoolsLogFormatter ( {
407
+ envVarsService : new EnvironmentVariablesService ( ) ,
408
+ } ) ;
409
+
410
+ // Act
411
+ const timestamp = formatter . formatTimestamp ( new Date ( ) ) ;
412
+
413
+ // Assess
414
+ expect ( timestamp ) . toEqual ( '2016-06-20T18:08:10.000+06:00' ) ;
415
+ } ) ;
416
+
417
+ test ( 'it formats the timestamp to ISO 8601 with correct milliseconds for `Asia/Dhaka` timezone' , ( ) => {
418
+ // Prepare
419
+ process . env . TZ = 'Asia/Dhaka' ;
420
+ jest . useFakeTimers ( ) . setSystemTime ( new Date ( '2016-06-20T12:08:10.910Z' ) ) ;
421
+ /*
422
+ Difference between UTC and `Asia/Dhaka`(GMT +06.00) is 360 minutes.
423
+ The negative value indicates that `Asia/Dhaka` is ahead of UTC.
424
+ */
425
+ jest . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( - 360 ) ;
426
+ const formatter = new PowertoolsLogFormatter ( {
427
+ envVarsService : new EnvironmentVariablesService ( ) ,
428
+ } ) ;
429
+
430
+ // Act
431
+ const timestamp = formatter . formatTimestamp ( new Date ( ) ) ;
432
+
433
+ // Assess
434
+ expect ( timestamp ) . toEqual ( '2016-06-20T18:08:10.910+06:00' ) ;
435
+ } ) ;
436
+
437
+ test ( 'it formats the timestamp to ISO 8601, adjusting for `Asia/Dhaka` timezone, preserving milliseconds and accounting for date change' , ( ) => {
438
+ // Prepare
439
+ process . env . TZ = 'Asia/Dhaka' ;
440
+ const mockDate = new Date ( '2016-06-20T20:08:10.910Z' ) ;
441
+ jest . useFakeTimers ( ) . setSystemTime ( mockDate ) ;
442
+ /*
443
+ Difference between UTC and `Asia/Dhaka`(GMT +06.00) is 360 minutes.
444
+ The negative value indicates that `Asia/Dhaka` is ahead of UTC.
445
+ */
446
+ jest . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( - 360 ) ;
447
+ const formatter = new PowertoolsLogFormatter ( {
448
+ envVarsService : new EnvironmentVariablesService ( ) ,
449
+ } ) ;
450
+
451
+ // Act
452
+ const timestamp = formatter . formatTimestamp ( new Date ( ) ) ;
453
+
454
+ // Assess
455
+ expect ( timestamp ) . toEqual ( '2016-06-21T02:08:10.910+06:00' ) ;
456
+ } ) ;
457
+
458
+ test ( 'if `envVarsService` is not set, ensures timestamp is formatted to `UTC` even with `Asia/Dhaka` timezone' , ( ) => {
459
+ // Prepare
460
+ process . env . TZ = 'Asia/Dhaka' ;
461
+ /*
462
+ Difference between UTC and `Asia/Dhaka`(GMT +06.00) is 360 minutes.
463
+ The negative value indicates that `Asia/Dhaka` is ahead of UTC.
464
+ */
465
+ jest . spyOn ( Date . prototype , 'getTimezoneOffset' ) . mockReturnValue ( - 360 ) ;
466
+ const formatter = new PowertoolsLogFormatter ( ) ;
467
+
468
+ // Act
469
+ const timestamp = formatter . formatTimestamp ( new Date ( ) ) ;
470
+
471
+ // Assess
472
+ expect ( timestamp ) . toEqual ( '2016-06-20T12:08:10.000Z' ) ;
473
+ } ) ;
312
474
} ) ;
313
475
314
476
describe ( 'Method: getCodeLocation' , ( ) => {
0 commit comments