diff --git a/CHANGELOG.md b/CHANGELOG.md index d7bf54d21..a8d1cb7ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,13 @@ + +## [0.8.26](https://github.com/angular/zone.js/compare/v0.8.25...0.8.26) (2018-04-08) + + +### Bug Fixes + +* **test:** fix [#1069](https://github.com/angular/zone.js/issues/1069), FakeDate should handle constructor parameter ([#1070](https://github.com/angular/zone.js/issues/1070)) ([b3fdd7e](https://github.com/angular/zone.js/commit/b3fdd7e)) + + + ## [0.8.25](https://github.com/angular/zone.js/compare/v0.8.24...0.8.25) (2018-04-04) diff --git a/dist/fake-async-test.js b/dist/fake-async-test.js index c9893b691..9bc27674e 100644 --- a/dist/fake-async-test.js +++ b/dist/fake-async-test.js @@ -18,17 +18,40 @@ * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ +var __read = (undefined && undefined.__read) || function (o, n) { + var m = typeof Symbol === "function" && o[Symbol.iterator]; + if (!m) return o; + var i = m.call(o), r, ar = [], e; + try { + while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); + } + catch (error) { e = { error: error }; } + finally { + try { + if (r && !r.done && (m = i["return"])) m.call(i); + } + finally { if (e) throw e.error; } + } + return ar; +}; +var __spread = (undefined && undefined.__spread) || function () { + for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); + return ar; +}; (function (global) { var OriginalDate = global.Date; var FakeDate = /** @class */ (function () { function FakeDate() { - var d = new OriginalDate(); - d.setTime(global.Date.now()); - return d; + if (arguments.length === 0) { + var d = new OriginalDate(); + d.setTime(FakeDate.now()); + return d; + } + else { + var args = Array.prototype.slice.call(arguments); + return new (OriginalDate.bind.apply(OriginalDate, __spread([void 0], args)))(); + } } - FakeDate.UTC = function () { - return OriginalDate.UTC(); - }; FakeDate.now = function () { var fakeAsyncTestZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec'); if (fakeAsyncTestZoneSpec) { @@ -36,11 +59,17 @@ } return OriginalDate.now.apply(this, arguments); }; - FakeDate.parse = function () { - return OriginalDate.parse(); - }; return FakeDate; }()); + FakeDate.UTC = OriginalDate.UTC; + FakeDate.parse = OriginalDate.parse; + // keep a reference for zone patched timer function + var timers = { + setTimeout: global.setTimeout, + setInterval: global.setInterval, + clearTimeout: global.clearTimeout, + clearInterval: global.clearInterval + }; var Scheduler = /** @class */ (function () { function Scheduler() { // Next scheduler id. @@ -50,7 +79,7 @@ // Current simulated time in millis. this._currentTime = 0; // Current real time in millis. - this._currentRealTime = Date.now(); + this._currentRealTime = OriginalDate.now(); } Scheduler.prototype.getCurrentTime = function () { return this._currentTime; @@ -309,12 +338,26 @@ } global['Date'] = FakeDate; FakeDate.prototype = OriginalDate.prototype; + // try check and reset timers + // because jasmine.clock().install() may + // have replaced the global timer + FakeAsyncTestZoneSpec.checkTimerPatch(); }; FakeAsyncTestZoneSpec.resetDate = function () { if (global['Date'] === FakeDate) { global['Date'] = OriginalDate; } }; + FakeAsyncTestZoneSpec.checkTimerPatch = function () { + if (global.setTimeout !== timers.setTimeout) { + global.setTimeout = timers.setTimeout; + global.clearTimeout = timers.clearTimeout; + } + if (global.setInterval !== timers.setInterval) { + global.setInterval = timers.setInterval; + global.clearInterval = timers.clearInterval; + } + }; FakeAsyncTestZoneSpec.prototype.lockDatePatch = function () { this.patchDateLocked = true; FakeAsyncTestZoneSpec.patchDate(); diff --git a/dist/jasmine-patch.js b/dist/jasmine-patch.js index 2b69773c8..33e809a72 100644 --- a/dist/jasmine-patch.js +++ b/dist/jasmine-patch.js @@ -76,42 +76,48 @@ return originalJasmineFn.apply(this, arguments); }; }); - if (enableClockPatch) { - var originalClockFn_1 = (jasmine[symbol('clock')] = jasmine['clock']); - jasmine['clock'] = function () { - var clock = originalClockFn_1.apply(this, arguments); - var originalTick = (clock[symbol('tick')] = clock.tick); + // need to patch jasmine.clock().mockDate and jasmine.clock().tick() so + // they can work properly in FakeAsyncTest + var originalClockFn = (jasmine[symbol('clock')] = jasmine['clock']); + jasmine['clock'] = function () { + var clock = originalClockFn.apply(this, arguments); + if (!clock[symbol('patched')]) { + clock[symbol('patched')] = symbol('patched'); + var originalTick_1 = (clock[symbol('tick')] = clock.tick); clock.tick = function () { var fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec'); if (fakeAsyncZoneSpec) { return fakeAsyncZoneSpec.tick.apply(fakeAsyncZoneSpec, arguments); } - return originalTick.apply(this, arguments); + return originalTick_1.apply(this, arguments); }; - var originalMockDate = (clock[symbol('mockDate')] = clock.mockDate); + var originalMockDate_1 = (clock[symbol('mockDate')] = clock.mockDate); clock.mockDate = function () { var fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec'); if (fakeAsyncZoneSpec) { - var dateTime = arguments[0]; + var dateTime = arguments.length > 0 ? arguments[0] : new Date(); return fakeAsyncZoneSpec.setCurrentRealTime.apply(fakeAsyncZoneSpec, dateTime && typeof dateTime.getTime === 'function' ? [dateTime.getTime()] : arguments); } - return originalMockDate.apply(this, arguments); + return originalMockDate_1.apply(this, arguments); }; - ['install', 'uninstall'].forEach(function (methodName) { - var originalClockFn = (clock[symbol(methodName)] = clock[methodName]); - clock[methodName] = function () { - var FakeAsyncTestZoneSpec = Zone['FakeAsyncTestZoneSpec']; - if (FakeAsyncTestZoneSpec) { - jasmine[symbol('clockInstalled')] = 'install' === methodName; - return; - } - return originalClockFn.apply(this, arguments); - }; - }); - return clock; - }; - } + // for auto go into fakeAsync feature, we need the flag to enable it + if (enableClockPatch) { + ['install', 'uninstall'].forEach(function (methodName) { + var originalClockFn = (clock[symbol(methodName)] = clock[methodName]); + clock[methodName] = function () { + var FakeAsyncTestZoneSpec = Zone['FakeAsyncTestZoneSpec']; + if (FakeAsyncTestZoneSpec) { + jasmine[symbol('clockInstalled')] = 'install' === methodName; + return; + } + return originalClockFn.apply(this, arguments); + }; + }); + } + } + return clock; + }; /** * Gets a function wrapping the body of a Jasmine `describe` block to execute in a * synchronous-only zone. diff --git a/dist/jasmine-patch.min.js b/dist/jasmine-patch.min.js index 8af9c4493..e78cb748a 100644 --- a/dist/jasmine-patch.min.js +++ b/dist/jasmine-patch.min.js @@ -1 +1 @@ -!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n():"function"==typeof define&&define.amd?define(n):n()}(this,function(){"use strict";!function(){function e(e){return function(){return u.run(e,this,arguments)}}function n(e,n,t,o){var r=!!jasmine[a("clockInstalled")],i=(t.testProxyZoneSpec,t.testProxyZone);if(r&&f){var s=Zone[Zone.__symbol__("fakeAsyncTest")];s&&"function"==typeof s.fakeAsync&&(e=s.fakeAsync(e))}return o?i.run(e,n,[o]):i.run(e,n)}function t(e){return e&&(e.length?function(t){return n(e,this,this.queueRunner,t)}:function(){return n(e,this,this.queueRunner)})}var o=function(e,n){function t(){this.constructor=e}for(var o in n)n.hasOwnProperty(o)&&(e[o]=n[o]);e.prototype=null===n?Object.create(n):(t.prototype=n.prototype,new t)},r="undefined"!=typeof window&&window||"undefined"!=typeof self&&self||global;if(!Zone)throw new Error("Missing: zone.js");if("undefined"==typeof jasmine)throw new Error("Missing: jasmine.js");if(jasmine.__zone_patch__)throw new Error("'jasmine' has already been patched with 'Zone'.");jasmine.__zone_patch__=!0;var i=Zone.SyncTestZoneSpec,s=Zone.ProxyZoneSpec;if(!i)throw new Error("Missing: SyncTestZoneSpec");if(!s)throw new Error("Missing: ProxyZoneSpec");var c=Zone.current,u=c.fork(new i("jasmine.describe")),a=Zone.__symbol__,f=r[a("fakeAsyncPatchLock")]===!0,l=jasmine.getEnv();if(["describe","xdescribe","fdescribe"].forEach(function(n){var t=l[n];l[n]=function(n,o){return t.call(this,n,e(o))}}),["it","xit","fit"].forEach(function(e){var n=l[e];l[a(e)]=n,l[e]=function(e,o,r){return arguments[1]=t(o),n.apply(this,arguments)}}),["beforeEach","afterEach"].forEach(function(e){var n=l[e];l[a(e)]=n,l[e]=function(e,o){return arguments[0]=t(e),n.apply(this,arguments)}}),f){var p=jasmine[a("clock")]=jasmine.clock;jasmine.clock=function(){var e=p.apply(this,arguments),n=e[a("tick")]=e.tick;e.tick=function(){var e=Zone.current.get("FakeAsyncTestZoneSpec");return e?e.tick.apply(e,arguments):n.apply(this,arguments)};var t=e[a("mockDate")]=e.mockDate;return e.mockDate=function(){var e=Zone.current.get("FakeAsyncTestZoneSpec");if(e){var n=arguments[0];return e.setCurrentRealTime.apply(e,n&&"function"==typeof n.getTime?[n.getTime()]:arguments)}return t.apply(this,arguments)},["install","uninstall"].forEach(function(n){var t=e[a(n)]=e[n];e[n]=function(){var e=Zone.FakeAsyncTestZoneSpec;return e?void(jasmine[a("clockInstalled")]="install"===n):t.apply(this,arguments)}}),e}}var h=jasmine.QueueRunner;jasmine.QueueRunner=function(e){function n(n){var t=this;n.onComplete=function(e){return function(){t.testProxyZone=null,t.testProxyZoneSpec=null,c.scheduleMicroTask("jasmine.onComplete",e)}}(n.onComplete);var o=r.__zone_symbol__setTimeout,i=r.__zone_symbol__clearTimeout;o&&(n.timeout={setTimeout:o?o:r.setTimeout,clearTimeout:i?i:r.clearTimeout}),jasmine.UserContext?(n.userContext||(n.userContext=new jasmine.UserContext),n.userContext.queueRunner=this):(n.userContext||(n.userContext={}),n.userContext.queueRunner=this);var s=n.onException;n.onException=function(e){if(e&&"Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL."===e.message){var n=this&&this.testProxyZoneSpec;if(n){var t=n.getAndClearPendingTasksInfo();e.message+=t}}s&&s.call(this,e)},e.call(this,n)}return o(n,e),n.prototype.execute=function(){for(var n=this,t=Zone.current,o=!1;t;){if(t===c){o=!0;break}t=t.parent}if(!o)throw new Error("Unexpected Zone: "+Zone.current.name);this.testProxyZoneSpec=new s,this.testProxyZone=c.fork(this.testProxyZoneSpec),Zone.currentTask?e.prototype.execute.call(this):Zone.current.scheduleMicroTask("jasmine.execute().forceTask",function(){return h.prototype.execute.call(n)})},n}(h)}()}); \ No newline at end of file +!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n():"function"==typeof define&&define.amd?define(n):n()}(this,function(){"use strict";!function(){function e(e){return function(){return a.run(e,this,arguments)}}function n(e,n,t,o){var r=!!jasmine[u("clockInstalled")],i=(t.testProxyZoneSpec,t.testProxyZone);if(r&&f){var c=Zone[Zone.__symbol__("fakeAsyncTest")];c&&"function"==typeof c.fakeAsync&&(e=c.fakeAsync(e))}return o?i.run(e,n,[o]):i.run(e,n)}function t(e){return e&&(e.length?function(t){return n(e,this,this.queueRunner,t)}:function(){return n(e,this,this.queueRunner)})}var o=function(e,n){function t(){this.constructor=e}for(var o in n)n.hasOwnProperty(o)&&(e[o]=n[o]);e.prototype=null===n?Object.create(n):(t.prototype=n.prototype,new t)},r="undefined"!=typeof window&&window||"undefined"!=typeof self&&self||global;if(!Zone)throw new Error("Missing: zone.js");if("undefined"==typeof jasmine)throw new Error("Missing: jasmine.js");if(jasmine.__zone_patch__)throw new Error("'jasmine' has already been patched with 'Zone'.");jasmine.__zone_patch__=!0;var i=Zone.SyncTestZoneSpec,c=Zone.ProxyZoneSpec;if(!i)throw new Error("Missing: SyncTestZoneSpec");if(!c)throw new Error("Missing: ProxyZoneSpec");var s=Zone.current,a=s.fork(new i("jasmine.describe")),u=Zone.__symbol__,f=r[u("fakeAsyncPatchLock")]===!0,p=jasmine.getEnv();["describe","xdescribe","fdescribe"].forEach(function(n){var t=p[n];p[n]=function(n,o){return t.call(this,n,e(o))}}),["it","xit","fit"].forEach(function(e){var n=p[e];p[u(e)]=n,p[e]=function(e,o,r){return arguments[1]=t(o),n.apply(this,arguments)}}),["beforeEach","afterEach"].forEach(function(e){var n=p[e];p[u(e)]=n,p[e]=function(e,o){return arguments[0]=t(e),n.apply(this,arguments)}});var l=jasmine[u("clock")]=jasmine.clock;jasmine.clock=function(){var e=l.apply(this,arguments);if(!e[u("patched")]){e[u("patched")]=u("patched");var n=e[u("tick")]=e.tick;e.tick=function(){var e=Zone.current.get("FakeAsyncTestZoneSpec");return e?e.tick.apply(e,arguments):n.apply(this,arguments)};var t=e[u("mockDate")]=e.mockDate;e.mockDate=function(){var e=Zone.current.get("FakeAsyncTestZoneSpec");if(e){var n=arguments.length>0?arguments[0]:new Date;return e.setCurrentRealTime.apply(e,n&&"function"==typeof n.getTime?[n.getTime()]:arguments)}return t.apply(this,arguments)},f&&["install","uninstall"].forEach(function(n){var t=e[u(n)]=e[n];e[n]=function(){var e=Zone.FakeAsyncTestZoneSpec;return e?void(jasmine[u("clockInstalled")]="install"===n):t.apply(this,arguments)}})}return e};var h=jasmine.QueueRunner;jasmine.QueueRunner=function(e){function n(n){var t=this;n.onComplete=function(e){return function(){t.testProxyZone=null,t.testProxyZoneSpec=null,s.scheduleMicroTask("jasmine.onComplete",e)}}(n.onComplete);var o=r.__zone_symbol__setTimeout,i=r.__zone_symbol__clearTimeout;o&&(n.timeout={setTimeout:o?o:r.setTimeout,clearTimeout:i?i:r.clearTimeout}),jasmine.UserContext?(n.userContext||(n.userContext=new jasmine.UserContext),n.userContext.queueRunner=this):(n.userContext||(n.userContext={}),n.userContext.queueRunner=this);var c=n.onException;n.onException=function(e){if(e&&"Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL."===e.message){var n=this&&this.testProxyZoneSpec;if(n){var t=n.getAndClearPendingTasksInfo();e.message+=t}}c&&c.call(this,e)},e.call(this,n)}return o(n,e),n.prototype.execute=function(){for(var n=this,t=Zone.current,o=!1;t;){if(t===s){o=!0;break}t=t.parent}if(!o)throw new Error("Unexpected Zone: "+Zone.current.name);this.testProxyZoneSpec=new c,this.testProxyZone=s.fork(this.testProxyZoneSpec),Zone.currentTask?e.prototype.execute.call(this):Zone.current.scheduleMicroTask("jasmine.execute().forceTask",function(){return h.prototype.execute.call(n)})},n}(h)}()}); \ No newline at end of file diff --git a/dist/zone-patch-resize-observer.js b/dist/zone-patch-resize-observer.js index b0cabc088..e80c32588 100644 --- a/dist/zone-patch-resize-observer.js +++ b/dist/zone-patch-resize-observer.js @@ -11,6 +11,16 @@ (factory()); }(this, (function () { 'use strict'; +var __values = (undefined && undefined.__values) || function (o) { + var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0; + if (m) return m.call(o); + return { + next: function () { + if (o && i >= o.length) o = void 0; + return { value: o && o[i++], done: !o }; + } + }; +}; /** * @license * Copyright Google Inc. All Rights Reserved. @@ -31,17 +41,26 @@ Zone.__load_patch('ResizeObserver', function (global, Zone, api) { var _this = this; var zones = {}; var currZone = Zone.current; - for (var _i = 0, entries_1 = entries; _i < entries_1.length; _i++) { - var entry = entries_1[_i]; - var zone = entry.target[resizeObserverSymbol]; - if (!zone) { - zone = currZone; + try { + for (var entries_1 = __values(entries), entries_1_1 = entries_1.next(); !entries_1_1.done; entries_1_1 = entries_1.next()) { + var entry = entries_1_1.value; + var zone = entry.target[resizeObserverSymbol]; + if (!zone) { + zone = currZone; + } + var zoneEntriesInfo = zones[zone.name]; + if (!zoneEntriesInfo) { + zones[zone.name] = zoneEntriesInfo = { entries: [], zone: zone }; + } + zoneEntriesInfo.entries.push(entry); } - var zoneEntriesInfo = zones[zone.name]; - if (!zoneEntriesInfo) { - zones[zone.name] = zoneEntriesInfo = { entries: [], zone: zone }; + } + catch (e_1_1) { e_1 = { error: e_1_1 }; } + finally { + try { + if (entries_1_1 && !entries_1_1.done && (_a = entries_1.return)) _a.call(entries_1); } - zoneEntriesInfo.entries.push(entry); + finally { if (e_1) throw e_1.error; } } Object.keys(zones).forEach(function (zoneName) { var zoneEntriesInfo = zones[zoneName]; @@ -52,6 +71,7 @@ Zone.__load_patch('ResizeObserver', function (global, Zone, api) { callback.call(_this, zoneEntriesInfo.entries, observer); } }); + var e_1, _a; }; } return args.length > 0 ? new ResizeObserver(args[0]) : new ResizeObserver(); diff --git a/dist/zone-patch-resize-observer.min.js b/dist/zone-patch-resize-observer.min.js index bffe2a10a..4e3ef0583 100644 --- a/dist/zone-patch-resize-observer.min.js +++ b/dist/zone-patch-resize-observer.min.js @@ -1 +1 @@ -!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n():"function"==typeof define&&define.amd?define(n):n()}(this,function(){"use strict";Zone.__load_patch("ResizeObserver",function(e,n,r){var t=e.ResizeObserver;if(t){var o=r.symbol("ResizeObserver");r.patchMethod(e,"ResizeObserver",function(e){return function(e,r){var i=r.length>0?r[0]:null;return i&&(r[0]=function(e,r){for(var t=this,u={},a=n.current,c=0,f=e;c0?new t(r[0]):new t}}),r.patchMethod(t.prototype,"observe",function(e){return function(r,t){var i=t.length>0?t[0]:null;if(!i)return e.apply(r,t);var u=r[o];return u||(u=r[o]=[]),u.push(i),i[o]=n.current,e.apply(r,t)}}),r.patchMethod(t.prototype,"unobserve",function(e){return function(n,r){var t=r.length>0?r[0]:null;if(!t)return e.apply(n,r);var i=n[o];if(i)for(var u=0;u=e.length&&(e=void 0),{value:e&&e[r++],done:!e}}}};Zone.__load_patch("ResizeObserver",function(n,r,t){var o=n.ResizeObserver;if(o){var i=t.symbol("ResizeObserver");t.patchMethod(n,"ResizeObserver",function(n){return function(n,t){var u=t.length>0?t[0]:null;return u&&(t[0]=function(n,t){var o=this,a={},c=r.current;try{for(var f=e(n),l=f.next();!l.done;l=f.next()){var v=l.value,p=v.target[i];p||(p=c);var s=a[p.name];s||(a[p.name]=s={entries:[],zone:p}),s.entries.push(v)}}catch(h){d={error:h}}finally{try{l&&!l.done&&(y=f["return"])&&y.call(f)}finally{if(d)throw d.error}}Object.keys(a).forEach(function(e){var n=a[e];n.zone!==r.current?n.zone.run(u,o,[n.entries,t],"ResizeObserver"):u.call(o,n.entries,t)});var d,y}),t.length>0?new o(t[0]):new o}}),t.patchMethod(o.prototype,"observe",function(e){return function(n,t){var o=t.length>0?t[0]:null;if(!o)return e.apply(n,t);var u=n[i];return u||(u=n[i]=[]),u.push(o),o[i]=r.current,e.apply(n,t)}}),t.patchMethod(o.prototype,"unobserve",function(e){return function(n,r){var t=r.length>0?r[0]:null;if(!t)return e.apply(n,r);var o=n[i];if(o)for(var u=0;u 0 ? arguments[0] : new Date(); return fakeAsyncZoneSpec.setCurrentRealTime.apply(fakeAsyncZoneSpec, dateTime && typeof dateTime.getTime === 'function' ? [dateTime.getTime()] : arguments); } - return originalMockDate.apply(this, arguments); + return originalMockDate_1.apply(this, arguments); }; - ['install', 'uninstall'].forEach(function (methodName) { - var originalClockFn = (clock[symbol(methodName)] = clock[methodName]); - clock[methodName] = function () { - var FakeAsyncTestZoneSpec = Zone['FakeAsyncTestZoneSpec']; - if (FakeAsyncTestZoneSpec) { - jasmine[symbol('clockInstalled')] = 'install' === methodName; - return; - } - return originalClockFn.apply(this, arguments); - }; - }); - return clock; - }; - } + // for auto go into fakeAsync feature, we need the flag to enable it + if (enableClockPatch) { + ['install', 'uninstall'].forEach(function (methodName) { + var originalClockFn = (clock[symbol(methodName)] = clock[methodName]); + clock[methodName] = function () { + var FakeAsyncTestZoneSpec = Zone['FakeAsyncTestZoneSpec']; + if (FakeAsyncTestZoneSpec) { + jasmine[symbol('clockInstalled')] = 'install' === methodName; + return; + } + return originalClockFn.apply(this, arguments); + }; + }); + } + } + return clock; + }; /** * Gets a function wrapping the body of a Jasmine `describe` block to execute in a * synchronous-only zone. @@ -3889,17 +3895,40 @@ Zone.__load_patch('asynctest', function (global, Zone, api) { * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ +var __read = (undefined && undefined.__read) || function (o, n) { + var m = typeof Symbol === "function" && o[Symbol.iterator]; + if (!m) return o; + var i = m.call(o), r, ar = [], e; + try { + while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); + } + catch (error) { e = { error: error }; } + finally { + try { + if (r && !r.done && (m = i["return"])) m.call(i); + } + finally { if (e) throw e.error; } + } + return ar; +}; +var __spread = (undefined && undefined.__spread) || function () { + for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); + return ar; +}; (function (global) { var OriginalDate = global.Date; var FakeDate = /** @class */ (function () { function FakeDate() { - var d = new OriginalDate(); - d.setTime(global.Date.now()); - return d; + if (arguments.length === 0) { + var d = new OriginalDate(); + d.setTime(FakeDate.now()); + return d; + } + else { + var args = Array.prototype.slice.call(arguments); + return new (OriginalDate.bind.apply(OriginalDate, __spread([void 0], args)))(); + } } - FakeDate.UTC = function () { - return OriginalDate.UTC(); - }; FakeDate.now = function () { var fakeAsyncTestZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec'); if (fakeAsyncTestZoneSpec) { @@ -3907,11 +3936,17 @@ Zone.__load_patch('asynctest', function (global, Zone, api) { } return OriginalDate.now.apply(this, arguments); }; - FakeDate.parse = function () { - return OriginalDate.parse(); - }; return FakeDate; }()); + FakeDate.UTC = OriginalDate.UTC; + FakeDate.parse = OriginalDate.parse; + // keep a reference for zone patched timer function + var timers = { + setTimeout: global.setTimeout, + setInterval: global.setInterval, + clearTimeout: global.clearTimeout, + clearInterval: global.clearInterval + }; var Scheduler = /** @class */ (function () { function Scheduler() { // Next scheduler id. @@ -3921,7 +3956,7 @@ Zone.__load_patch('asynctest', function (global, Zone, api) { // Current simulated time in millis. this._currentTime = 0; // Current real time in millis. - this._currentRealTime = Date.now(); + this._currentRealTime = OriginalDate.now(); } Scheduler.prototype.getCurrentTime = function () { return this._currentTime; @@ -4180,12 +4215,26 @@ Zone.__load_patch('asynctest', function (global, Zone, api) { } global['Date'] = FakeDate; FakeDate.prototype = OriginalDate.prototype; + // try check and reset timers + // because jasmine.clock().install() may + // have replaced the global timer + FakeAsyncTestZoneSpec.checkTimerPatch(); }; FakeAsyncTestZoneSpec.resetDate = function () { if (global['Date'] === FakeDate) { global['Date'] = OriginalDate; } }; + FakeAsyncTestZoneSpec.checkTimerPatch = function () { + if (global.setTimeout !== timers.setTimeout) { + global.setTimeout = timers.setTimeout; + global.clearTimeout = timers.clearTimeout; + } + if (global.setInterval !== timers.setInterval) { + global.setInterval = timers.setInterval; + global.clearInterval = timers.clearInterval; + } + }; FakeAsyncTestZoneSpec.prototype.lockDatePatch = function () { this.patchDateLocked = true; FakeAsyncTestZoneSpec.patchDate(); diff --git a/dist/zone-testing-node-bundle.js b/dist/zone-testing-node-bundle.js index 5460dfc8c..a95fbf996 100644 --- a/dist/zone-testing-node-bundle.js +++ b/dist/zone-testing-node-bundle.js @@ -2724,42 +2724,48 @@ Zone['SyncTestZoneSpec'] = SyncTestZoneSpec; return originalJasmineFn.apply(this, arguments); }; }); - if (enableClockPatch) { - var originalClockFn_1 = (jasmine[symbol('clock')] = jasmine['clock']); - jasmine['clock'] = function () { - var clock = originalClockFn_1.apply(this, arguments); - var originalTick = (clock[symbol('tick')] = clock.tick); + // need to patch jasmine.clock().mockDate and jasmine.clock().tick() so + // they can work properly in FakeAsyncTest + var originalClockFn = (jasmine[symbol('clock')] = jasmine['clock']); + jasmine['clock'] = function () { + var clock = originalClockFn.apply(this, arguments); + if (!clock[symbol('patched')]) { + clock[symbol('patched')] = symbol('patched'); + var originalTick_1 = (clock[symbol('tick')] = clock.tick); clock.tick = function () { var fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec'); if (fakeAsyncZoneSpec) { return fakeAsyncZoneSpec.tick.apply(fakeAsyncZoneSpec, arguments); } - return originalTick.apply(this, arguments); + return originalTick_1.apply(this, arguments); }; - var originalMockDate = (clock[symbol('mockDate')] = clock.mockDate); + var originalMockDate_1 = (clock[symbol('mockDate')] = clock.mockDate); clock.mockDate = function () { var fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec'); if (fakeAsyncZoneSpec) { - var dateTime = arguments[0]; + var dateTime = arguments.length > 0 ? arguments[0] : new Date(); return fakeAsyncZoneSpec.setCurrentRealTime.apply(fakeAsyncZoneSpec, dateTime && typeof dateTime.getTime === 'function' ? [dateTime.getTime()] : arguments); } - return originalMockDate.apply(this, arguments); + return originalMockDate_1.apply(this, arguments); }; - ['install', 'uninstall'].forEach(function (methodName) { - var originalClockFn = (clock[symbol(methodName)] = clock[methodName]); - clock[methodName] = function () { - var FakeAsyncTestZoneSpec = Zone['FakeAsyncTestZoneSpec']; - if (FakeAsyncTestZoneSpec) { - jasmine[symbol('clockInstalled')] = 'install' === methodName; - return; - } - return originalClockFn.apply(this, arguments); - }; - }); - return clock; - }; - } + // for auto go into fakeAsync feature, we need the flag to enable it + if (enableClockPatch) { + ['install', 'uninstall'].forEach(function (methodName) { + var originalClockFn = (clock[symbol(methodName)] = clock[methodName]); + clock[methodName] = function () { + var FakeAsyncTestZoneSpec = Zone['FakeAsyncTestZoneSpec']; + if (FakeAsyncTestZoneSpec) { + jasmine[symbol('clockInstalled')] = 'install' === methodName; + return; + } + return originalClockFn.apply(this, arguments); + }; + }); + } + } + return clock; + }; /** * Gets a function wrapping the body of a Jasmine `describe` block to execute in a * synchronous-only zone. @@ -3127,17 +3133,40 @@ Zone.__load_patch('asynctest', function (global, Zone, api) { * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ +var __read = (undefined && undefined.__read) || function (o, n) { + var m = typeof Symbol === "function" && o[Symbol.iterator]; + if (!m) return o; + var i = m.call(o), r, ar = [], e; + try { + while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); + } + catch (error) { e = { error: error }; } + finally { + try { + if (r && !r.done && (m = i["return"])) m.call(i); + } + finally { if (e) throw e.error; } + } + return ar; +}; +var __spread = (undefined && undefined.__spread) || function () { + for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); + return ar; +}; (function (global) { var OriginalDate = global.Date; var FakeDate = /** @class */ (function () { function FakeDate() { - var d = new OriginalDate(); - d.setTime(global.Date.now()); - return d; + if (arguments.length === 0) { + var d = new OriginalDate(); + d.setTime(FakeDate.now()); + return d; + } + else { + var args = Array.prototype.slice.call(arguments); + return new (OriginalDate.bind.apply(OriginalDate, __spread([void 0], args)))(); + } } - FakeDate.UTC = function () { - return OriginalDate.UTC(); - }; FakeDate.now = function () { var fakeAsyncTestZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec'); if (fakeAsyncTestZoneSpec) { @@ -3145,11 +3174,17 @@ Zone.__load_patch('asynctest', function (global, Zone, api) { } return OriginalDate.now.apply(this, arguments); }; - FakeDate.parse = function () { - return OriginalDate.parse(); - }; return FakeDate; }()); + FakeDate.UTC = OriginalDate.UTC; + FakeDate.parse = OriginalDate.parse; + // keep a reference for zone patched timer function + var timers = { + setTimeout: global.setTimeout, + setInterval: global.setInterval, + clearTimeout: global.clearTimeout, + clearInterval: global.clearInterval + }; var Scheduler = /** @class */ (function () { function Scheduler() { // Next scheduler id. @@ -3159,7 +3194,7 @@ Zone.__load_patch('asynctest', function (global, Zone, api) { // Current simulated time in millis. this._currentTime = 0; // Current real time in millis. - this._currentRealTime = Date.now(); + this._currentRealTime = OriginalDate.now(); } Scheduler.prototype.getCurrentTime = function () { return this._currentTime; @@ -3418,12 +3453,26 @@ Zone.__load_patch('asynctest', function (global, Zone, api) { } global['Date'] = FakeDate; FakeDate.prototype = OriginalDate.prototype; + // try check and reset timers + // because jasmine.clock().install() may + // have replaced the global timer + FakeAsyncTestZoneSpec.checkTimerPatch(); }; FakeAsyncTestZoneSpec.resetDate = function () { if (global['Date'] === FakeDate) { global['Date'] = OriginalDate; } }; + FakeAsyncTestZoneSpec.checkTimerPatch = function () { + if (global.setTimeout !== timers.setTimeout) { + global.setTimeout = timers.setTimeout; + global.clearTimeout = timers.clearTimeout; + } + if (global.setInterval !== timers.setInterval) { + global.setInterval = timers.setInterval; + global.clearInterval = timers.clearInterval; + } + }; FakeAsyncTestZoneSpec.prototype.lockDatePatch = function () { this.patchDateLocked = true; FakeAsyncTestZoneSpec.patchDate(); diff --git a/dist/zone-testing.js b/dist/zone-testing.js index 06e1596c4..aa2bbb2f2 100644 --- a/dist/zone-testing.js +++ b/dist/zone-testing.js @@ -440,42 +440,48 @@ Zone['SyncTestZoneSpec'] = SyncTestZoneSpec; return originalJasmineFn.apply(this, arguments); }; }); - if (enableClockPatch) { - var originalClockFn_1 = (jasmine[symbol('clock')] = jasmine['clock']); - jasmine['clock'] = function () { - var clock = originalClockFn_1.apply(this, arguments); - var originalTick = (clock[symbol('tick')] = clock.tick); + // need to patch jasmine.clock().mockDate and jasmine.clock().tick() so + // they can work properly in FakeAsyncTest + var originalClockFn = (jasmine[symbol('clock')] = jasmine['clock']); + jasmine['clock'] = function () { + var clock = originalClockFn.apply(this, arguments); + if (!clock[symbol('patched')]) { + clock[symbol('patched')] = symbol('patched'); + var originalTick_1 = (clock[symbol('tick')] = clock.tick); clock.tick = function () { var fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec'); if (fakeAsyncZoneSpec) { return fakeAsyncZoneSpec.tick.apply(fakeAsyncZoneSpec, arguments); } - return originalTick.apply(this, arguments); + return originalTick_1.apply(this, arguments); }; - var originalMockDate = (clock[symbol('mockDate')] = clock.mockDate); + var originalMockDate_1 = (clock[symbol('mockDate')] = clock.mockDate); clock.mockDate = function () { var fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec'); if (fakeAsyncZoneSpec) { - var dateTime = arguments[0]; + var dateTime = arguments.length > 0 ? arguments[0] : new Date(); return fakeAsyncZoneSpec.setCurrentRealTime.apply(fakeAsyncZoneSpec, dateTime && typeof dateTime.getTime === 'function' ? [dateTime.getTime()] : arguments); } - return originalMockDate.apply(this, arguments); + return originalMockDate_1.apply(this, arguments); }; - ['install', 'uninstall'].forEach(function (methodName) { - var originalClockFn = (clock[symbol(methodName)] = clock[methodName]); - clock[methodName] = function () { - var FakeAsyncTestZoneSpec = Zone['FakeAsyncTestZoneSpec']; - if (FakeAsyncTestZoneSpec) { - jasmine[symbol('clockInstalled')] = 'install' === methodName; - return; - } - return originalClockFn.apply(this, arguments); - }; - }); - return clock; - }; - } + // for auto go into fakeAsync feature, we need the flag to enable it + if (enableClockPatch) { + ['install', 'uninstall'].forEach(function (methodName) { + var originalClockFn = (clock[symbol(methodName)] = clock[methodName]); + clock[methodName] = function () { + var FakeAsyncTestZoneSpec = Zone['FakeAsyncTestZoneSpec']; + if (FakeAsyncTestZoneSpec) { + jasmine[symbol('clockInstalled')] = 'install' === methodName; + return; + } + return originalClockFn.apply(this, arguments); + }; + }); + } + } + return clock; + }; /** * Gets a function wrapping the body of a Jasmine `describe` block to execute in a * synchronous-only zone. @@ -843,17 +849,40 @@ Zone.__load_patch('asynctest', function (global, Zone, api) { * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ +var __read = (undefined && undefined.__read) || function (o, n) { + var m = typeof Symbol === "function" && o[Symbol.iterator]; + if (!m) return o; + var i = m.call(o), r, ar = [], e; + try { + while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); + } + catch (error) { e = { error: error }; } + finally { + try { + if (r && !r.done && (m = i["return"])) m.call(i); + } + finally { if (e) throw e.error; } + } + return ar; +}; +var __spread = (undefined && undefined.__spread) || function () { + for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); + return ar; +}; (function (global) { var OriginalDate = global.Date; var FakeDate = /** @class */ (function () { function FakeDate() { - var d = new OriginalDate(); - d.setTime(global.Date.now()); - return d; + if (arguments.length === 0) { + var d = new OriginalDate(); + d.setTime(FakeDate.now()); + return d; + } + else { + var args = Array.prototype.slice.call(arguments); + return new (OriginalDate.bind.apply(OriginalDate, __spread([void 0], args)))(); + } } - FakeDate.UTC = function () { - return OriginalDate.UTC(); - }; FakeDate.now = function () { var fakeAsyncTestZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec'); if (fakeAsyncTestZoneSpec) { @@ -861,11 +890,17 @@ Zone.__load_patch('asynctest', function (global, Zone, api) { } return OriginalDate.now.apply(this, arguments); }; - FakeDate.parse = function () { - return OriginalDate.parse(); - }; return FakeDate; }()); + FakeDate.UTC = OriginalDate.UTC; + FakeDate.parse = OriginalDate.parse; + // keep a reference for zone patched timer function + var timers = { + setTimeout: global.setTimeout, + setInterval: global.setInterval, + clearTimeout: global.clearTimeout, + clearInterval: global.clearInterval + }; var Scheduler = /** @class */ (function () { function Scheduler() { // Next scheduler id. @@ -875,7 +910,7 @@ Zone.__load_patch('asynctest', function (global, Zone, api) { // Current simulated time in millis. this._currentTime = 0; // Current real time in millis. - this._currentRealTime = Date.now(); + this._currentRealTime = OriginalDate.now(); } Scheduler.prototype.getCurrentTime = function () { return this._currentTime; @@ -1134,12 +1169,26 @@ Zone.__load_patch('asynctest', function (global, Zone, api) { } global['Date'] = FakeDate; FakeDate.prototype = OriginalDate.prototype; + // try check and reset timers + // because jasmine.clock().install() may + // have replaced the global timer + FakeAsyncTestZoneSpec.checkTimerPatch(); }; FakeAsyncTestZoneSpec.resetDate = function () { if (global['Date'] === FakeDate) { global['Date'] = OriginalDate; } }; + FakeAsyncTestZoneSpec.checkTimerPatch = function () { + if (global.setTimeout !== timers.setTimeout) { + global.setTimeout = timers.setTimeout; + global.clearTimeout = timers.clearTimeout; + } + if (global.setInterval !== timers.setInterval) { + global.setInterval = timers.setInterval; + global.clearInterval = timers.clearInterval; + } + }; FakeAsyncTestZoneSpec.prototype.lockDatePatch = function () { this.patchDateLocked = true; FakeAsyncTestZoneSpec.patchDate(); diff --git a/package.json b/package.json index ee5c972e9..b93ffc01b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "zone.js", - "version": "0.8.25", + "version": "0.8.26", "description": "Zones for JavaScript", "main": "dist/zone-node.js", "browser": "dist/zone.js",