Karma Jasmine - 在jasmine.DEFAULT_TIMEOUT_INTERVAL指定的超时内未调用异步回调

Jar*_* K. 6 unit-testing jenkins karma-jasmine angular

我对karma/jasmine的Angular4单元测试有问题.我在本地运行PhantomJS浏览器测试,一切都很好.但是当我尝试在jenkins上运行相同的测试时(在PhantomJS上)我得到了错误:

堆栈跟踪

错误:超时 - 在jasmine.DEFAULT_TIMEOUT_INTERVAL指定的超时内未调用异步回调.

来自login-form.component.spec.ts的每个测试都会抛出相同的错误

登录-from.component.spec.ts

describe('LoginFormComponent', () => {
  let fixture;
  let submitBtn: DebugElement;
  let component;
  let authenticationService: AuthenticationService = null;
  let backend: MockBackend = null;
  const requestData = {
    //mock request data
  };
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        LoginFormComponent,
      ],
      imports: [
        CommonModule,
        FormsModule,
        FormElementsModule,
        ReactiveFormsModule,
        RouterTestingModule,
        TranslateModule,
        SharedModule,
        EwniosekSharedModule,
        Ng2PageScrollModule,
        ModalModule.forRoot(),
        VexModalModule,
      ],
      providers: [
        i18nService,
        AuthenticationService,
        BaseRequestOptions,
        {provide: XHRBackend, useExisting: MockBackend},
        {
          provide: HttpService,
          useFactory: (backendInstance: XHRBackend, defaultOptions: BaseRequestOptions) => {
            return new HttpService(backendInstance, defaultOptions);
          },
          deps: [MockBackend, BaseRequestOptions]
        },
        MockBackend
      ],
    }).compileComponents();
    fixture = TestBed.createComponent(LoginFormComponent);
    authenticationService = TestBed.get(AuthenticationService);
    backend = TestBed.get(MockBackend);
    component = fixture.debugElement.componentInstance;
    submitBtn = fixture.debugElement.query(By.css('#submitBtn'));
  }));

  it('should create this component', () => {
    expect(component).toBeTruthy();
  });
  it('should have sumbit button', () => {
    expect(submitBtn).not.toBeNull();
  });
  it('should be avaible on /xxx/login url', () => {
    backend.connections.subscribe((connection: MockConnection) => {
      const options = new ResponseOptions({
        body: JSON.stringify(requestData)
      });
      connection.mockRespond(new Response(options));
      expect(connection.request.url).toEqual('/xxx/login');
    });
  });
  it('should click to submit button to login', () => {
    spyOn<any>(component, 'onSubmit');
    expect(fixture.debugElement.query(By.css('#submitBtn'))).toBeDefined();
    submitBtn.nativeElement.click();
    fixture.detectChanges();
    expect(component.onSubmit).toHaveBeenCalled();
  });
  it('should call login method by URL', (done) => {
    backend.connections.subscribe((connection: MockConnection) => {
      const options = new ResponseOptions({
        body: JSON.stringify(requestData)
      });
      connection.mockRespond(new Response(options));
      expect(connection.request.url).toEqual('/xxx/login');
    });
    authenticationService.login('TEST', 'xxx').subscribe(
      (res) => {
        expect(res.username).toContain('TEST');
        expect(res.password).toContain('xxx');
        expect(res.sex).toContain('male');
        done();
      }
    )
  });
});
Run Code Online (Sandbox Code Playgroud)

任何人都可以告诉我为什么我在这个组件中的每个测试都有这个错误?

小智 16

你应该删除async beforeEach(async(()=> {

  • Pawel移除`async`的原因是什么? (2认同)