Angular 17 fakeAsync tasks not running until test is cleaned up with ‘resetFakeAsyncZone()’

Overview

I am in the progress of migrating my Angular project from v14 to v17, and also migrating my Jest from v27 to v29 at the same time. I have my project running, with no console errors / warnings, but I am struggling to get Jest to work.

I am running my tests using the fakeAsync wrapper, but am getting an issue where some of the async tasks within aren’t ran until after any of my expect(...) checks fail and Angular’s resetFakeAsyncZone() runs, despite having all the necessary flush / tick calls (this test passed before and breakpoints can show that it is still getting the same data but just not until after the expect call)

One of my failing tests
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>it('should call Update on submit if shouldUpdate is true', fakeAsync(() => {
// arrange
comp.shouldUpdate = true;
comp.CanAlter = true;
jest.spyOn(connectionResource, 'Update').mockImplementation((c) => {
return Promise.resolve({ ...c, Id: connection.Id, Children: [] } as ApiConnection);
});
let onSubSpy = jest.spyOn(comp.onSubmit, 'emit');
// act
comp.ngOnChanges();
flush();
fixture.detectChanges();
let result = { ...comp.form.getRawValue(), ParentId: probe.Id };
let submitButton = fixture.debugElement.query(By.css('button[type="submit"]'));
submitButton.nativeElement.click();
flush();
// assert
expect(connectionResource.Update).toHaveBeenCalledWith(result, connection.Id);
let cached = coreCache.SearchMonitorableTree(coreCache.PayloadCache.Monitorables[0], 'Id', connection.Id)[0];
expect(onSubSpy).toHaveBeenCalledWith(cached); // This expect fails, and the the spied function is hit directly after
}));
</code>
<code>it('should call Update on submit if shouldUpdate is true', fakeAsync(() => { // arrange comp.shouldUpdate = true; comp.CanAlter = true; jest.spyOn(connectionResource, 'Update').mockImplementation((c) => { return Promise.resolve({ ...c, Id: connection.Id, Children: [] } as ApiConnection); }); let onSubSpy = jest.spyOn(comp.onSubmit, 'emit'); // act comp.ngOnChanges(); flush(); fixture.detectChanges(); let result = { ...comp.form.getRawValue(), ParentId: probe.Id }; let submitButton = fixture.debugElement.query(By.css('button[type="submit"]')); submitButton.nativeElement.click(); flush(); // assert expect(connectionResource.Update).toHaveBeenCalledWith(result, connection.Id); let cached = coreCache.SearchMonitorableTree(coreCache.PayloadCache.Monitorables[0], 'Id', connection.Id)[0]; expect(onSubSpy).toHaveBeenCalledWith(cached); // This expect fails, and the the spied function is hit directly after })); </code>
it('should call Update on submit if shouldUpdate is true', fakeAsync(() => {
  // arrange
  comp.shouldUpdate = true;
  comp.CanAlter = true;
  jest.spyOn(connectionResource, 'Update').mockImplementation((c) => {
     return Promise.resolve({ ...c, Id: connection.Id, Children: [] } as ApiConnection);
  });
  let onSubSpy = jest.spyOn(comp.onSubmit, 'emit');

  // act
  comp.ngOnChanges();
  flush();
  fixture.detectChanges();

  let result = { ...comp.form.getRawValue(), ParentId: probe.Id };
  let submitButton = fixture.debugElement.query(By.css('button[type="submit"]'));
  submitButton.nativeElement.click();
  flush();

  // assert
  expect(connectionResource.Update).toHaveBeenCalledWith(result, connection.Id);
  let cached = coreCache.SearchMonitorableTree(coreCache.PayloadCache.Monitorables[0], 'Id', connection.Id)[0];
  expect(onSubSpy).toHaveBeenCalledWith(cached); // This expect fails, and the the spied function is hit directly after
}));
What I have tried so far
  • I have tried changing the expect to toHaveBeenCalled() to double check that there wasn’t some small change to the model somewhere, it insisted that it wasn’t called, then called it right after (same problem)

  • I tried using jest.useFakeTimers() (there were many changes to fakeTimers in the last couple Jest updates), this did not fix.

  • I tried adding tick() and flushMicrotasks() to see if those would have any affect, they did not.

  • I tried calling resetFakeAsyncZone() to see if that had any affect as that is what seems to cause it to work at the end, that did not work.

Helpful Resources
  • Here is a gif of what the bug looks like in action: enter image description here

  • Here is the Angular Update Guide with breaking changes (Didn’t see anything with fakeAsync): https://update.angular.io/?l=3&v=14.0-17.0

  • Here is the Jest changelog with all the breaking changes: https://github.com/jestjs/jest/blob/main/CHANGELOG.md

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật