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

时间:2017-09-13 08:19:37

标签: angular unit-testing jenkins karma-jasmine

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();
      }
    )
  });
});

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

2 个答案:

答案 0 :(得分:11)

你应该删除之前的异步(异步(()=&gt; {

答案 1 :(得分:2)

您的所有服务都需要被嘲笑,您只需简单地定义它们并希望jenkins进行api调用。导致jenkins无法正常工作异步并将暂停。

i18nService,
AuthenticationService,

这些服务需要使用mockdata进行模拟。