首页 > 解决方案 > 异步测试因超时而失败

问题描述

我们在 Angular 12 中的项目中使用 jest 27 来运行我们的单元测试。在本地,这些测试运行正常,但在它们中断的云代理管道中运行它们,出现以下错误:

抛出:“挂钩超时超过 5000 毫秒。如果这是一个长时间运行的测试,请使用 jest.setTimeout(newTimeout) 增加超时值。”

这是代码中断的部分:

const createHost = createHostFactory({
component: Component,    
imports: [
  TranslateModule.forRoot(),
  GridModule,
  MaterialWrapperTestingModule,
  ReactiveFormsModule,
  TooltipModule,
  TableEditionToolbarModule,
],
providers: [
  { provide: ContactsService, useValue: contactsServiceSpy },
  { provide: CustomerService, useValue: customerServiceSpy },
  { provide: CustomerDetailFacade, useValue: customerFacadeSpy },
  { provide: HeaderMenuFacade, useValue: headerMenuFacadeSpy },
  { provide: ContextFacade, useValue: contextFacadeSpy },
  { provide: MatDialog, useValue: dialogMock },
],});

 beforeEach(() => {
  host = createHost('<component-selector></component-selector>', { detectChanges: true, 
  props: { structure } });
  return host.fixture.whenStable();
 }); 

运行测试需要太多时间并且任务失败。

为了解决这个问题,我们做了一些更改,但没有任何帮助:

标签: jestjsangular12

解决方案


推荐阅读