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

feat: assert that right ZoneAwarePromise is available #420

Merged
merged 1 commit into from
Sep 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 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,15 @@ 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 (If you must load polyfills do so before Zone.js.)");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"(Polyfilling promise api is not necessary when zone.js is loaded. If you must load one, do so before loading zone.js.)"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

}
}


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