Skip to content
This repository has been archived by the owner on Aug 20, 2019. It is now read-only.

toLocaleDateString() results in "Method invoked on an object that is not Date" error in Chrome #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

TylerRick
Copy link

After calling Timecop.install and Timecop.travel, when I then call toLocaleDateString() on any date object, I get this error:

(new Date).toLocaleDateString()
TypeError: Method invoked on an object that is not Date.

(new Date).toLocaleTimeString()
TypeError: Method invoked on an object that is not Date.

The backtrace is:

toLocaleDateTime v8/i18n:1107
Object.defineProperty.value v8/i18n:1132
Timecop.MockDate.(anonymous function) timecop.js:173

You can where the error is defined in the V8 source code here:

function toLocaleDateTime(date, locales, options, required, defaults, service) {
  if (!(date instanceof Date)) {
    throw new TypeError('Method invoked on an object that is not Date.');
  }
  ...

I don't understand it completely, but it looks like even though we called the toLocaleDateTime method on an actual native Date object (this._underlyingDate), the way toLocaleDateTime is defined (Object.defineProperty(Date.prototype, 'toLocaleDateString',...), it ends up calling toLocaleDateTime with a non-native Date object (presumably a Timecop.MockDate object) as the first argument. Hence my proposed workaround in the attached pull request...

I'm using Chromium Version 28.0.1500.52 Ubuntu 12.04 (28.0.1500.52-0ubuntu1.12.04.2).

…ack to the native Date

implementation) before calling the delegate method.

This works around "TypeError: Method invoked on an object that is not Date" errors that we would
otherwise get in Chrome when calling toLocaleDateString().
@jamesarosen
Copy link
Owner

I don't see this problem in a simplistic reduction:

function MockDate(date) {
  this._underlyingDate = date || new Date();
}

MockDate.prototype.toLocaleDateString = function() {
  return this._underlyingDate.toLocaleDateString.apply(this._underlyingDate, arguments);
}

d1 = new MockDate()
d2 = new MockDate(d1)
d2.toLocaleDateString()
"7/16/2013"

That suggests that something else is going wrong.

@TylerRick
Copy link
Author

You might be right. I pasted your simplistic MockDate into the console of my Chrome browser and it worked fine.

I guess I should find a minimal reproduction of the bug first. Don't have time at the moment though...

@jamesarosen
Copy link
Owner

I'll leave this open for a while. It sounds somewhat serious, so if we can find a test case, I'd like to solve it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants