From 4c35e5bde514389dc9d9bc141464197151382ce3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=A1ko=20Hevery?= Date: Wed, 31 Aug 2016 21:10:07 -0700 Subject: [PATCH] feat: assert that right ZoneAwarePromise is available (#420) --- lib/zone.ts | 15 +++++++++++++++ test/common/zone.spec.ts | 21 +++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/lib/zone.ts b/lib/zone.ts index 03eb759e2..6ed37ee21 100644 --- a/lib/zone.ts +++ b/lib/zone.ts @@ -232,6 +232,11 @@ interface ZoneType { * @returns {Task} The task associated with the current execution. */ currentTask: Task; + + /** + * Verify that Zone has been correctly patched. Specifically that Promise is zone aware. + */ + assertZonePatched(); } /** @@ -508,6 +513,16 @@ const Zone: ZoneType = (function(global: any) { class Zone implements AmbientZone { static __symbol__: (name: string) => string = __symbol__; + static assertZonePatched() { + if (global.Promise !== ZoneAwarePromise) { + throw new Error("Zone.js has detected that ZoneAwarePromise `(window|global).Promise` " + + "has been overwritten.\n" + + "Most likely cause is that a Promise polyfill has been loaded " + + "after Zone.js (Polyfilling Promise api is not necessary when zone.js is loaded. " + + "If you must load one, do so before loading zone.js.)"); + } + } + static get current(): AmbientZone { return _currentZone; }; static get currentTask(): Task { return _currentTask; }; diff --git a/test/common/zone.spec.ts b/test/common/zone.spec.ts index 68d068175..bd9799057 100644 --- a/test/common/zone.spec.ts +++ b/test/common/zone.spec.ts @@ -149,6 +149,27 @@ describe('Zone', function () { { microTask: false, macroTask: false, eventTask: false, change: 'microTask', zone: 'parent' }, ]); }); + + describe('assert ZoneAwarePromise', () => { + it('should not throw when all is OK', () => { + Zone.assertZonePatched(); + }); + + it('should throw when Promise has been patched', () => { + class WrongPromise{} + + var ZoneAwarePromise = global.Promise; + global.Promise = WrongPromise; + try { + expect(ZoneAwarePromise).toBeTruthy(); + expect(() => Zone.assertZonePatched()).toThrow(); + } finally { + // restore it. + global.Promise = ZoneAwarePromise; + } + Zone.assertZonePatched(); + }); + }); }); describe('invoking tasks', () => {