首页 > 解决方案 > 如何修复 [karma-server]:未捕获的异常

问题描述

我正在运行 Angular 9 应用程序,并且正在使用以下命令在 jasmine 中运行单元测试来运行测试,一切正常:

ng test

但是,当我使用 watch=false 运行单元测试时,所有测试都通过了,但最后我得到了“ UncaughtException ”。

ng test --watch=false // using this command in SonarQube to generate UT report

在此处输入图像描述

由于上述错误,SonarQube 未生成 UT 报告。

有什么方法可以获取错误的详细信息(日志文件或其他内容)或调试 [karma-server]" UncaughtException 错误...?

我从以下规范文件中收到错误:

profile.component.spec.ts

describe('Client.App.Advanced.ProfileComponent', () => {
    let component: ProfileComponent;
    let fixture: ComponentFixture<ProfileComponent>;
    let httpMock: HttpTestingController;

    function mockHttp(data) {
        const mockReq = httpMock.expectOne((req) => true);
        mockReq.flush(data);
        httpMock.verify();
    }

    beforeEach(async(() => {
        TestBed.configureTestingModule({
            imports: [HttpClientTestingModule, RouterTestingModule],
            declarations: [ProfileComponent],
            providers: [ProfileToolHandlerService ]
        }).compileComponents();
    }));

    beforeEach(fakeAsync(() => {
        httpMock = TestBed.inject<HttpTestingController>(HttpTestingController);
        fixture = TestBed.createComponent(ProfileComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
        // mock request for Login
        mockHttp(null);
        tick();
    }));

    it('shall create the component', () => {
        fixture.detectChanges();
        expect(component).toBeDefined();
    });
}

标签: jasmineangular-clikarma-jasminekarma-runner

解决方案


推荐阅读