From 8bcccb8b0fe0ab2d08f520f2f50945ab4446ec95 Mon Sep 17 00:00:00 2001 From: Vikram Subramanian Date: Tue, 5 Sep 2017 16:54:16 -0700 Subject: [PATCH] feat(spec): log URL in error when attempting XHR from FakeAsyncTestZone Save the URL from xhr.open and log it when throwing an error from FakeAsyncTestZone when the XHR send it attempted. This would make it easier to debug such errors from a fakeAsync test. --- lib/browser/browser.ts | 13 +++++++-- lib/zone-spec/fake-async-test.ts | 4 ++- test/zone-spec/fake-async-test.spec.ts | 39 +++++++++++++------------- 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/lib/browser/browser.ts b/lib/browser/browser.ts index e796322b1..8a8615ab5 100644 --- a/lib/browser/browser.ts +++ b/lib/browser/browser.ts @@ -82,9 +82,11 @@ Zone.__load_patch('XHR', (global: any, Zone: ZoneType, api: _ZonePrivate) => { const XHR_SYNC = zoneSymbol('xhrSync'); const XHR_LISTENER = zoneSymbol('xhrListener'); const XHR_SCHEDULED = zoneSymbol('xhrScheduled'); + const XHR_URL = zoneSymbol('xhrURL'); interface XHROptions extends TaskData { target: any; + url: string; args: any[]; aborted: boolean; } @@ -158,6 +160,7 @@ Zone.__load_patch('XHR', (global: any, Zone: ZoneType, api: _ZonePrivate) => { const openNative: Function = patchMethod( window.XMLHttpRequest.prototype, 'open', () => function(self: any, args: any[]) { self[XHR_SYNC] = args[2] == false; + self[XHR_URL] = args[1]; return openNative.apply(self, args); }); @@ -169,8 +172,14 @@ Zone.__load_patch('XHR', (global: any, Zone: ZoneType, api: _ZonePrivate) => { // if the XHR is sync there is no task to schedule, just execute the code. return sendNative.apply(self, args); } else { - const options: XHROptions = - {target: self, isPeriodic: false, delay: null, args: args, aborted: false}; + const options: XHROptions = { + target: self, + url: self[XHR_URL], + isPeriodic: false, + delay: null, + args: args, + aborted: false + }; return zone.scheduleMacroTask( XMLHTTPREQUEST_SOURCE, placeholderCallback, options, scheduleTask, clearTask); } diff --git a/lib/zone-spec/fake-async-test.ts b/lib/zone-spec/fake-async-test.ts index 0217570a3..a22134002 100644 --- a/lib/zone-spec/fake-async-test.ts +++ b/lib/zone-spec/fake-async-test.ts @@ -339,7 +339,9 @@ this._setInterval(task.invoke, task.data['delay'], (task.data as any)['args']); break; case 'XMLHttpRequest.send': - throw new Error('Cannot make XHRs from within a fake async test.'); + throw new Error( + 'Cannot make XHRs from within a fake async test. Request URL: ' + + (task.data as any)['url']); case 'requestAnimationFrame': case 'webkitRequestAnimationFrame': case 'mozRequestAnimationFrame': diff --git a/test/zone-spec/fake-async-test.spec.ts b/test/zone-spec/fake-async-test.spec.ts index 871c47855..b919293fe 100644 --- a/test/zone-spec/fake-async-test.spec.ts +++ b/test/zone-spec/fake-async-test.spec.ts @@ -664,25 +664,26 @@ describe('FakeAsyncTestZoneSpec', () => { }); }); - describe('XHRs', ifEnvSupports('XMLHttpRequest', () => { - it('should throw an exception if an XHR is initiated in the zone', () => { - expect(() => { - fakeAsyncTestZone.run(() => { - let finished = false; - let req = new XMLHttpRequest(); - - req.onreadystatechange = () => { - if (req.readyState === XMLHttpRequest.DONE) { - finished = true; - } - }; - - req.open('GET', '/', true); - req.send(); - }); - }).toThrowError('Cannot make XHRs from within a fake async test.'); - }); - })); + describe( + 'XHRs', ifEnvSupports('XMLHttpRequest', () => { + it('should throw an exception if an XHR is initiated in the zone', () => { + expect(() => { + fakeAsyncTestZone.run(() => { + let finished = false; + let req = new XMLHttpRequest(); + + req.onreadystatechange = () => { + if (req.readyState === XMLHttpRequest.DONE) { + finished = true; + } + }; + + req.open('GET', '/test', true); + req.send(); + }); + }).toThrowError('Cannot make XHRs from within a fake async test. Request URL: /test'); + }); + })); describe('node process', ifEnvSupports(supportNode, () => { it('should be able to schedule microTask with additional arguments', () => {