异步测试由于超时而失败

jvb*_*008 5 jestjs angular12

我们在 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();
 }); 
Run Code Online (Sandbox Code Playgroud)

运行测试花费太多时间并且任务失败。

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

  • 我们已经使用了 jest.setTimeout(newTimeout)
  • 我们还使用 --runInBand 串行运行测试