Skip to content

Commit ea5c6d7

Browse files
committed
Test: Add tests for new timezone features
Add comprehensive tests for: - Consolidated timezone imports from 'date-and-time/timezone' - IANA timezone name string support in format() - Ensure both import methods produce identical results
1 parent 69b896e commit ea5c6d7

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

tests/format.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { describe, expect, test, beforeAll } from 'vitest';
22
import { format, compile } from '@/index.ts';
33
import Los_Angeles from '@/timezones/America/Los_Angeles.ts';
44
import Tokyo from '@/timezones/Asia/Tokyo.ts';
5+
import { Los_Angeles as los_angeles, Tokyo as tokyo } from '@/timezone.ts';
56

67
describe('YYYY', () => {
78
beforeAll(() => (process.env.TZ = 'UTC'));
@@ -750,7 +751,11 @@ describe('options', () => {
750751
test('timeZone', () => {
751752
const now = new Date(2025, 1 - 1, 1, 0);
752753
expect(format(now, 'YYYY-MM-DD HH:mm:ss.SSS Z dd', { timeZone: Los_Angeles })).toBe('2024-12-31 16:00:00.000 -0800 Tu');
754+
expect(format(now, 'YYYY-MM-DD HH:mm:ss.SSS Z dd', { timeZone: los_angeles })).toBe('2024-12-31 16:00:00.000 -0800 Tu');
755+
expect(format(now, 'YYYY-MM-DD HH:mm:ss.SSS Z dd', { timeZone: 'America/Los_Angeles' })).toBe('2024-12-31 16:00:00.000 -0800 Tu');
753756
expect(format(now, 'YYYY-MM-DD HH:mm:ss.SSS Z dd', { timeZone: Tokyo })).toBe('2025-01-01 09:00:00.000 +0900 We');
757+
expect(format(now, 'YYYY-MM-DD HH:mm:ss.SSS Z dd', { timeZone: tokyo })).toBe('2025-01-01 09:00:00.000 +0900 We');
758+
expect(format(now, 'YYYY-MM-DD HH:mm:ss.SSS Z dd', { timeZone: 'Asia/Tokyo' })).toBe('2025-01-01 09:00:00.000 +0900 We');
754759
expect(format(now, 'YYYY-MM-DD HH:mm:ss.SSS Z dd', { timeZone: 'UTC' })).toBe('2025-01-01 00:00:00.000 +0000 We');
755760
});
756761
});

tests/parse.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Los_Angeles from '@/timezones/America/Los_Angeles.ts';
55
import Tokyo from '@/timezones/Asia/Tokyo.ts';
66
import Adelaide from '@/timezones/Australia/Adelaide.ts';
77
import Apia from '@/timezones/Pacific/Apia.ts';
8+
import { Los_Angeles as los_angeles, Tokyo as tokyo, Adelaide as adelaide, Apia as apia } from '@/timezone.ts';
89

910
test('YYYY', () => {
1011
expect(Number.isNaN(parse('0000', 'YYYY').getTime())).toBe(true);
@@ -532,7 +533,9 @@ describe('options', () => {
532533
test('timeZone', () => {
533534
const now = new Date(Date.UTC(2025, 1 - 1, 1, 0));
534535
expect(parse('2024-12-31 16:00:00', 'YYYY-MM-DD HH:mm:ss', { timeZone: Los_Angeles })).toEqual(now);
536+
expect(parse('2024-12-31 16:00:00', 'YYYY-MM-DD HH:mm:ss', { timeZone: los_angeles })).toEqual(now);
535537
expect(parse('2025-01-01 09:00:00', 'YYYY-MM-DD HH:mm:ss', { timeZone: Tokyo })).toEqual(now);
538+
expect(parse('2025-01-01 09:00:00', 'YYYY-MM-DD HH:mm:ss', { timeZone: tokyo })).toEqual(now);
536539
expect(parse('2025-01-01 00:00:00', 'YYYY-MM-DD HH:mm:ss', { timeZone: 'UTC' })).toEqual(now);
537540

538541
const dummyTimeZone = {
@@ -548,31 +551,37 @@ describe('timeZone Los_Angeles', () => {
548551
test('before DST', () => {
549552
const now = new Date('2021-03-14T09:59:59.999Z');
550553
expect(parse('2021-03-14 01:59:59.999', 'YYYY-MM-DD HH:mm:ss.SSS', { timeZone: Los_Angeles })).toEqual(now);
554+
expect(parse('2021-03-14 01:59:59.999', 'YYYY-MM-DD HH:mm:ss.SSS', { timeZone: los_angeles })).toEqual(now);
551555
});
552556

553557
test('start DST 1', () => {
554558
const now = new Date('2021-03-14T10:00:00.000Z');
555559
expect(parse('2021-03-14 02:00:00.000', 'YYYY-MM-DD HH:mm:ss.SSS', { timeZone: Los_Angeles })).toEqual(now);
560+
expect(parse('2021-03-14 02:00:00.000', 'YYYY-MM-DD HH:mm:ss.SSS', { timeZone: los_angeles })).toEqual(now);
556561
});
557562

558563
test('start DST 2', () => {
559564
const now = new Date('2021-03-14T10:00:00.000Z');
560565
expect(parse('2021-03-14 03:00:00.000', 'YYYY-MM-DD HH:mm:ss.SSS', { timeZone: Los_Angeles })).toEqual(now);
566+
expect(parse('2021-03-14 03:00:00.000', 'YYYY-MM-DD HH:mm:ss.SSS', { timeZone: los_angeles })).toEqual(now);
561567
});
562568

563569
test('before of PST', () => {
564570
const now = new Date('2021-11-07T08:59:59.999Z');
565571
expect(parse('2021-11-07 01:59:59.999', 'YYYY-MM-DD HH:mm:ss.SSS', { timeZone: Los_Angeles })).toEqual(now);
572+
expect(parse('2021-11-07 01:59:59.999', 'YYYY-MM-DD HH:mm:ss.SSS', { timeZone: los_angeles })).toEqual(now);
566573
});
567574

568575
test('end of DST', () => {
569576
const now = new Date('2021-11-07T10:00:00.000Z');
570577
expect(parse('2021-11-07 02:00:00.000', 'YYYY-MM-DD HH:mm:ss.SSS', { timeZone: Los_Angeles })).toEqual(now);
578+
expect(parse('2021-11-07 02:00:00.000', 'YYYY-MM-DD HH:mm:ss.SSS', { timeZone: los_angeles })).toEqual(now);
571579
});
572580

573581
test('after of DST', () => {
574582
const now = new Date('2021-11-07T11:00:00.000Z');
575583
expect(parse('2021-11-07 03:00:00.000', 'YYYY-MM-DD HH:mm:ss.SSS', { timeZone: Los_Angeles })).toEqual(now);
584+
expect(parse('2021-11-07 03:00:00.000', 'YYYY-MM-DD HH:mm:ss.SSS', { timeZone: los_angeles })).toEqual(now);
576585
});
577586
});
578587

@@ -584,6 +593,7 @@ describe('timeZone Adelaide', () => {
584593
const dateObj = new Date(Date.UTC(2021, 9, 2, 16, 29, 59, 999));
585594

586595
expect(parse(dateString, formatString, { timeZone: Adelaide }).getTime()).toBe(dateObj.getTime());
596+
expect(parse(dateString, formatString, { timeZone: adelaide }).getTime()).toBe(dateObj.getTime());
587597
});
588598

589599
test('start of DST 1', () => {
@@ -593,6 +603,7 @@ describe('timeZone Adelaide', () => {
593603
const dateObj = new Date(Date.UTC(2021, 9, 2, 16, 30, 0, 0));
594604

595605
expect(parse(dateString, formatString, { timeZone: Adelaide }).getTime()).toBe(dateObj.getTime());
606+
expect(parse(dateString, formatString, { timeZone: adelaide }).getTime()).toBe(dateObj.getTime());
596607
});
597608

598609
test('start of DST 2', () => {
@@ -602,6 +613,7 @@ describe('timeZone Adelaide', () => {
602613
const dateObj = new Date(Date.UTC(2021, 9, 2, 17, 29, 59, 999));
603614

604615
expect(parse(dateString, formatString, { timeZone: Adelaide }).getTime()).toBe(dateObj.getTime());
616+
expect(parse(dateString, formatString, { timeZone: adelaide }).getTime()).toBe(dateObj.getTime());
605617
});
606618

607619
test('start of DST 3', () => {
@@ -611,6 +623,7 @@ describe('timeZone Adelaide', () => {
611623
const dateObj = new Date(Date.UTC(2021, 9, 2, 16, 30, 0, 0));
612624

613625
expect(parse(dateString, formatString, { timeZone: Adelaide }).getTime()).toBe(dateObj.getTime());
626+
expect(parse(dateString, formatString, { timeZone: adelaide }).getTime()).toBe(dateObj.getTime());
614627
});
615628

616629
test('end of DST', () => {
@@ -620,6 +633,7 @@ describe('timeZone Adelaide', () => {
620633
const dateObj = new Date(Date.UTC(2021, 3, 3, 16, 29, 59, 999));
621634

622635
expect(parse(dateString, formatString, { timeZone: Adelaide }).getTime()).toBe(dateObj.getTime());
636+
expect(parse(dateString, formatString, { timeZone: adelaide }).getTime()).toBe(dateObj.getTime());
623637
});
624638

625639
test('after DST', () => {
@@ -629,6 +643,7 @@ describe('timeZone Adelaide', () => {
629643
const dateObj = new Date(Date.UTC(2021, 3, 3, 17, 30, 0, 0));
630644

631645
expect(parse(dateString, formatString, { timeZone: Adelaide }).getTime()).toBe(dateObj.getTime());
646+
expect(parse(dateString, formatString, { timeZone: adelaide }).getTime()).toBe(dateObj.getTime());
632647
});
633648
});
634649

@@ -641,6 +656,7 @@ describe('timeZone Apia', () => {
641656
const dateObj = new Date(1892, 7 - 1, 4, 23, 59, 59);
642657

643658
expect(parse(dateString, formatString, { timeZone: Apia }).getTime()).toBe(dateObj.getTime());
659+
expect(parse(dateString, formatString, { timeZone: apia }).getTime()).toBe(dateObj.getTime());
644660
});
645661

646662
test('1.2', () => {
@@ -649,6 +665,7 @@ describe('timeZone Apia', () => {
649665
const dateObj = new Date(1892, 7 - 1, 4, 0, 0, 0);
650666

651667
expect(parse(dateString, formatString, { timeZone: Apia }).getTime()).toBe(dateObj.getTime());
668+
expect(parse(dateString, formatString, { timeZone: apia }).getTime()).toBe(dateObj.getTime());
652669
});
653670

654671
test('2.1', () => {
@@ -657,6 +674,7 @@ describe('timeZone Apia', () => {
657674
const dateObj = new Date(1910, 12 - 1, 31, 23, 59, 59);
658675

659676
expect(parse(dateString, formatString, { timeZone: Apia }).getTime()).toBe(dateObj.getTime());
677+
expect(parse(dateString, formatString, { timeZone: apia }).getTime()).toBe(dateObj.getTime());
660678
});
661679

662680
test('2.2', () => {
@@ -665,6 +683,7 @@ describe('timeZone Apia', () => {
665683
const dateObj = new Date(1910, 12 - 1, 31, 23, 56, 56);
666684

667685
expect(parse(dateString, formatString, { timeZone: Apia }).getTime()).toBe(dateObj.getTime());
686+
expect(parse(dateString, formatString, { timeZone: apia }).getTime()).toBe(dateObj.getTime());
668687
});
669688

670689
test('3.1', () => {
@@ -673,6 +692,7 @@ describe('timeZone Apia', () => {
673692
const dateObj = new Date(1949, 12 - 1, 31, 23, 59, 59);
674693

675694
expect(parse(dateString, formatString, { timeZone: Apia }).getTime()).toBe(dateObj.getTime());
695+
expect(parse(dateString, formatString, { timeZone: apia }).getTime()).toBe(dateObj.getTime());
676696
});
677697

678698
test('3.2', () => {
@@ -681,6 +701,7 @@ describe('timeZone Apia', () => {
681701
const dateObj = new Date(1950, 1 - 1, 1, 0, 30, 0);
682702

683703
expect(parse(dateString, formatString, { timeZone: Apia }).getTime()).toBe(dateObj.getTime());
704+
expect(parse(dateString, formatString, { timeZone: apia }).getTime()).toBe(dateObj.getTime());
684705
});
685706

686707
test('4.1', () => {
@@ -689,6 +710,7 @@ describe('timeZone Apia', () => {
689710
const dateObj = new Date(2010, 9 - 1, 25, 23, 59, 59);
690711

691712
expect(parse(dateString, formatString, { timeZone: Apia }).getTime()).toBe(dateObj.getTime());
713+
expect(parse(dateString, formatString, { timeZone: apia }).getTime()).toBe(dateObj.getTime());
692714
});
693715

694716
test('4.2', () => {
@@ -697,6 +719,7 @@ describe('timeZone Apia', () => {
697719
const dateObj = new Date(2010, 9 - 1, 26, 1, 0, 0);
698720

699721
expect(parse(dateString, formatString, { timeZone: Apia }).getTime()).toBe(dateObj.getTime());
722+
expect(parse(dateString, formatString, { timeZone: apia }).getTime()).toBe(dateObj.getTime());
700723
});
701724

702725
test('5.1', () => {
@@ -705,6 +728,7 @@ describe('timeZone Apia', () => {
705728
const dateObj = new Date(2011, 4 - 1, 2, 3, 59, 59);
706729

707730
expect(parse(dateString, formatString, { timeZone: Apia }).getTime()).toBe(dateObj.getTime());
731+
expect(parse(dateString, formatString, { timeZone: apia }).getTime()).toBe(dateObj.getTime());
708732
});
709733

710734
test('5.2', () => {
@@ -713,6 +737,7 @@ describe('timeZone Apia', () => {
713737
const dateObj = new Date(2011, 4 - 1, 2, 3, 0, 0);
714738

715739
expect(parse(dateString, formatString, { timeZone: Apia }).getTime()).toBe(dateObj.getTime());
740+
expect(parse(dateString, formatString, { timeZone: apia }).getTime()).toBe(dateObj.getTime());
716741
});
717742

718743
test('6.1', () => {
@@ -721,6 +746,7 @@ describe('timeZone Apia', () => {
721746
const dateObj = new Date(2011, 9 - 1, 24, 2, 59, 59);
722747

723748
expect(parse(dateString, formatString, { timeZone: Apia }).getTime()).toBe(dateObj.getTime());
749+
expect(parse(dateString, formatString, { timeZone: apia }).getTime()).toBe(dateObj.getTime());
724750
});
725751

726752
test('6.2', () => {
@@ -729,6 +755,7 @@ describe('timeZone Apia', () => {
729755
const dateObj = new Date(2011, 9 - 1, 24, 4, 0, 0);
730756

731757
expect(parse(dateString, formatString, { timeZone: Apia }).getTime()).toBe(dateObj.getTime());
758+
expect(parse(dateString, formatString, { timeZone: apia }).getTime()).toBe(dateObj.getTime());
732759
});
733760

734761
test('7.1', () => {
@@ -737,6 +764,7 @@ describe('timeZone Apia', () => {
737764
const dateObj = new Date(2011, 12 - 1, 29, 23, 59, 59);
738765

739766
expect(parse(dateString, formatString, { timeZone: Apia }).getTime()).toBe(dateObj.getTime());
767+
expect(parse(dateString, formatString, { timeZone: apia }).getTime()).toBe(dateObj.getTime());
740768
});
741769

742770
test('7.2', () => {
@@ -745,6 +773,7 @@ describe('timeZone Apia', () => {
745773
const dateObj = new Date(2011, 12 - 1, 31, 0, 0, 0);
746774

747775
expect(parse(dateString, formatString, { timeZone: Apia }).getTime()).toBe(dateObj.getTime());
776+
expect(parse(dateString, formatString, { timeZone: apia }).getTime()).toBe(dateObj.getTime());
748777
});
749778

750779
test('8.1', () => {
@@ -753,6 +782,7 @@ describe('timeZone Apia', () => {
753782
const dateObj = new Date(2012, 4 - 1, 1, 3, 59, 59);
754783

755784
expect(parse(dateString, formatString, { timeZone: Apia }).getTime()).toBe(dateObj.getTime());
785+
expect(parse(dateString, formatString, { timeZone: apia }).getTime()).toBe(dateObj.getTime());
756786
});
757787

758788
test('8.2', () => {
@@ -761,5 +791,6 @@ describe('timeZone Apia', () => {
761791
const dateObj = new Date(2012, 4 - 1, 1, 3, 0, 0);
762792

763793
expect(parse(dateString, formatString, { timeZone: Apia }).getTime()).toBe(dateObj.getTime());
794+
expect(parse(dateString, formatString, { timeZone: apia }).getTime()).toBe(dateObj.getTime());
764795
});
765796
});

0 commit comments

Comments
 (0)