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

Commit 4c35e5b

Browse files
committed
feat: assert that right ZoneAwarePromise is available (#420)
1 parent f57fc18 commit 4c35e5b

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

Diff for: lib/zone.ts

+15
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,11 @@ interface ZoneType {
232232
* @returns {Task} The task associated with the current execution.
233233
*/
234234
currentTask: Task;
235+
236+
/**
237+
* Verify that Zone has been correctly patched. Specifically that Promise is zone aware.
238+
*/
239+
assertZonePatched();
235240
}
236241

237242
/**
@@ -508,6 +513,16 @@ const Zone: ZoneType = (function(global: any) {
508513
class Zone implements AmbientZone {
509514
static __symbol__: (name: string) => string = __symbol__;
510515

516+
static assertZonePatched() {
517+
if (global.Promise !== ZoneAwarePromise) {
518+
throw new Error("Zone.js has detected that ZoneAwarePromise `(window|global).Promise` " +
519+
"has been overwritten.\n" +
520+
"Most likely cause is that a Promise polyfill has been loaded " +
521+
"after Zone.js (Polyfilling Promise api is not necessary when zone.js is loaded. " +
522+
"If you must load one, do so before loading zone.js.)");
523+
}
524+
}
525+
511526

512527
static get current(): AmbientZone { return _currentZone; };
513528
static get currentTask(): Task { return _currentTask; };

Diff for: test/common/zone.spec.ts

+21
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,27 @@ describe('Zone', function () {
149149
{ microTask: false, macroTask: false, eventTask: false, change: 'microTask', zone: 'parent' },
150150
]);
151151
});
152+
153+
describe('assert ZoneAwarePromise', () => {
154+
it('should not throw when all is OK', () => {
155+
Zone.assertZonePatched();
156+
});
157+
158+
it('should throw when Promise has been patched', () => {
159+
class WrongPromise{}
160+
161+
var ZoneAwarePromise = global.Promise;
162+
global.Promise = WrongPromise;
163+
try {
164+
expect(ZoneAwarePromise).toBeTruthy();
165+
expect(() => Zone.assertZonePatched()).toThrow();
166+
} finally {
167+
// restore it.
168+
global.Promise = ZoneAwarePromise;
169+
}
170+
Zone.assertZonePatched();
171+
});
172+
});
152173
});
153174

154175
describe('invoking tasks', () => {

0 commit comments

Comments
 (0)