使用玩笑和旁观者的角度测试失败 TypeError: testing.waitForAsync is not a function

问题描述

我正在使用 角 CLI:10.2.3 节点:12.22.1 npm: 6.14.12

我是 angular 的新手。我正在尝试使用 jest 来测试 angular 代码(项目运行良好,没有任何错误,现在在我看到问题的地方添加了一些测试)。我有很多自定义服务,所以使用 spectator 来模拟它们。我的测试很少,其中一个(失败)在下面。

一个简单的组件测试如下。

import { HttpClientTestingModule } from '@angular/common/http/testing';
import { MatSnackBarModule } from '@angular/material/snack-bar';
import { MatDialog } from '@angular/material/dialog';
import { RouterTestingModule } from '@angular/router/testing';
import { MyComponent } from './my.component';

import { BasicAuthService } from './../../services/auth/basic-auth.service';
import { RefdataService } from '../../services/common/refdata.service';
import { of } from 'rxjs';
import {MyUploadService} from '../../services/data/my-upload.service';

import { 
  createComponentFactory,createHostFactory,spectator,spectatorHost
} from '@ngneat/spectator/jest';

import { ObjectKeysPipe } from '../../pipes/common/object-keys.pipe';
import { DatePipe } from '@angular/common';

import { MockProxy,mock,mockReset } from 'jest-mock-extended'; 
import { NO_ERRORS_SCHEMA } from '@angular/core';

//Create a MatDialog mock class 
export class MatDialogMock {
  
  open() {
    return {
      afterClosed: () => of({action: true})
    };
  }
}

describe('MyUploadComponent',() => {

  let spectator: spectator<MyUploadComponent>;

  const createComponent = createComponentFactory({
    component: MyUploadComponent,declarations: [MyUploadComponent,ObjectKeysPipe],imports: [HttpClientTestingModule,MatSnackBarModule,RouterTestingModule
             ],schemas: [NO_ERRORS_SCHEMA],mocks: [BasicAuthService,RefdataService,MyUploadService
           ],detectChanges: false
  });

  beforeEach(()=> {
    spectator= createComponent();
    spectator.detectChanges();
  });

  it('should create',() => {    
    expect(spectator.component).toBeTruthy();
  });

});

我收到以下错误

 FAIL  src/app/components/my-upload/my-upload.component.spec.ts
  ? Test suite Failed to run

    TypeError: testing.waitForAsync is not a function

      38 |   let spectator: spectator<MyUploadComponent>;
      39 |
    > 40 |   const createComponent = createComponentFactory({
         |                           ^
      41 |     component: MyUploadComponent,42 |     declarations: [MyUploadComponent,43 |     imports: [HttpClientTestingModule,at Object.baseCreateComponentFactory [as createComponentFactory] (node_modules/projects/spectator/src/lib/spectator/create-factory.ts:77:5)
      at Object.createComponentFactory (node_modules/projects/spectator/jest/src/lib/spectator.ts:19:10)
      at src/app/components/my-upload/my-upload.component.spec.ts:40:27
      at ZoneDelegate.Object.<anonymous>.ZoneDelegate.invoke (node_modules/zone.js/dist/zone.js:386:30)
      at Zone.Object.<anonymous>.Zone.run (node_modules/zone.js/dist/zone.js:143:47)
      at Object.<anonymous> (src/app/components/my-upload/my-upload.component.spec.ts:36:1)

我找不到与此错误相关的任何信息。我没有明确使用waitForAsync。这里应该是什么问题?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)