HttpTestingController does not resolve async/await
See original GitHub issueI’m submitting a…
[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report
[ ] Performance issue
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
[ ] Other... Please describe:
Current behavior
When using async await with promises (and therefore: this.http.get(...).toPromise()
), one can unittest everything like normal with the HttpTestingController
. BUT if you have a service method that has multiple await
in it, the testing controller is not aware of the later executed await
and will just wait and fail.
Expected behavior
All await calls inside a method should be executed during async testing.
Minimal reproduction of the problem with instructions
- use a service with async await function like:
@Injectable()
class Service {
constructor(private http: HttpClient){}
public async foobar(): Promise<number> {
await Promise.resolve();
await Promise.resolve();
await this.http.get('http://asdf').toPromise();
return 1;
}
}
- try to use the
HttpTestingClient
to catch thehttp://asdf
request.
Environment
Angular version: 6.1.0
Browser:
- [x ] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
Issue Analytics
- State:
- Created 5 years ago
- Reactions:25
- Comments:11
Top Results From Across the Web
How to test promises with HttpTestingController?
I'm getting in a chicken-and-egg situation. The getCustomer method will not return until a mocked request is provided, and I cannot use httpMock ......
Read more >Unit testing — Best practices testing Angular Applications
So that will really helps you in your work. And you could spend more time learning about the problem than debugging.
Read more >HttpTestingController - Angular
If any requests are outstanding, fail with an error message indicating which requests were not handled. If ignoreCancelled is not set (the default),...
Read more >Testing promise rejection in JavaScript with Jest - Codeleak.pl
with async/await it("resolves (1)", async () => { await ... In case the Promise rejects and the test did not expect that, Jest...
Read more >[Solved]-Async await hitting API so many times-angular.js
Firstly, try to remove the : Promise<any> from companydata() function as it is not returning any promise, also try removing the .pipe(map((response) ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It works, you just have to make the call and then expect the call, and then await it:
Acceptable solution, but I would like to have a simplier solution, like this:
The point is to define answers ahead the requests would be sent. I think that functionality must be out-of-the-box.