0

I Work on an application using ionic and angular, and I faced this problem with unit test : Timeout - Async callback was not invoked within 5000ms (set by jasmine.DEFAULT_TIMEOUT_INTERVAL)

auth.service.ts

login(email: string, password: string) {
return this.http
  .post<AuthResponseData>(
    `https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=${
      environment.firebaseAPIKey
    }`,
    // tslint:disable-next-line: object-literal-shorthand
    { email: email, password: password }
  ).pipe(tap(this.setUserData.bind(this))); }

auth.service.spec.ts

import { TestBed } from '@angular/core/testing';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { AuthService } from './auth.service';

let server: AuthService;

describe('AuthService', () => {
beforeEach(() => TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [AuthService]
}));
beforeEach(() =>  server = TestBed.get(AuthService));

it('fail ', done => {
const e = 'INVALID_PASSWORD';
server.login('s@S.com', '987456').subscribe(
    good =>  {
    },
    errRes => {
       expect(errRes.error.error.message).toEqual(e);
       done();
    },
   );
   }, );
   });
R. Richards
  • 24,603
  • 10
  • 64
  • 64
  • This doesn't work because the code never traverses the error callback. You need to use the `HttpTestingController` to send mock data or error response to the `subscribe`. Check the following links out: https://medium.com/better-programming/testing-http-requests-in-angular-with-httpclienttestingmodule-3880ceac74cf And how to simulate an error: https://stackoverflow.com/questions/46028804/how-to-mock-angular-4-3-httpclient-an-error-response-in-testing – AliF50 Apr 17 '20 at 11:48
  • @AliF50 Thanks for your help, but I couldn't apply it to my code well, could you help me? – shaima saad Apr 17 '20 at 22:05

0 Answers0