【问题标题】:Karma Jasmine - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVALKarma Jasmine - 在 jasmine.DEFAULT_TIMEOUT_INTERVAL 指定的超时时间内未调用异步回调
【发布时间】:2017-09-13 08:19:36
【问题描述】:

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

堆栈跟踪

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

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

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

谁能告诉我为什么我在这个组件中的每个测试都会出现这个错误?

【问题讨论】:

    标签: angular unit-testing jenkins karma-jasmine


    【解决方案1】:

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

    【讨论】:

    • 这解决了我所有的问题。我不明白为什么 PhantomJS 会发生这种情况。
    • Pawel 删除async 的原因是什么?
    • 哇,非常感谢。他们确实应该改进错误消息,这是非常误导的。
    【解决方案2】:

    您的所有服务都需要被模拟,您不能只是简单地定义它们并希望 jenkins 进行 api 调用。导致 jenkins 无法工作异步并且会超时。

    i18nService,
    AuthenticationService,
    

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

    【讨论】:

      猜你喜欢
      • 2014-05-01
      • 2015-05-26
      • 2017-07-24
      • 1970-01-01
      • 1970-01-01
      • 2016-04-21
      • 2019-09-21
      • 2018-11-21
      相关资源
      最近更新 更多