sinonjs / fake-timers

Fake setTimeout and friends (collectively known as "timers"). Useful in your JavaScript tests. Extracted from Sinon.JS
BSD 3-Clause "New" or "Revised" License
793 stars 103 forks source link

How should `shouldAdvanceTime` work together with `Date`? #481

Closed sokyrynskyi closed 9 months ago

sokyrynskyi commented 9 months ago

What did you expect to happen?

Each time new Date() is called -- the value is advanced according to the config.

What actually happens

The value is always the same.

How to reproduce

it('should advance Date', function() {
  const clock = FakeTimers.install({
      now: new Date('2022-02-02 22:22:22').getTime(),
      shouldAdvanceTime: true,
      advanceTimeDelta: 1000,
      toFake: ['Date'],
  });

  console.log('Date1: ', new Date()); // Actual output 'Date1:  2022-02-02T22:22:22.000Z'
  console.log('Date2: ', new Date()); // Actual output 'Date2:  2022-02-02T22:22:22.000Z'

  clock.uninstall();
});
fatso83 commented 9 months ago

Hi, @sokyrynskyi! AFAIK, there is nothing in the docs that says creating a new date instance should advance time.

tells lolex to increment mocked time automatically based on the real system time shift

To be honest, that would be quite contra-intuitive, as you can create several hundred thousands of date instance per second, so deciding what should the granularity be would be a task upon itself.

You can see examples of how this is being used in the docs.