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

Commit 874bfdc

Browse files
vikermanmhevery
authored andcommitted
feat(spec): log URL in error when attempting XHR from FakeAsyncTestZone (#893)
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.
1 parent b9c0d9c commit 874bfdc

File tree

3 files changed

+34
-22
lines changed

3 files changed

+34
-22
lines changed

lib/browser/browser.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,11 @@ Zone.__load_patch('XHR', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
8282
const XHR_SYNC = zoneSymbol('xhrSync');
8383
const XHR_LISTENER = zoneSymbol('xhrListener');
8484
const XHR_SCHEDULED = zoneSymbol('xhrScheduled');
85+
const XHR_URL = zoneSymbol('xhrURL');
8586

8687
interface XHROptions extends TaskData {
8788
target: any;
89+
url: string;
8890
args: any[];
8991
aborted: boolean;
9092
}
@@ -158,6 +160,7 @@ Zone.__load_patch('XHR', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
158160
const openNative: Function = patchMethod(
159161
window.XMLHttpRequest.prototype, 'open', () => function(self: any, args: any[]) {
160162
self[XHR_SYNC] = args[2] == false;
163+
self[XHR_URL] = args[1];
161164
return openNative.apply(self, args);
162165
});
163166

@@ -169,8 +172,14 @@ Zone.__load_patch('XHR', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
169172
// if the XHR is sync there is no task to schedule, just execute the code.
170173
return sendNative.apply(self, args);
171174
} else {
172-
const options: XHROptions =
173-
{target: self, isPeriodic: false, delay: null, args: args, aborted: false};
175+
const options: XHROptions = {
176+
target: self,
177+
url: self[XHR_URL],
178+
isPeriodic: false,
179+
delay: null,
180+
args: args,
181+
aborted: false
182+
};
174183
return zone.scheduleMacroTask(
175184
XMLHTTPREQUEST_SOURCE, placeholderCallback, options, scheduleTask, clearTask);
176185
}

lib/zone-spec/fake-async-test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,9 @@
339339
this._setInterval(task.invoke, task.data['delay'], (task.data as any)['args']);
340340
break;
341341
case 'XMLHttpRequest.send':
342-
throw new Error('Cannot make XHRs from within a fake async test.');
342+
throw new Error(
343+
'Cannot make XHRs from within a fake async test. Request URL: ' +
344+
(task.data as any)['url']);
343345
case 'requestAnimationFrame':
344346
case 'webkitRequestAnimationFrame':
345347
case 'mozRequestAnimationFrame':

test/zone-spec/fake-async-test.spec.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -664,25 +664,26 @@ describe('FakeAsyncTestZoneSpec', () => {
664664
});
665665
});
666666

667-
describe('XHRs', ifEnvSupports('XMLHttpRequest', () => {
668-
it('should throw an exception if an XHR is initiated in the zone', () => {
669-
expect(() => {
670-
fakeAsyncTestZone.run(() => {
671-
let finished = false;
672-
let req = new XMLHttpRequest();
673-
674-
req.onreadystatechange = () => {
675-
if (req.readyState === XMLHttpRequest.DONE) {
676-
finished = true;
677-
}
678-
};
679-
680-
req.open('GET', '/', true);
681-
req.send();
682-
});
683-
}).toThrowError('Cannot make XHRs from within a fake async test.');
684-
});
685-
}));
667+
describe(
668+
'XHRs', ifEnvSupports('XMLHttpRequest', () => {
669+
it('should throw an exception if an XHR is initiated in the zone', () => {
670+
expect(() => {
671+
fakeAsyncTestZone.run(() => {
672+
let finished = false;
673+
let req = new XMLHttpRequest();
674+
675+
req.onreadystatechange = () => {
676+
if (req.readyState === XMLHttpRequest.DONE) {
677+
finished = true;
678+
}
679+
};
680+
681+
req.open('GET', '/test', true);
682+
req.send();
683+
});
684+
}).toThrowError('Cannot make XHRs from within a fake async test. Request URL: /test');
685+
});
686+
}));
686687

687688
describe('node process', ifEnvSupports(supportNode, () => {
688689
it('should be able to schedule microTask with additional arguments', () => {

0 commit comments

Comments
 (0)