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

Commit

Permalink
feat(spec): log URL in error when attempting XHR from FakeAsyncTestZo…
Browse files Browse the repository at this point in the history
…ne (#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.
  • Loading branch information
vikerman authored and mhevery committed Sep 6, 2017
1 parent b9c0d9c commit 874bfdc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
13 changes: 11 additions & 2 deletions lib/browser/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
});

Expand All @@ -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);
}
Expand Down
4 changes: 3 additions & 1 deletion lib/zone-spec/fake-async-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
39 changes: 20 additions & 19 deletions test/zone-spec/fake-async-test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 874bfdc

Please sign in to comment.