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

feat(spec): log URL in error when attempting XHR from FakeAsyncTestZone #893

Merged
merged 1 commit into from
Sep 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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']);
Copy link
Collaborator

@JiaLiPassion JiaLiPassion Sep 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vikerman , if we get the url from task.data, maybe we can get the url by directly calling 'task.data.target.responseURL? So we don't need to save an additional urlinxhrandtask.data`

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my testing responseURL is an empty string before the actual request is sent.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vikerman , yes, you are right!

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