异步函数没有在 5000 毫秒内完成

问题描述 投票:0回答:1

我正在尝试测试 gridApi 是否已正确初始化,但出现错误:异步功能未在 5000 毫秒内完成。 有时代码运行良好,但有时我必须运行代码 100 次才能通过

let component: SGridComponent;
let fixture: ComponentFixture<SGridComponent>;
let data$: Observable<Criteria[]>=new BehaviorSubject(
[{
id: "some",
lob: "some"
}])

const columnDefs = [
 { field: 'id' },
 { field: 'priorityOrder' }
]

beforeEach(async () => {
 await TestBed.configureTestingModule({
 imports: [AgGridModule, matIconModule, ngSelectModule],
 declarations:[SGridComponent]
 }).compileComponents();
 fixture = TestBed.createComponent(SGridComponent);
 component = fixture.debugElement.componentInstance;
 fixture.detectChanges();
});

it('should test if ag-grid exists in HTML', () => {
component.columnDefs = columnDefs;
component.data=data$;
const grid = fixture.nativeElement.querySelector('ag-grid-angular');
expec(grid).not.toBeNull();
}

这是我的 ts 组件:

export class UicGridComponent {
 gridApi: any;
 gridColumnApi: any;

 @Input
 columnDefs: any;

 @Input
 data: any;
 
 onGridReady(params: any){
  this.gridApi = params.api;
  this.gridColumnApi = params.columnApi;
  this.data.subscribe((data:any) => {
   params.api.setRowData(data);
  };
 };
}
karma-jasmine angular15
1个回答
0
投票

尝试查看数据$是否完成

import { BehaviorSubject, Observable } from 'rxjs';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { AgGridModule } from 'ag-grid-angular';
import { matIconModule } from '@angular/material/icon';
import { ngSelectModule } from '@ng-select/ng-select';
import { SGridComponent } from './s-grid.component';

let component: SGridComponent;
let fixture: ComponentFixture<SGridComponent>;
let data$: Observable<Criteria[]> = new BehaviorSubject<Criteria[]>([
  {
    id: 'some',
    lob: 'some'
  }
]).pipe(
  // Complete the observable after emitting its initial value
  finalize(() => console.log('data$ completed'))
);

const columnDefs = [
  { field: 'id' },
  { field: 'priorityOrder' }
];

beforeEach(async () => {
  await TestBed.configureTestingModule({
    imports: [AgGridModule, matIconModule, ngSelectModule],
    declarations: [SGridComponent]
  }).compileComponents();
  fixture = TestBed.createComponent(SGridComponent);
  component = fixture.debugElement.componentInstance;
  fixture.detectChanges();
});

it('should test if ag-grid exists in HTML', () => {
  component.columnDefs = columnDefs;
  component.data = data$;
  const grid = fixture.nativeElement.querySelector('ag-grid-angular');
  expect(grid).not.toBeNull();
});

Jasmine 将在 5 秒后超时,也许它太忙了并且您有竞争条件

© www.soinside.com 2019 - 2024. All rights reserved.