question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HttpTestingController does not resolve async/await

See original GitHub issue

I’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

  1. 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;
  }
}
  1. try to use the HttpTestingClient to catch the http://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:open
  • Created 5 years ago
  • Reactions:25
  • Comments:11

github_iconTop GitHub Comments

19reactions
joshrizzocommented, Dec 14, 2018

It works, you just have to make the call and then expect the call, and then await it:

    it('should return true and set the token', async () => {
      // Arrange
      const call = service.login('Test2', 'Password2', 'Local');
      const testToken = 'testToken';
      const loginRequest = httpMock.expectOne(environment.apiUrl + 'auth/login');
      loginRequest.flush(testToken);

      // Act
      const result = await call;

      // Assert
      httpMock.verify();
      expect(result).toBeTruthy();
      expect(service.authToken).toBe(testToken);
    });
5reactions
picolinocommented, Dec 10, 2019

Acceptable solution, but I would like to have a simplier solution, like this:

httpMock.addResponse(environment.apiUrl + 'auth/login', 'testToken');

const result = await service.login('Login', 'Password', 'Local');

expect(service.authToken).toBe(testToken);

The point is to define answers ahead the requests would be sent. I think that functionality must be out-of-the-box.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found