Getting console error: 'expect' was used when there was no current spec, this could be because an asynchronous test timed out?

Getting console error: 'expect' was used when there was no current spec, this could be because an asynchronous test timed out?

这是我在 service.ts 文件中的代码

  private isMessageShown = new BehaviorSubject({ visibility: false});

  showMessage() {
    const visibilityDetails = {
      show: true,
    };
   this.isMessageShown.next(visibilityDetails);
   setTimeout(this.hideMessage.bind(this), 3000);
 }
 
 hideMessage() {
   this.isMessageShown.next({ visibility: false});
 }

 get messageVisibilityInfo() {
   return this.isMessageShown.asObservable();
 }

我的 service.spec.ts 与 showMessage 断言相关的代码

  it('should get message status', done => {
    const result = service.messageVisibilityInfo;
    service.showMessage();
    result.subscribe(res => {
      expect(res.visibility).toBeTruthy();
      done();
    });
  });

测试用例按预期工作正常,但我在控制台中遇到此错误

Uncaught Error: 'expect' was used when there was no current spec, this could be because an asynchronous test timed out

几天来我一直在尝试解决这个错误,如有任何帮助,我们将不胜感激

我这里也遇到了同样的问题,原来我用setTimeout测试了一下。

所以.. 清除 setTimeOut() 它将 运行

如果您正在实施一项服务,那么您必须有一个与之关联的 UI 组件,以便它呈现 toast(我希望如此)。尝试删除计时器并以这种方式实现它

  ngOnInit(): void {
    this.toastObservable.currentToastVisibility.subscribe((data) => {
      // Do what every logic or work you want
      if (data.visibility) {
        setTimeout(() => {
          this.toastObservable.hideToast();
        }, 3000);
      }
    });
  }

--spec.ts
  it('should call ngOnInit', fakeAsync(() => {
    component.ngOnInit();
    spyOn(toaster, 'hideToast');
    tick(3000);
    expect(toaster.hideToast).toHaveBeenCalled();
  }));