From 5c4fe3d06bf628af4916aeaa62c05720a8d6e9fe Mon Sep 17 00:00:00 2001 From: Daniel Kimmich Date: Sat, 30 May 2026 14:32:00 +0200 Subject: [PATCH] refactor: replace dayjs with temporal in unit tests --- libs/angular-ecmascript-intl/package.json | 1 - .../relative-time/relative-time.pipe.spec.ts | 92 ++++++++++++++----- .../tsconfig.spec.json | 3 +- package.json | 2 +- pnpm-lock.yaml | 69 ++++++-------- pnpm-workspace.yaml | 1 - 6 files changed, 102 insertions(+), 66 deletions(-) diff --git a/libs/angular-ecmascript-intl/package.json b/libs/angular-ecmascript-intl/package.json index b00d45a4..3fd1814c 100644 --- a/libs/angular-ecmascript-intl/package.json +++ b/libs/angular-ecmascript-intl/package.json @@ -45,7 +45,6 @@ "@vitest/browser-playwright": "catalog:", "@vitest/coverage-v8": "catalog:", "cpy-cli": "catalog:", - "dayjs": "catalog:", "ng-packagr": "catalog:", "playwright": "catalog:", "rxjs": "catalog:", diff --git a/libs/angular-ecmascript-intl/src/lib/relative-time/relative-time.pipe.spec.ts b/libs/angular-ecmascript-intl/src/lib/relative-time/relative-time.pipe.spec.ts index 4f868342..08993740 100644 --- a/libs/angular-ecmascript-intl/src/lib/relative-time/relative-time.pipe.spec.ts +++ b/libs/angular-ecmascript-intl/src/lib/relative-time/relative-time.pipe.spec.ts @@ -1,5 +1,4 @@ import { TestBed } from '@angular/core/testing'; -import dayjs from 'dayjs'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { INTL_LOCALES } from '../locale'; import { INTL_RELATIVE_TIME_PIPE_DEFAULT_OPTIONS } from './relative-time-pipe-default-options'; @@ -38,13 +37,13 @@ describe('RelativeTimePipe', () => { }); it('should throw an error when an invalid string is passed', () => { - expect(() => testUnit.transform('someInvalidDate')).toThrowError( + expect(() => testUnit.transform('someInvalidDate')).toThrow( 'someInvalidDate is not a valid date', ); }); it('should throw an error when an invalid date is passed', () => { - expect(() => testUnit.transform(new Date('invalid'))).toThrowError( + expect(() => testUnit.transform(new Date('invalid'))).toThrow( 'Invalid Date is not a valid date', ); }); @@ -65,16 +64,23 @@ describe('RelativeTimePipe', () => { describe('years', () => { it('should transform a date one year in past', () => { - const date = dayjs().subtract(1, 'year').subtract(1, 'second').toDate(); + const date = new Date( + Temporal.Now.instant() + .toZonedDateTimeISO('UTC') + .subtract({ years: 1 }) + .subtract({ seconds: 1 }).epochMilliseconds, + ); expect(testUnit.transform(date)).toEqual('1 year ago'); }); it('should transform a date almost 3 years in future', () => { - const date = dayjs() - .add(365 * 3, 'days') - .subtract(1, 'second') - .toDate(); + const date = new Date( + Temporal.Now.instant() + .toZonedDateTimeISO('UTC') + .add({ days: 365 * 3 }) + .subtract({ seconds: 1 }).epochMilliseconds, + ); expect(testUnit.transform(date)).toEqual('in 2 years'); }); @@ -82,16 +88,23 @@ describe('RelativeTimePipe', () => { describe('months', () => { it('should transform a date 1 month in future', () => { - const date = dayjs().add(31, 'days').add(1, 'second').toDate(); + const date = new Date( + Temporal.Now.instant() + .toZonedDateTimeISO('UTC') + .add({ days: 31 }) + .add({ seconds: 1 }).epochMilliseconds, + ); expect(testUnit.transform(date)).toEqual('in 1 month'); }); it('should transform a date almost 12 months in past', () => { - const date = dayjs() - .subtract(30 * 12, 'days') - .add(1, 'second') - .toDate(); + const date = new Date( + Temporal.Now.instant() + .toZonedDateTimeISO('UTC') + .subtract({ days: 30 * 12 }) + .add({ seconds: 1 }).epochMilliseconds, + ); expect(testUnit.transform(date)).toEqual('11 months ago'); }); @@ -100,13 +113,23 @@ describe('RelativeTimePipe', () => { describe('weeks', () => { it('should transform a date 1 week in future', () => { // We need to account for clock changes here - const date = dayjs().add(1, 'week').add(2, 'hour').toDate(); + const date = new Date( + Temporal.Now.instant() + .toZonedDateTimeISO('UTC') + .add({ weeks: 1 }) + .add({ hours: 2 }).epochMilliseconds, + ); expect(testUnit.transform(date)).toEqual('in 1 week'); }); it('should transform a date almost 4 weeks in past', () => { - const date = dayjs().subtract(4, 'weeks').add(1, 'day').toDate(); + const date = new Date( + Temporal.Now.instant() + .toZonedDateTimeISO('UTC') + .subtract({ weeks: 4 }) + .add({ days: 1 }).epochMilliseconds, + ); expect(testUnit.transform(date)).toEqual('3 weeks ago'); }); @@ -114,13 +137,23 @@ describe('RelativeTimePipe', () => { describe('days', () => { it('should transform a date 1 day in future', () => { - const date = dayjs().add(1, 'day').add(1, 'second').toDate(); + const date = new Date( + Temporal.Now.instant() + .toZonedDateTimeISO('UTC') + .add({ days: 1 }) + .add({ seconds: 1 }).epochMilliseconds, + ); expect(testUnit.transform(date)).toEqual('in 1 day'); }); it('should transform a date almost 7 days in past', () => { - const date = dayjs().subtract(7, 'days').add(1, 'second').toDate(); + const date = new Date( + Temporal.Now.instant() + .toZonedDateTimeISO('UTC') + .subtract({ days: 7 }) + .add({ seconds: 1 }).epochMilliseconds, + ); expect(testUnit.transform(date)).toEqual('6 days ago'); }); @@ -128,13 +161,19 @@ describe('RelativeTimePipe', () => { describe('hours', () => { it('should transform a date 1 hour in future', () => { - const date = dayjs().add(1, 'hour').add(1, 'second').toDate(); + const date = new Date( + Temporal.Now.instant().add({ hours: 1 }).add({ seconds: 1 }) + .epochMilliseconds, + ); expect(testUnit.transform(date)).toEqual('in 1 hour'); }); it('should transform a date almost 24 hours in past', () => { - const date = dayjs().subtract(24, 'hours').add(1, 'second').toDate(); + const date = new Date( + Temporal.Now.instant().subtract({ hours: 24 }).add({ seconds: 1 }) + .epochMilliseconds, + ); expect(testUnit.transform(date)).toEqual('23 hours ago'); }); @@ -142,20 +181,29 @@ describe('RelativeTimePipe', () => { describe('minutes', () => { it('should transform a date 1 minute in future', () => { - const date = dayjs().add(1, 'minute').add(1, 'second').toDate(); + const date = new Date( + Temporal.Now.instant().add({ minutes: 1 }).add({ seconds: 1 }) + .epochMilliseconds, + ); expect(testUnit.transform(date)).toEqual('in 1 minute'); }); it('should transform a date almost 59 minutes in past', () => { - const date = dayjs().subtract(60, 'minutes').add(1, 'second').toDate(); + const date = new Date( + Temporal.Now.instant().subtract({ minutes: 60 }).add({ seconds: 1 }) + .epochMilliseconds, + ); expect(testUnit.transform(date)).toEqual('59 minutes ago'); }); }); it('should transform a date almost than 1 minute in past', () => { - const date = dayjs().subtract(1, 'minute').add(1, 'second').toDate(); + const date = new Date( + Temporal.Now.instant().subtract({ minutes: 1 }).add({ seconds: 1 }) + .epochMilliseconds, + ); expect(testUnit.transform(date)).toEqual('in 0 minutes'); }); diff --git a/libs/angular-ecmascript-intl/tsconfig.spec.json b/libs/angular-ecmascript-intl/tsconfig.spec.json index 81c7d1ca..8d76a9bb 100644 --- a/libs/angular-ecmascript-intl/tsconfig.spec.json +++ b/libs/angular-ecmascript-intl/tsconfig.spec.json @@ -2,7 +2,8 @@ { "extends": "./tsconfig.json", "compilerOptions": { - "outDir": "./out-tsc/spec" + "outDir": "./out-tsc/spec", + "lib": ["dom", "es2022", "ESNext.Temporal"] }, "include": ["src/**/*.ts"] } diff --git a/package.json b/package.json index 6152dceb..2c8172c4 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "engines": { "runtime": { "name": "node", - "version": "24.15.0", + "version": "26.2.0", "onFail": "download" } }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c5cb1214..88cbde5b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -55,9 +55,6 @@ catalogs: cpy-cli: specifier: 7.0.0 version: 7.0.0 - dayjs: - specifier: 1.11.21 - version: 1.11.21 eslint: specifier: 10.4.0 version: 10.4.0 @@ -109,8 +106,8 @@ importers: .: dependencies: node: - specifier: runtime:24.15.0 - version: runtime:24.15.0 + specifier: runtime:26.2.0 + version: runtime:26.2.0 devDependencies: '@angular/core': specifier: 'catalog:' @@ -225,9 +222,6 @@ importers: cpy-cli: specifier: 'catalog:' version: 7.0.0 - dayjs: - specifier: 'catalog:' - version: 1.11.21 ng-packagr: specifier: 'catalog:' version: 21.2.3(tslib@2.8.1)(typescript@6.0.3) @@ -2483,9 +2477,6 @@ packages: resolution: {integrity: sha512-wD5oz5xibMOPHzy13CyGmogB3phdvcDaB5t0W/Nr5Z2O/agcB8YwOz6e2Lsp10pNDzBoDO9nVa3RGs/2BttpHQ==} engines: {node: '>= 6'} - dayjs@1.11.21: - resolution: {integrity: sha512-98IT+HOahAisibz/yjKbzuOBwYcjJ7BCLPzARyHiyEBmRz4fatF+KPJszEHXsGYjUG234aH/cOjW1wwTbKUZlA==} - debug@4.4.3: resolution: {integrity: sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA==} engines: {node: '>=6.0'} @@ -3249,7 +3240,7 @@ packages: resolution: {integrity: sha512-GYVXHE2KnrzAfsAjl4uP++evGFCrAU1jta4ubEjIG7YWt/64Gqv66a30yKwWczVjA6j3bM4nBwH7Pk1JmDHaxQ==} engines: {node: '>=18'} - node@runtime:24.15.0: + node@runtime:26.2.0: resolution: type: variations variants: @@ -3257,9 +3248,9 @@ packages: archive: tarball bin: node: bin/node - integrity: sha256-3UvHfctfTJoslkNzm7WTUAzLCUE+xCyqD47w5e8RYJU= + integrity: sha256-zvLmWJf5cIi1EvkiiWpW3j7AYU0CCBbvDst2KQ5wi4E= type: binary - url: https://nodejs.org/download/release/v24.15.0/node-v24.15.0-aix-ppc64.tar.gz + url: https://nodejs.org/download/release/v26.2.0/node-v26.2.0-aix-ppc64.tar.gz targets: - cpu: ppc64 os: aix @@ -3267,9 +3258,9 @@ packages: archive: tarball bin: node: bin/node - integrity: sha256-NyMxuWl3mrXRW5SYhPxur4jVr+h73ouogdZAC5EA/8Q= + integrity: sha256-LYV1tqdJwLxmzG7NIqlS99m787Dqdvyw7TzoWAGK//c= type: binary - url: https://nodejs.org/download/release/v24.15.0/node-v24.15.0-darwin-arm64.tar.gz + url: https://nodejs.org/download/release/v26.2.0/node-v26.2.0-darwin-arm64.tar.gz targets: - cpu: arm64 os: darwin @@ -3277,9 +3268,9 @@ packages: archive: tarball bin: node: bin/node - integrity: sha256-/9XuKTRnkn8+5zGlU+uI/R9Iz3TuvC10prq+SvIoZzs= + integrity: sha256-JqVIDQ3ZUr6Gvq6D5mNtrNej/9F+9R0LJrybJg8rQuk= type: binary - url: https://nodejs.org/download/release/v24.15.0/node-v24.15.0-darwin-x64.tar.gz + url: https://nodejs.org/download/release/v26.2.0/node-v26.2.0-darwin-x64.tar.gz targets: - cpu: x64 os: darwin @@ -3287,9 +3278,9 @@ packages: archive: tarball bin: node: bin/node - integrity: sha256-c6/CNNVYwkkZh19RwtHqACoq2k6m+DYBo4OGn++mTu0= + integrity: sha256-v1JGHSUBdHnNxUlkLE56IwOsXFOV18PQJtXYs/ooPAU= type: binary - url: https://nodejs.org/download/release/v24.15.0/node-v24.15.0-linux-arm64.tar.gz + url: https://nodejs.org/download/release/v26.2.0/node-v26.2.0-linux-arm64.tar.gz targets: - cpu: arm64 os: linux @@ -3297,9 +3288,9 @@ packages: archive: tarball bin: node: bin/node - integrity: sha256-sfiJAKSxY2XqulYmkwT8Edp1gdvwNVLUSV+azh/AX20= + integrity: sha256-S1JCmzWgSaDPV0NgzRsRy8nwMgHROHbyaJfJhj0l7Ek= type: binary - url: https://nodejs.org/download/release/v24.15.0/node-v24.15.0-linux-ppc64le.tar.gz + url: https://nodejs.org/download/release/v26.2.0/node-v26.2.0-linux-ppc64le.tar.gz targets: - cpu: ppc64le os: linux @@ -3307,9 +3298,9 @@ packages: archive: tarball bin: node: bin/node - integrity: sha256-+YVFVDnVL+m43mqPbQe/7MxzbepSfofqyv5ajXUWo4A= + integrity: sha256-nQZJFOOKWz5qMHRkdAcZgRb3vHJPzlqWAM7OhKx9pnE= type: binary - url: https://nodejs.org/download/release/v24.15.0/node-v24.15.0-linux-s390x.tar.gz + url: https://nodejs.org/download/release/v26.2.0/node-v26.2.0-linux-s390x.tar.gz targets: - cpu: s390x os: linux @@ -3317,9 +3308,9 @@ packages: archive: tarball bin: node: bin/node - integrity: sha256-RINoctmuxJ8ea1KpqSKHLbmisC0jWmFqVoG2qF/sjYk= + integrity: sha256-jltLlpuyQUrKL/hNK5lghRGu3KMr+KhCfmGH291wO74= type: binary - url: https://nodejs.org/download/release/v24.15.0/node-v24.15.0-linux-x64.tar.gz + url: https://nodejs.org/download/release/v26.2.0/node-v26.2.0-linux-x64.tar.gz targets: - cpu: x64 os: linux @@ -3327,10 +3318,10 @@ packages: archive: zip bin: node: node.exe - integrity: sha256-yet0Au2ibiun5EtnJ/yFqN5WxQlbH3Hr0wYokiEaoRY= - prefix: node-v24.15.0-win-arm64 + integrity: sha256-Ex76i9hY+K0AChoENti7EyDGOCkcd5qSH7IOVwKhzAo= + prefix: node-v26.2.0-win-arm64 type: binary - url: https://nodejs.org/download/release/v24.15.0/node-v24.15.0-win-arm64.zip + url: https://nodejs.org/download/release/v26.2.0/node-v26.2.0-win-arm64.zip targets: - cpu: arm64 os: win32 @@ -3338,10 +3329,10 @@ packages: archive: zip bin: node: node.exe - integrity: sha256-zFFJ6r1Td5zh573FQBZDYi0MfmgAreGJKKdn6UC7DmI= - prefix: node-v24.15.0-win-x64 + integrity: sha256-NxBxpPfiyKXdKAcwBJxoWRH+7MWfUOvEiNZ13BCHxpw= + prefix: node-v26.2.0-win-x64 type: binary - url: https://nodejs.org/download/release/v24.15.0/node-v24.15.0-win-x64.zip + url: https://nodejs.org/download/release/v26.2.0/node-v26.2.0-win-x64.zip targets: - cpu: x64 os: win32 @@ -3349,9 +3340,9 @@ packages: archive: tarball bin: node: bin/node - integrity: sha256-MemKqWCgZ9qR7f/V2TvEZle10qgClhLDWfXyrABgFSo= + integrity: sha256-RZPYIgEv4yOIFHIZCUCMZvKoKVXgFBRXZDp6TIThMv0= type: binary - url: https://unofficial-builds.nodejs.org/download/release/v24.15.0/node-v24.15.0-linux-arm64-musl.tar.gz + url: https://unofficial-builds.nodejs.org/download/release/v26.2.0/node-v26.2.0-linux-arm64-musl.tar.gz targets: - cpu: arm64 os: linux @@ -3360,14 +3351,14 @@ packages: archive: tarball bin: node: bin/node - integrity: sha256-9Vr1vUicU0exE8pllMrgClSzC6V6xYdTJDEb/G9HYuM= + integrity: sha256-nvyZMy4c9w7rhWGDmEZ8nTObwgi+ZRFTgMYR3aTChAc= type: binary - url: https://unofficial-builds.nodejs.org/download/release/v24.15.0/node-v24.15.0-linux-x64-musl.tar.gz + url: https://unofficial-builds.nodejs.org/download/release/v26.2.0/node-v26.2.0-linux-x64-musl.tar.gz targets: - cpu: x64 os: linux libc: musl - version: 24.15.0 + version: 26.2.0 hasBin: true nopt@9.0.0: @@ -6140,8 +6131,6 @@ snapshots: css-what@7.0.0: {} - dayjs@1.11.21: {} - debug@4.4.3: dependencies: ms: 2.1.3 @@ -6971,7 +6960,7 @@ snapshots: node-releases@2.0.46: {} - node@runtime:24.15.0: {} + node@runtime:26.2.0: {} nopt@9.0.0: dependencies: diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 7c466cbe..9796f4f1 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -27,7 +27,6 @@ catalog: '@vitest/coverage-v8': '4.1.7' angular-eslint: '21.4.0' cpy-cli: '7.0.0' - dayjs: '1.11.21' eslint: '10.4.0' marked: '18.0.4' ng-packagr: '21.2.3'