Stub Get TimeZone Offset Method

Let's say the application gets the current timezone using the method new Date().getTimezoneOffset() call. How do we stub it?

<output id="timezone" />
  setTimeout(() => {
    const tz = (new Date().getTimezoneOffset() / 60) * -1
    ).innerText = `TZ ${tz} hours`
  }, 100)

The method getTimezoneOffset comes from Date.prototype object.

  .then((datePrototype) => {
    cy.stub(datePrototype, 'getTimezoneOffset')
cy.contains('output', 'TZ -15 hours')

Confirm the method stub was called

  // optional: reset the stub to restore the original timezone

See also