Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: save methods of children Date instance (#437) #480

Merged
merged 4 commits into from
Aug 24, 2024

Conversation

Oustinger
Copy link
Contributor

Fix issue #437 by creating ClockDate as JS class and extending it from NativeDate

Why I did this way:

  • In initinial solution (creating ClockDate by JS function) ClockDate can't be correct instance to extend from it. That's why I replaced function on class. And it resolved my problem.
  • Mechanics of call Date() without new keyword I solved by using Proxy instance.
  • Also in my solution removed an mirrorDateProperties function. It's not needed any more.

But I had some troubles with tests:

  • I skiped this test, because I didn't understand what it checks and how it should be now
  • I skiped an other test, because I think it's not corresponding for now. ClockDate.prototype never equals to Date.prototype, because ClockDate is extended from Date
  • Please, check the other tests, that I changed. Hope, I changed them correct

@fatso83
Copy link
Contributor

fatso83 commented Oct 5, 2023

Oooh, I somehow missed that this was added. Sorry about the late look, but while this is interesting, my head is a bit fried 🤯 This is quite a big change, so I (or someone else with a bigger head) needs to take a new look with a fresh head!

Thanks for providing a fix to something that bugged you a year ago. That's persistence we can appreciate 😄

Copy link
Contributor

@fatso83 fatso83 left a comment

Choose a reason for hiding this comment

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

It's a bit scary breaking change, so I think I would like some more 👀 on this before going ahead.

test/issue-437-test.js Outdated Show resolved Hide resolved
test/fake-timers-test.js Show resolved Hide resolved
@@ -3221,7 +3221,7 @@ describe("FakeTimers", function () {
assert.equals(fakeDateStr, new this.clock.Date().toString());
});

it("mirrors native Date.prototype", function () {
it.skip("mirrors native Date.prototype", function () {
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess we could change this to be a test that it passes an instanceof check. it("is a Date instance", ...?

Copy link
Contributor

Choose a reason for hiding this comment

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

Just kill the test. It would just be this in the end:

node -p 'class FooDate extends Date {}; D=Date; global.Date = function(){};  (new FooDate()) instanceof D'
true

@@ -3103,7 +3103,7 @@ describe("FakeTimers", function () {
assert(typeof date === "string");
});

it("creates real Date objects when Date constructor is gone", function () {
it.skip("creates real Date objects when Date constructor is gone", function () {
Copy link
Contributor

Choose a reason for hiding this comment

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

This test does not seem very useful. I would check the Git history for references.

Copy link
Contributor

Choose a reason for hiding this comment

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

Dug back, found it was just copied over from Sinon ten years ago, so can't be bothered to look deeper:
Oustinger@dbd7d17#diff-7c9e5e3f3dd46601b73d2d05be43d6d7a33f2f7ccd564494f4eb7b3f4ae915e0R636

The only meaningful bit here about the test is that we check that everything still works as before. So the result of the toString should be the same before and after the removal of the global Date.

@benjamingr
Copy link
Member

It's a bit scary breaking change, so I think I would like some more 👀 on this before going ahead.

I started reviewing it and came to the "bit scary" conclusion as well.

Copy link

stale bot commented Dec 27, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Dec 27, 2023
@stale stale bot removed the stale label Feb 10, 2024
Copy link
Contributor

@fatso83 fatso83 left a comment

Choose a reason for hiding this comment

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

Stepan, I took a fresh new look at this and with rested eyes (and a lot of time - sorry), I don't think this looks all that controversial. I get the changes and I think it makes sense. All meaninful tests are running, so I would like to get this in.

If you read this anytime soon, do you think you could be bothered to just implement the very small changes?

Comment on lines 418 to 446
const ClockDateProxy = new Proxy(ClockDate, {
apply(Target, thisArg, argumentsList) {
// the Date constructor called as a function, ref Ecma-262 Edition 5.1, section 15.9.2.
// This remains so in the 10th edition of 2019 as well.
if (!(this instanceof ClockDate)) {
return new NativeDate(ClockDate.clock.now).toString();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

had to do a little brush up on Proxy objects and its apply to grok the logic, which made me pay so much attention to the details that I failed to see the toString() bit. That was until I actually read section 15.9.2:

When Date is called as a function rather than as a constructor, it returns a String representing the current time (UTC).

Ooh. Did not remember this. Had to check:

❯ node -p 'Date(2023,01,10)'
Sat Feb 10 2024 11:54:24 GMT+0100 (GMT+01:00)

❯ node -p 'typeof Date(2023,01,10)'
string

I'd rather extract that into a silly, but aptly named, little method called currentDateTimeAsString() and stuff all the details there, though! Makes small minds as my own able to grok the bigger picture faster. Makes for quicker 🆗 on PRs 😅 I know the bit of code was already there, though, so no worries!

src/fake-timers-src.js Show resolved Hide resolved
test/fake-timers-test.js Show resolved Hide resolved
@@ -3103,7 +3103,7 @@ describe("FakeTimers", function () {
assert(typeof date === "string");
});

it("creates real Date objects when Date constructor is gone", function () {
it.skip("creates real Date objects when Date constructor is gone", function () {
Copy link
Contributor

Choose a reason for hiding this comment

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

Dug back, found it was just copied over from Sinon ten years ago, so can't be bothered to look deeper:
Oustinger@dbd7d17#diff-7c9e5e3f3dd46601b73d2d05be43d6d7a33f2f7ccd564494f4eb7b3f4ae915e0R636

The only meaningful bit here about the test is that we check that everything still works as before. So the result of the toString should be the same before and after the removal of the global Date.

@@ -3221,7 +3221,7 @@ describe("FakeTimers", function () {
assert.equals(fakeDateStr, new this.clock.Date().toString());
});

it("mirrors native Date.prototype", function () {
it.skip("mirrors native Date.prototype", function () {
Copy link
Contributor

Choose a reason for hiding this comment

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

Just kill the test. It would just be this in the end:

node -p 'class FooDate extends Date {}; D=Date; global.Date = function(){};  (new FooDate()) instanceof D'
true

@fatso83
Copy link
Contributor

fatso83 commented Aug 15, 2024

@Oustinger This is getting a bit old, but from the comments I see that I basically OK'd it. Any chance you can get it over the finish line? Was fine work.

Степан Карташев and others added 3 commits August 24, 2024 16:59
see pr comment
by ensuring we are checking something meaningful.
Copy link

codecov bot commented Aug 24, 2024

Codecov Report

Attention: Patch coverage is 91.17647% with 3 lines in your changes missing coverage. Please review.

Project coverage is 97.53%. Comparing base (6f90a44) to head (35363ee).
Report is 6 commits behind head on main.

Files Patch % Lines
src/fake-timers-src.js 81.25% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #480      +/-   ##
==========================================
- Coverage   97.56%   97.53%   -0.03%     
==========================================
  Files          16       17       +1     
  Lines        4430     4424       -6     
==========================================
- Hits         4322     4315       -7     
- Misses        108      109       +1     
Flag Coverage Δ
unit 97.53% <91.17%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@fatso83 fatso83 merged commit 2433719 into sinonjs:main Aug 24, 2024
11 checks passed
@fatso83
Copy link
Contributor

fatso83 commented Sep 13, 2024

As soon as this was out someone found an issue: #504

Basically, new dates do not pass the mustard.

image

What could be done to get the instanceof passing? I guess we cannot get the arrow to go both ways ... 😢

@benjamingr
Copy link
Member

What could be done to get the instanceof passing?

We override Symbol.hasInstance I guess

@fatso83
Copy link
Contributor

fatso83 commented Sep 13, 2024

Ooh, you learn something every day! Programmable instanceof, that's some cool shit 😄 I'll create a fix asap.

@fatso83
Copy link
Contributor

fatso83 commented Sep 13, 2024

Worked like a charm, thanks, Ben!
#505

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.

3 participants