From 29f9547e5355adaa887172d43f35a7a10f202579 Mon Sep 17 00:00:00 2001 From: Carl-Erik Kopseng Date: Thu, 17 Oct 2024 20:30:46 +0200 Subject: [PATCH] Ensure checks based on the constructor property passes for fake Dates (#511) --- src/fake-timers-src.js | 4 ++++ test/fake-timers-test.js | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/src/fake-timers-src.js b/src/fake-timers-src.js index 308daf1..f368628 100644 --- a/src/fake-timers-src.js +++ b/src/fake-timers-src.js @@ -447,6 +447,10 @@ function withGlobal(_global) { } else { super(...arguments); } + + // ensures identity checks using the constructor prop still works + // this should have no other functional effect + this.constructor = Date; } static [Symbol.hasInstance](instance) { diff --git a/test/fake-timers-test.js b/test/fake-timers-test.js index 7a67b9e..38e08e0 100644 --- a/test/fake-timers-test.js +++ b/test/fake-timers-test.js @@ -3200,6 +3200,15 @@ describe("FakeTimers", function () { assert(date instanceof realDate.constructor); }); + // issue #510 + it("creates Date objects where the constructor prop matches the original", function () { + const realDate = new Date(); + const date = new this.clock.Date(); + + assert.equals(date.constructor.name, realDate.constructor.name); + assert.equals(date.constructor, realDate.constructor); + }); + it("creates Date objects representing clock time", function () { const date = new this.clock.Date();