mardi 11 juin 2019

angular 7 testing error- rxjs asyncObject Error: Timeout - Async callback was not invoked within 5000ms

I got the following error:

   Error: Timeout - Async callback was not invoked within 5000ms (set by jasmine.DEFAULT_TIMEOUT_INTERVAL)
            at <Jasmine>

If I don't reinitialize the resourceSubject$ in before each both test together won't work(I mean by test will not cover all subscription code), when I try to make a new AsyncSubject beforeEach, I got the above error

my service subscribe to an event that I need to test

this is my test code:

let resourceSubject$: AsyncSubject<Setting>;
beforeEach(async () => {
  resourceLoaderMock.funToMock.and.returnValue({
      observableObj: resourceSubject$.asObservable() ,
      loadData(){}
    });
  ......
.....
beforeEach(() => {
  subject$ = new AsyncSubject<any>();
  resourceSubject$ = new AsyncSubject<Data>(); // this make the issue , if i removed it i could not run both test cases
});


fit('should get data from server', async () => {
  // given

  // when
  service.loadData();
  resourceSubject$.next(deafultValues)
  resourceSubject$.complete(); 

  // then
  const res: Setting = await new Promise((resolve, reject) => {
    service.subscribe$.subscribe(value => {
      resolve(value);
    });
  });

  expect(deafultValues).toEqual(res);
});

my service func I need to test:

this.resource.observableObj.subscribe(
  result => {
    // Handle result
    this.subscriber$.next(result);
    this.subscriber$.complete();
  },
  error => {
    this.subscriber$.next(defaultData);
    this.subscriber$.complete();
  }
);

Aucun commentaire:

Enregistrer un commentaire