Skip to content

Commit 52bd789

Browse files
authored
feat: initial support for Temporal equality (#8007)
1 parent 69ca8b8 commit 52bd789

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

packages/expect/src/jest-utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,16 @@ function eq(
159159
// RegExps are compared by their source patterns and flags.
160160
case '[object RegExp]':
161161
return a.source === b.source && a.flags === b.flags
162+
case '[object Temporal.Instant]':
163+
case '[object Temporal.ZonedDateTime]':
164+
case '[object Temporal.PlainDateTime]':
165+
case '[object Temporal.PlainDate]':
166+
case '[object Temporal.PlainTime]':
167+
case '[object Temporal.PlainYearMonth]':
168+
case '[object Temporal.PlainMonthDay]':
169+
return a.equals(b)
170+
case '[object Temporal.Duration]':
171+
return a.toString() === b.toString()
162172
}
163173
if (typeof a !== 'object' || typeof b !== 'object') {
164174
return false

pnpm-lock.yaml

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"react": "^19.1.0",
3131
"react-18": "npm:[email protected]",
3232
"sweetalert2": "^11.21.2",
33+
"temporal-polyfill": "~0.3.0",
3334
"tinyrainbow": "catalog:",
3435
"tinyspy": "^1.1.1",
3536
"url": "^0.11.4",

test/core/test/expect.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Tester } from '@vitest/expect'
22
import { getCurrentTest } from '@vitest/runner'
3+
import { Temporal } from 'temporal-polyfill'
34
import { describe, expect, expectTypeOf, test, vi } from 'vitest'
45

56
describe('expect.soft', () => {
@@ -346,3 +347,45 @@ describe('iterator', () => {
346347
expect(a).not.toStrictEqual(b)
347348
})
348349
})
350+
351+
describe('Temporal equality', () => {
352+
describe.each([
353+
['Instant', ['2025-01-01T00:00:00.000Z', '2026-01-01T00:00:00.000Z']],
354+
['ZonedDateTime', ['2025-01-01T00:00:00+01:00[Europe/Amsterdam]', '2025-01-01T00:00:00+01:00[Europe/Paris]']],
355+
['PlainDateTime', ['2025-01-01T00:00:00.000', '2026-01-01T00:00:00.000']],
356+
['PlainDate', ['2025-01-01', '2026-01-01']],
357+
['PlainTime', ['15:00:00.000', '16:00:00.000']],
358+
['PlainYearMonth', ['2025-01', '2026-01']],
359+
['PlainMonthDay', ['01-01', '02-01']],
360+
] as const)('of $className', (className, [first, second]) => {
361+
test('returns true when equal', () => {
362+
const a = Temporal[className].from(first)
363+
const b = Temporal[className].from(first)
364+
365+
expect(a).toStrictEqual(b)
366+
})
367+
368+
test('returns false when not equal', () => {
369+
const a = Temporal[className].from(first)
370+
const b = Temporal[className].from(second)
371+
372+
expect(a).not.toStrictEqual(b)
373+
})
374+
})
375+
376+
describe('of Duration', () => {
377+
test('returns true when .toString() is equal', () => {
378+
const a = Temporal.Duration.from('P1M')
379+
const b = Temporal.Duration.from('P1M')
380+
381+
expect(a).toStrictEqual(b)
382+
})
383+
384+
test('returns true when .toString() is not equal', () => {
385+
const a = Temporal.Duration.from('PT1M')
386+
const b = Temporal.Duration.from('PT60S')
387+
388+
expect(a).not.toStrictEqual(b)
389+
})
390+
})
391+
})

0 commit comments

Comments
 (0)