diff --git a/packages/zone.js/lib/common/to-string.ts b/packages/zone.js/lib/common/to-string.ts index 3ae8d0e99d..7722623c7a 100644 --- a/packages/zone.js/lib/common/to-string.ts +++ b/packages/zone.js/lib/common/to-string.ts @@ -49,9 +49,10 @@ Zone.__load_patch('toString', (global: any) => { const originalObjectToString = Object.prototype.toString; const PROMISE_OBJECT_TO_STRING = '[object Promise]'; Object.prototype.toString = function() { - if (this instanceof Promise) { + if (typeof Promise === 'function' && this instanceof Promise) { return PROMISE_OBJECT_TO_STRING; } + return originalObjectToString.call(this); }; }); diff --git a/packages/zone.js/test/common/toString.spec.ts b/packages/zone.js/test/common/toString.spec.ts index d959df23f1..d1f0320726 100644 --- a/packages/zone.js/test/common/toString.spec.ts +++ b/packages/zone.js/test/common/toString.spec.ts @@ -18,6 +18,18 @@ describe('global function patch', () => { .toEqual(Function.prototype.toString.call(g[zoneSymbol('setTimeout')])); }); + it('should not throw error if Promise is not a function', () => { + const P = g.Promise; + try { + g.Promise = undefined; + expect(() => { + const a = {}.toString(); + }).not.toThrow(); + } finally { + g.Promise = P; + } + }); + it('MutationObserver toString should be the same with native version', ifEnvSupports('MutationObserver', () => { const nativeMutationObserver = g[zoneSymbol('MutationObserver')];