Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
feat: assert that right ZoneAwarePromise is available (#420)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhevery committed Sep 1, 2016
1 parent f57fc18 commit 4c35e5b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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; };
Expand Down
21 changes: 21 additions & 0 deletions test/common/zone.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 4c35e5b

Please sign in to comment.