New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error: Cannot make XHRs from within a fake async test. #8280
Comments
We are also running into the same issue. Is there a solution for this. All our unittests are failing because of this issue. I assume the XHR is used to load the styleUrl. Is there a workaround for this. Due to this issue, we are not able to close any story because of failure to meet the acceptance criteria. Let us know how to proceed. |
To make some of the tests passing, I had to remove the fakeAsync and skip tests that uses tick (as tick works only in the fakeAsyncZone). What are our options? Is this going to be fixed soon? |
Same here. If I use async to inject the component karma fails. If I use fakeAsync I get this exception. |
This issue is also preventing us from unit testing any component that makes uses of |
+1 here... |
+1 |
I guess I know where it comes from! We should find a way to override all component templates. |
@yjaaidi what Angular2 version? |
Hi @zoechi, It's rc1. Didn't try with rc2 yet. |
This is working as intended - we can't "fake" async behavior in any reasonable way if a real XHR is made, so the test must throw an error. Something that we can do, however, is run tests against a cached version of the templates and styles, so we don't need to make the XHR in the first place. @vikerman worked on a system to do this, see #7940 There is little documentation though - we should add an example of a test setup using this. @vikerman does one exist that I don't know about, or should I create one? |
Should that be in a cookbook on angular.io ? If you think this makes sense could you please create an issue with the detailson the angular.io repo. Thanks. |
Below is a spec example for a component using However, the
|
@centaure Here's how I hacked my way through this:
As you can see, I make a GET request for the contents of the files I want and just save them in the |
Hm, same problem here...
Where page.form.submit() simply calls register() and users.register() is hitting a local unit test database...
|
Hello @mattxo , Oh! That's normal. As your app is making an http request, You should mock the http backend. It's not as fun as AngularJS 1.x beforeEach((done) => {
TestBed
.configureTestingModule({
imports: [
UserModule
],
providers: [
MockBackend,
{
provide: Http,
deps: [MockBackend, RequestOptions],
useFactory: (mockBackend, requestOptions) => {
return new Http(mockBackend, requestOptions);
}
}
]
})
.compileComponents()
.then(done);
});
beforeEach(inject([MockBackend], (mockBackend: MockBackend) => {
this.spyConnection = jasmine.createSpy('connection');
mockBackend.connections.subscribe((connection: MockConnection) => {
connection.mockRespond(new Response(new ResponseOptions(this.spyConnection({
body: connection.request.text(),
method: connection.request.method,
url: connection.request.url
}))));
});
}));
it(..., () => {
this.spyConnection.and.returnValue({
body: { /* YOUR EXPECTED RESPONSE HERE */ },
status: 200
});
expect(this.spyConnection).toHaveBeenCalledTimes(1);
expect(this.spyConnection.calls.argsFor(0)[0].method).toEqual(RequestMethod.Get);
expect(this.spyConnection.calls.argsFor(0)[0].url).toEqual('/api/v1/endpoint.../');
}); |
Hey @yjaaidi, Thanks for the sample code :) I'd prefer not mock the backend in this case though, if I can help it. It's an e2e/integration test, and performance isn't an issue with a local database. The fakeAsync in this case is only there to make this work
What I'd really like, if it's not possible to have the XHR run synchronously when called under fakeAsync, is to have the register() promise returned from form.submit() or possibly better still, from page.submitButton.click(), but that doesn't seem possible either... |
We had the same error. For us the key thing was setup providers correctly in TestBed.configureTestingModule.
Then in the test:
|
Example should be covered as part of resolving #12409 |
I have the same error. Can I have any help here? Thanks in advance. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Steps to reproduce and a minimal demo of the problem
What steps should we try in your demo to see the problem?
The text was updated successfully, but these errors were encountered: