我正在尝试找到解决此错误的方法:
错误:超时 - 在jasmine.DEFAULT_TIMEOUT_INTERVAL指定的超时时间内未调用异步回调
当我使用无头浏览器运行karma单元测试时,如果使用chrome浏览器运行测试通过,当我的角度组件几乎没有测试通过的html行时。
这是我的测试文件,
import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';
fdescribe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent
],
}).compileComponents();
}));
it('should create the app', async(() => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app).toBeTruthy();
}));
});
我的组件非常简单,只加载html模板
import { Component } from '@angular/core';
@Component({
selector: 'pm-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
pageTitle: string = 'Angular: Getting Started';
}
答案 0 :(得分:2)
iHazCode指出解决方法,使用fakeAsync代替异步
从' @ angular / core / testing'中导入{TestBed,async,fakeAsync};
从' ./ app.component';
导入{AppComponent}fdescribe('AppComponent', () => {
beforeEach(fakeAsync(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent
],
}).compileComponents();
}));
it('should create the app', async(() => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app).toBeTruthy();
}));