diff --git a/packages/expect/src/jest-utils.ts b/packages/expect/src/jest-utils.ts index 63b3a402a..bd6a2e4ba 100644 --- a/packages/expect/src/jest-utils.ts +++ b/packages/expect/src/jest-utils.ts @@ -159,6 +159,16 @@ function eq( // RegExps are compared by their source patterns and flags. case '[object RegExp]': return a.source === b.source && a.flags === b.flags + case '[object Temporal.Instant]': + case '[object Temporal.ZonedDateTime]': + case '[object Temporal.PlainDateTime]': + case '[object Temporal.PlainDate]': + case '[object Temporal.PlainTime]': + case '[object Temporal.PlainYearMonth]': + case '[object Temporal.PlainMonthDay]': + return a.equals(b) + case '[object Temporal.Duration]': + return a.toString() === b.toString() } if (typeof a !== 'object' || typeof b !== 'object') { return false diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 40b279711..7eb56caa1 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1294,6 +1294,9 @@ importers: sweetalert2: specifier: ^11.21.2 version: 11.21.2 + temporal-polyfill: + specifier: ~0.3.0 + version: 0.3.0 tinyrainbow: specifier: 'catalog:' version: 2.0.0 @@ -8277,6 +8280,12 @@ packages: resolution: {integrity: sha512-aoBAniQmmwtcKp/7BzsH8Cxzv8OL736p7v1ihGb5e9DJ9kTwGWHrQrVB5+lfVDzfGrdRzXch+ig7LHaY1JTOrg==} engines: {node: '>=8'} + temporal-polyfill@0.3.0: + resolution: {integrity: sha512-qNsTkX9K8hi+FHDfHmf22e/OGuXmfBm9RqNismxBrnSmZVJKegQ+HYYXT+R7Ha8F/YSm2Y34vmzD4cxMu2u95g==} + + temporal-spec@0.3.0: + resolution: {integrity: sha512-n+noVpIqz4hYgFSMOSiINNOUOMFtV5cZQNCmmszA6GiVFVRt3G7AqVyhXjhCSmowvQn+NsGn+jMDMKJYHd3bSQ==} + tempy@0.6.0: resolution: {integrity: sha512-G13vtMYPT/J8A4X2SjdtBTphZlrp1gKv6hZiOjw14RCWg6GbHuQBGtjlx75xLbYV/wEc0D7G5K4rxKP/cXk8Bw==} engines: {node: '>=10'} @@ -16750,6 +16759,12 @@ snapshots: temp-dir@2.0.0: {} + temporal-polyfill@0.3.0: + dependencies: + temporal-spec: 0.3.0 + + temporal-spec@0.3.0: {} + tempy@0.6.0: dependencies: is-stream: 2.0.1 diff --git a/test/core/package.json b/test/core/package.json index 4b0cc1e88..25a49cc03 100644 --- a/test/core/package.json +++ b/test/core/package.json @@ -30,6 +30,7 @@ "react": "^19.1.0", "react-18": "npm:react@18.3.1", "sweetalert2": "^11.21.2", + "temporal-polyfill": "~0.3.0", "tinyrainbow": "catalog:", "tinyspy": "^1.1.1", "url": "^0.11.4", diff --git a/test/core/test/expect.test.ts b/test/core/test/expect.test.ts index c0e0d71a5..6947be4be 100644 --- a/test/core/test/expect.test.ts +++ b/test/core/test/expect.test.ts @@ -1,5 +1,6 @@ import type { Tester } from '@vitest/expect' import { getCurrentTest } from '@vitest/runner' +import { Temporal } from 'temporal-polyfill' import { describe, expect, expectTypeOf, test, vi } from 'vitest' describe('expect.soft', () => { @@ -346,3 +347,45 @@ describe('iterator', () => { expect(a).not.toStrictEqual(b) }) }) + +describe('Temporal equality', () => { + describe.each([ + ['Instant', ['2025-01-01T00:00:00.000Z', '2026-01-01T00:00:00.000Z']], + ['ZonedDateTime', ['2025-01-01T00:00:00+01:00[Europe/Amsterdam]', '2025-01-01T00:00:00+01:00[Europe/Paris]']], + ['PlainDateTime', ['2025-01-01T00:00:00.000', '2026-01-01T00:00:00.000']], + ['PlainDate', ['2025-01-01', '2026-01-01']], + ['PlainTime', ['15:00:00.000', '16:00:00.000']], + ['PlainYearMonth', ['2025-01', '2026-01']], + ['PlainMonthDay', ['01-01', '02-01']], + ] as const)('of $className', (className, [first, second]) => { + test('returns true when equal', () => { + const a = Temporal[className].from(first) + const b = Temporal[className].from(first) + + expect(a).toStrictEqual(b) + }) + + test('returns false when not equal', () => { + const a = Temporal[className].from(first) + const b = Temporal[className].from(second) + + expect(a).not.toStrictEqual(b) + }) + }) + + describe('of Duration', () => { + test('returns true when .toString() is equal', () => { + const a = Temporal.Duration.from('P1M') + const b = Temporal.Duration.from('P1M') + + expect(a).toStrictEqual(b) + }) + + test('returns true when .toString() is not equal', () => { + const a = Temporal.Duration.from('PT1M') + const b = Temporal.Duration.from('PT60S') + + expect(a).not.toStrictEqual(b) + }) + }) +})