diff --git a/lib/browser/browser.ts b/lib/browser/browser.ts index 06e66eeab..7a445b7a9 100644 --- a/lib/browser/browser.ts +++ b/lib/browser/browser.ts @@ -7,7 +7,7 @@ */ import {patchTimer} from '../common/timers'; -import {patchFuncToString} from '../common/to-string'; +import {patchFuncToString, patchObjectToString} from '../common/to-string'; import {findEventTask, patchClass, patchEventTargetMethods, patchMethod, patchPrototype, zoneSymbol} from '../common/utils'; import {propertyPatch} from './define-property'; @@ -154,6 +154,8 @@ if (_global['navigator'] && _global['navigator'].geolocation) { // patch Func.prototype.toString to let them look like native patchFuncToString(); +// patch Object.prototype.toString to let them look like native +patchObjectToString(); // handle unhandled promise rejection function findPromiseRejectionHandler(evtName: string) { diff --git a/lib/common/to-string.ts b/lib/common/to-string.ts index 2a4fec3ce..a58e610ce 100644 --- a/lib/common/to-string.ts +++ b/lib/common/to-string.ts @@ -34,3 +34,13 @@ export function patchFuncToString() { return originalFunctionToString.apply(this, arguments); }; } + +export function patchObjectToString() { + const originalObjectToString = Object.prototype.toString; + Object.prototype.toString = function() { + if (this instanceof Promise) { + return '[object Promise]'; + } + return originalObjectToString.apply(this, arguments); + }; +} \ No newline at end of file diff --git a/lib/node/node.ts b/lib/node/node.ts index 42ed632ee..24a78ecf6 100644 --- a/lib/node/node.ts +++ b/lib/node/node.ts @@ -11,7 +11,7 @@ import './events'; import './fs'; import {patchTimer} from '../common/timers'; -import {patchFuncToString} from '../common/to-string'; +import {patchFuncToString, patchObjectToString} from '../common/to-string'; import {findEventTask, patchMacroTask, patchMicroTask, zoneSymbol} from '../common/utils'; const set = 'set'; @@ -38,6 +38,8 @@ handleUnhandledPromiseRejection(); // patch Function.prototyp.toString patchFuncToString(); +// patch Object.prototyp.toString +patchObjectToString(); // Crypto let crypto: any; diff --git a/test/common/Promise.spec.ts b/test/common/Promise.spec.ts index d0254741c..6b6fc536c 100644 --- a/test/common/Promise.spec.ts +++ b/test/common/Promise.spec.ts @@ -60,6 +60,10 @@ describe( expect(String(Promise).indexOf('[native code]') >= 0).toBe(true); }); + it('should use native toString for promise instance', () => { + expect(Object.prototype.toString.call(Promise.resolve())).toEqual('[object Promise]'); + }); + it('should make sure that new Promise is instance of Promise', () => { expect(Promise.resolve(123) instanceof Promise).toBe(true); expect(new Promise(() => null) instanceof Promise).toBe(true);