nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: sinonjs/fake-timers
Pull request #512 (2024-10-21 10:12:39)

Fix constructor-based checks for fake Date no longer pass after installing
#### Purpose (TL;DR) - mandatory
Fixes #511 (fix for #510) no longer working after the clock had been installed and global `Date` assigned to the fake.

<!--
> give a concise (one or two short sentences) description of what problem is being solved by this PR
>
> Example: Fix issue #123456 by re-structuring the colour selection conditional in method `paintBlue`
-->

#### Background (Problem in detail)  - optional
Once `ClockDateProxy` was installed to global `Date` (to my surprise) the constructor reference on it was 'updated' to point to `ClockDateProxy`.
<!--
> When relevant, give a more thorough description of what the problem the PR is trying to solve. Examples of good topics for this section are:
> * Link to an existing GitHub issue describing the problem
> * Describing the problem in greater detail than the TL;DR section above
> * How you discovered the issue if it's not already described as an issue on GitHub
> * Discussion of different approaches to solving this problem and why you chose your proposed solution
-->

<!--
#### Solution  - optional
-->
<!--
> When contributing code (and not just fixing typos, documentation and configuration), please describe why/how your solution works. This helps reviewers spot any mistakes in the implementation.
>
> Example:
> "This solution works by adding a `paintBlue()` method"
> Then your reviewer might spot a mistake in the implementation, if `paintBlue()` uses the colour red.
-->

Thanks again for the help!



