angular/angular.js · error · Error

Method '{}' is not implemented in the TzDate mock

Error message

Method '{}' is not implemented in the TzDate mock

What it means

angular.mock.TzDate is a Date mock with a fixed timezone offset, intended only for testing timezone-dependent reading logic. It deliberately implements a small subset of Date (getTimezoneOffset, getDate, getHours, ...) and replaces every method in unimplementedMethods — mutators (setDate, setTime, ...), formatters (toString, toDateString, toISOString-adjacent, toLocaleString, ...) and toJSON/valueOf — with stubs that throw, because silently wrong results would be worse than a loud failure.

Source

Thrown at src/ngMock/angular-mocks.js:878

            padNumberInMock(self.origDate.getUTCDate(), 2) + 'T' +
            padNumberInMock(self.origDate.getUTCHours(), 2) + ':' +
            padNumberInMock(self.origDate.getUTCMinutes(), 2) + ':' +
            padNumberInMock(self.origDate.getUTCSeconds(), 2) + '.' +
            padNumberInMock(self.origDate.getUTCMilliseconds(), 3) + 'Z';
    };
  }

  //hide all methods not implemented in this mock that the Date prototype exposes
  var unimplementedMethods = ['getUTCDay',
      'getYear', 'setDate', 'setFullYear', 'setHours', 'setMilliseconds',
      'setMinutes', 'setMonth', 'setSeconds', 'setTime', 'setUTCDate', 'setUTCFullYear',
      'setUTCHours', 'setUTCMilliseconds', 'setUTCMinutes', 'setUTCMonth', 'setUTCSeconds',
      'setYear', 'toDateString', 'toGMTString', 'toJSON', 'toLocaleFormat', 'toLocaleString',
      'toLocaleTimeString', 'toSource', 'toString', 'toTimeString', 'toUTCString', 'valueOf'];

  angular.forEach(unimplementedMethods, function(methodName) {
    self[methodName] = function() {
      throw new Error('Method \'' + methodName + '\' is not implemented in the TzDate mock');
    };
  });

  return self;
};

//make "tzDateInstance instanceof Date" return true
angular.mock.TzDate.prototype = Date.prototype;


/**
 * @ngdoc service
 * @name $animate
 *
 * @description
 * Mock implementation of the {@link ng.$animate `$animate`} service. Exposes two additional methods
 * for testing animations.
 *

View on GitHub (pinned to d8f77817eb)

Solutions

  1. Restrict TzDate usage to code paths that only read the implemented getters (getTimezoneOffset, getDate, getDay, getHours, getMinutes, getMonth, getSeconds, getFullYear, getTime, getMilliseconds).
  2. Convert to a real Date before general manipulation: var real = new Date(tzDate.getTime()); then call set*/toString/toJSON on that.
  3. For richer timezone testing, use a real timezone library (moment-timezone) or set the runtime TZ environment instead of TzDate.

Example fix

// before
var tz = new angular.mock.TzDate(-3, '2013-12-11T15:20:00Z');
var json = JSON.stringify({at: tz}); // throws: toJSON not implemented

// after
var tz = new angular.mock.TzDate(-3, '2013-12-11T15:20:00Z');
var json = JSON.stringify({at: new Date(tz.getTime())}); // real Date serializes fine
Defensive patterns

Strategy: type-guard

Validate before calling

// Convert to a real Date before any general Date manipulation
var date = (tzDate instanceof angular.mock.TzDate)
  ? new Date(tzDate.getTime()) // getTime IS implemented
  : tzDate;

Type guard

var TZ_DATE_UNIMPLEMENTED = ['getUTCDay','getYear','setDate','setFullYear','setHours',
  'setMilliseconds','setMinutes','setMonth','setSeconds','setTime','setUTCDate',
  'setUTCFullYear','setUTCHours','setUTCMilliseconds','setUTCMinutes','setUTCMonth',
  'setUTCSeconds','setYear','toDateString','toGMTString','toJSON','toLocaleFormat',
  'toLocaleString','toLocaleTimeString','toSource','toString','toTimeString',
  'toUTCString','valueOf'];

function isTzDateMethodSafe(methodName) {
  return TZ_DATE_UNIMPLEMENTED.indexOf(methodName) === -1;
}

Prevention

When it happens

Trigger: Calling any listed method on a TzDate instance: tzDate.toString() (implicit string coercion), tzDate.setDate(n) or setHours(n) from a date-picker library, tzDate.toJSON() — which means JSON.stringify of an object holding a TzDate throws — or tzDate.valueOf() from arithmetic/comparison.

Common situations: Using TzDate as a drop-in general Date replacement in code under test; passing a model containing TzDate through JSON.stringify or $http request serialization; moment/lodash/date utilities calling valueOf()/set* methods; TzDate.prototype is even set to Date.prototype so instanceof Date passes, making the failure surprising.

Related errors


AI-assisted analysis of angular/angular.js@d8f77817eb (2026-08-21). Data as JSON: /api/errors/c845154b6040f47d. Report an issue: GitHub.