FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

feat: initial support for Temporal equality by dirkluijk · Pull Request #8007 · vitest-dev/vitest · GitHub

feat: initial support for Temporal equality - #8007

Merged
sheremet-va merged 3 commits into
vitest-dev:mainfrom
dirkluijk:temporal-support
May 23, 2025
Merged

sheremet-va merged 3 commits into
vitest-dev:mainfrom
dirkluijk:temporal-support

Conversation

dirkluijk commented May 20, 2025 •
edited
Loading

Copy link
Copy Markdown
Contributor

Description

Fixes #7991

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

netlify Bot commented May 20, 2025 •
edited
Loading

Copy link
Copy Markdown

✅ Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 2e366b1
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/682ee8f85842d600088b5124
😎 Deploy Preview https://deploy-preview-8007--vitest-dev.netlify.app
📱 Preview on mobile Toggle QR Code...



Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

case '[object Temporal.PlainYearMonth]':
case '[object Temporal.PlainMonthDay]':
case '[object Temporal.Duration]':
return a.toString() === b.toString()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Do you know which is preferred whether toString equality and builtin equals method? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/equals

It looks like there are several old issues mentions calendar comparison (such as tc39/proposal-temporal#625), but the discussion seem quite technical 😅

dirkluijk May 22, 2025 •
edited
Loading

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

.equals() seems to compare including the calendar, so I think it's a good suggestion. Let's do it!

For Temporal.Duration this .equals() method doesn't exist, but I wasn't sure about that one to begin with. For Durations, there IS actually ambiguity. For example, are 60 seconds and 1 minute the same? If you compare the duration in time, yes; but if you literally compare all their components, no. Also, for example "1 day" is not necessarily the same as "24 hour" and depends on context.

My suggestion is to use .toString() for durations so it does a literal comparison of all the different components.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Indeed it looks like Duration is tricky. tc39/proposal-temporal#856 seems to explain the details.

As a builtin equality, I think choosing toString comparison makes sense since that's the most strict way to do it. Users can still add own custom equality tester to loosen it if desired.

Copy link
Copy Markdown
Contributor Author

@hi-ogawa I updated the PR:

  • added Temporal polyfill
  • adjusted equality check and use .equals()
  • for Duration I still compare the string representation to compare all its components.

hi-ogawa left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Thanks!

AriPerkkio requested a review from sheremet-va May 23, 2025 08:50

Copy link
Copy Markdown

I'm one of the champions of the Temporal proposal. Sorry I was too late to comment on this PR back in May, but I was very happy to see this PR! Thanks for building it.

I did have a question for @hi-ogawa and @dirkluijk: in vitest, is equality comparison expected to have no observable differences between the values?

If yes, then using Temporal.ZonedDateTime.prototype.equals might be a problem. Two ZonedDateTime instances can use time zone IDs that are aliases of each other in the IANA Time Zone Database, for example Asia/Calcutta and Asia/Kolkata. The equals method will canonicalize the time zone ID before comparing, so those two time zones (and hence the ZDT instances) will be considered equal. But the value of the timeZoneId property on the ZDT instances will be different.

That said, this may be desirable behavior. One of the main reasons that we made equals canonicalize time zones is to avoid breaking automated tests when a time zone ID is deprecated in favor of a new name, like the recent rename of Europe/Kiev => Europe/Kyiv. If you use equals, then you're guaranteed that even if the IANA Time Zone Database changes, your tests will still behave the same.

So the behavior in this PR may be better! But at least I wanted to flag the behavior in case you didn't want it.

Copy link
Copy Markdown
Collaborator

is equality comparison expected to have no observable differences between the values?

@justingrant Thanks for the heads up! For this case, Vitest's goal is to provide whatever most ergonomic for users and we don't necessary need to detect anything observable. Since Temporal provides one "opinionated" equality implementation, I think test/assertion framework building up on it sounds most natural. For any niche use case, Vitest allows overriding equality via addEqualityTesters, which can always serve as escape hatch.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Strict equal not working with Temporal dates

4 participants


Back | FazBrowse Home | New Git URL