Patch:
diff --git a/src/fake-timers-src.js b/src/fake-timers-src.js
index f368628..5741f96 100644
--- a/src/fake-timers-src.js
+++ b/src/fake-timers-src.js
@@ -450,7 +450,7 @@ function withGlobal(_global) {
 
                 // ensures identity checks using the constructor prop still works
                 // this should have no other functional effect
-                this.constructor = Date;
+                this.constructor = NativeDate;
             }
 
             static [Symbol.hasInstance](instance) {


Test patch:
diff --git a/test/fake-timers-test.js b/test/fake-timers-test.js
index 38e08e0..ae3e8c6 100644
--- a/test/fake-timers-test.js
+++ b/test/fake-timers-test.js
@@ -3203,6 +3203,9 @@ describe("FakeTimers", function () {
         // issue #510
         it("creates Date objects where the constructor prop matches the original", function () {
             const realDate = new Date();
+            Date = NOOP; // eslint-disable-line no-global-assign
+            global.Date = NOOP;
+
             const date = new this.clock.Date();
 
             assert.equals(date.constructor.name, realDate.constructor.name);
Repository: sinonjs/fake-timers
Pull request #513 (2024-10-28 09:37:56)

Ensure checks based Object.keys length pass for fake Dates
#### Purpose (TL;DR) - mandatory
Fixes sinon.match / samsam equality checks failing between `Date` and fake dates.

<!--
> give a concise (one or two short sentences) description of what problem is being solved by this PR
>
> Example: Fix issue #123456 by re-structuring the colour selection conditional in method `paintBlue`
-->

#### Background
Ran into another small issue with the constructor: samsam's `deepEqual` [checks the number](https://github.com/sinonjs/samsam/blob/1b141a7cb9d54b0d8a1f20812c2db4bc1f1e53a3/lib/deep-equal.js#L154) of properties on the object, and the constructor [should not be enumerable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor) so there should be 0 keys, but fake Dates had one now.

<!--
> When relevant, give a more thorough description of what the problem the PR is trying to solve. Examples of good topics for this section are:
> * Link to an existing GitHub issue describing the problem
> * Describing the problem in greater detail than the TL;DR section above
> * How you discovered the issue if it's not already described as an issue on GitHub
> * Discussion of different approaches to solving this problem and why you chose your proposed solution
-->

<!--
#### Solution  - optional
-->
<!--
> When contributing code (and not just fixing typos, documentation and configuration), please describe why/how your solution works. This helps reviewers spot any mistakes in the implementation.
>
> Example:
> "This solution works by adding a `paintBlue()` method"
> Then your reviewer might spot a mistake in the implementation, if `paintBlue()` uses the colour red.
-->



Patch:
diff --git a/src/fake-timers-src.js b/src/fake-timers-src.js
index 5741f96..b9a8a8d 100644
--- a/src/fake-timers-src.js
+++ b/src/fake-timers-src.js
@@ -450,7 +450,10 @@ function withGlobal(_global) {
 
                 // ensures identity checks using the constructor prop still works
                 // this should have no other functional effect
-                this.constructor = NativeDate;
+                Object.defineProperty(this, "constructor", {
+                    value: NativeDate,
+                    enumerable: false,
+                });
             }
 
             static [Symbol.hasInstance](instance) {


Test patch:
diff --git a/test/fake-timers-test.js b/test/fake-timers-test.js
index ae3e8c6..0a4b676 100644
--- a/test/fake-timers-test.js
+++ b/test/fake-timers-test.js
@@ -3212,6 +3212,12 @@ describe("FakeTimers", function () {
             assert.equals(date.constructor, realDate.constructor);
         });
 
+        it("creates Date objects where the constructor prop is not enumerable", function () {
+            const date = new this.clock.Date();
+
+            assert.equals(Object.keys(date).length, 0);
+        });
+
         it("creates Date objects representing clock time", function () {
             const date = new this.clock.Date();
Repository: sinonjs/lolex
Pull request #70 (2016-06-14 16:11:37)

fixed a case where runAll was called and there are no timers

fatso83: Had to run it without your fixes to see that this really wasn't fixed already, and lo and behold: it didn't. So thanks! 

Patch:
diff --git a/src/lolex-src.js b/src/lolex-src.js
index b2bcab4..c1a5196 100644
--- a/src/lolex-src.js
+++ b/src/lolex-src.js
@@ -542,6 +542,10 @@
         clock.runAll = function runAll() {
             var numTimers, i;
             for (i = 0; i < clock.loopLimit; i++) {
+                if (!clock.timers) {
+                    return clock.now;
+                }
+
                 numTimers = Object.keys(clock.timers).length;
                 if (numTimers === 0) {
                     return clock.now;


Test patch:
diff --git a/test/lolex-test.js b/test/lolex-test.js
index 4730a40..3fda1d7 100644
--- a/test/lolex-test.js
+++ b/test/lolex-test.js
@@ -803,6 +803,11 @@ describe("lolex", function () {
 
     describe('runAll', function() {
 
+        it('if there are no timers just return', function() {
+            this.clock = lolex.createClock();
+            this.clock.runAll();
+        });
+
         it('runs all timers', function() {
             this.clock = lolex.createClock();
             var spies = [sinon.spy(), sinon.spy()];
Repository: sinonjs/lolex
Pull request #77 (2016-07-19 20:24:11)

Fix setInterval() behavior with string times
In Node.js, when setTimeout is called with an time string, it is parsed. If the string is an invalid number, it's assumed as 0. In lolex, using a string as time causes the setTimeout not to trigger the callback function.

This is the behaviour that I would expect:

```js
it("parses no-numeric string times", function () {
    this.clock.setTimeout(function () { lolex.evalCalled = true; }, "string");
    this.clock.tick(10);

    assert(lolex.evalCalled);
});

it("evals non-function callbacks", function () {
    this.clock.setTimeout("lolex.evalCalled = true", 10);
    this.clock.tick(10);

    assert(lolex.evalCalled);
});
```


Patch:
diff --git a/src/lolex-src.js b/src/lolex-src.js
index 5017a47..8ee5b34 100644
--- a/src/lolex-src.js
+++ b/src/lolex-src.js
@@ -173,7 +173,7 @@
 
         timer.id = uniqueTimerId++;
         timer.createdAt = clock.now;
-        timer.callAt = clock.now + (timer.delay || (clock.duringTick ? 1 : 0));
+        timer.callAt = clock.now + (parseInt(timer.delay) || (clock.duringTick ? 1 : 0));
 
         clock.timers[timer.id] = timer;
 


Test patch:
diff --git a/test/lolex-test.js b/test/lolex-test.js
index 70388ba..2fe03d5 100644
--- a/test/lolex-test.js
+++ b/test/lolex-test.js
@@ -90,6 +90,20 @@ describe("lolex", function () {
             assert(stubs[1].called);
         });
 
+        it("parses numeric string times", function () {
+            this.clock.setTimeout(function () { lolex.evalCalled = true; }, "10");
+            this.clock.tick(10);
+
+            assert(lolex.evalCalled);
+        });
+
+        it("parses no-numeric string times", function () {
+            this.clock.setTimeout(function () { lolex.evalCalled = true; }, "string");
+            this.clock.tick(10);
+
+            assert(lolex.evalCalled);
+        });
+
         it("evals non-function callbacks", function () {
             this.clock.setTimeout("lolex.evalCalled = true", 10);
             this.clock.tick(10);
Repository: sinonjs/lolex
Pull request #208 (2018-09-17 17:02:53)

fix: runToLast should respect current time
#### Purpose (TL;DR) - mandatory
Without the code change, the added test makes node hangs forever until it runs out of memory.

#### Background (Problem in detail)  - optional
`clock.tick` expect the argument to be how many millis it should tick. Currently, we pass it the timestamp of when it should be done ticking.

https://github.com/sinonjs/lolex/blob/1278eaade7da311d203fc6c33f67f85c828a09f2/src/lolex-src.js#L723

I don't understand how no-one hit this bug for 2.5 years... It's been that way since the original implementation: #50.

#### Solution

Subtract the current time on the clock before calling `tick`. The first thing tick does is add it back, so it knows when to stop.

https://github.com/sinonjs/lolex/blob/1278eaade7da311d203fc6c33f67f85c828a09f2/src/lolex-src.js#L603

We might want a `tickTo` method? Implementation would be `this.tick(to - clock.now)`, so maybe not valuable.


Patch:
diff --git a/src/lolex-src.js b/src/lolex-src.js
index 14f55ca..0ade09c 100644
--- a/src/lolex-src.js
+++ b/src/lolex-src.js
@@ -720,7 +720,7 @@ function withGlobal(_global) {
                 return clock.now;
             }
 
-            return clock.tick(timer.callAt);
+            return clock.tick(timer.callAt - clock.now);
         };
 
         clock.reset = function reset() {


Test patch:
diff --git a/test/lolex-test.js b/test/lolex-test.js
index 6684870..b6b7a6a 100644
--- a/test/lolex-test.js
+++ b/test/lolex-test.js
@@ -1222,6 +1222,21 @@ describe("lolex", function () {
             assert.isTrue(spy.called);
         });
 
+        it("should support clocks with start time", function () {
+            this.clock = lolex.createClock(200);
+            var that = this;
+            var invocations = 0;
+
+            this.clock.setTimeout(function cb() {
+                invocations++;
+                that.clock.setTimeout(cb, 50);
+            }, 50);
+
+            this.clock.runToLast();
+
+            assert.equals(invocations, 1);
+        });
+
     });
 
     describe("clearTimeout", function () {