Angular 4测试 - 抛出[object ErrorEvent]

5
我正在完成我的Angular应用程序的测试平台。但是在karma-jasmine测试中有一个问题,会抛出一个错误:
```[object ErrorEvent] thrown```
我更新了node_modules,这是我在以下链接中找到的解决方案: 如何调试Karma/Jasmine测试中的"[object ErrorEvent] thrown"错误? 但现在错误会在随机时间出现,有时测试平台没有任何故障,有时会触发上述错误。有什么建议可以永久避免它吗?
PS - 如果需要更多资源,请在评论中告诉我。谢谢! SomeComponent.spec.ts
import { RouterTestingModule } from '@angular/router/testing';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { NgModule } from '@angular/core';
import { HttpClient, HttpClientModule } from '@angular/common/http';
import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';

import { SomeComponent } from './some.component';
import { HttpLoaderFactory } from '../app.module';
import { AppRoutingModule } from '../app-routing.module';    
import { SomeService } from './../services/some.service';

describe('SomeComponent', () => {
  let component: SomeComponent;
  let fixture: ComponentFixture<SomeComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        TranslateModule.forRoot({
          loader: {
            provide: TranslateLoader,
            useFactory: HttpLoaderFactory,
            deps: [HttpClient]
          }
        }),
        HttpClientModule,
        AppRoutingModule,
        FormsModule,
        ReactiveFormsModule ,
        RouterTestingModule,
        NgbModule.forRoot(),
        FormsModule, 
        ReactiveFormsModule,
      ],
      declarations: [
        SomeComponent
       ],
      providers: [
        SomeService
       ]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(SomeComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

你能添加导致错误的代码吗? - Jonathan Corrin
1
@JonathanCorrin添加了spec.ts的代码。错误会在随机时间发生,大多数情况下它是正常工作的。 - Lakindu Gunasekara
这个有更新了吗?我也遇到同样的问题,测试时出现随机问题。 - Yoeri
2个回答

3

我遇到了同样的问题,后来发现升级到 jasmine-core 3.0.0 导致了这个问题,所以我将其降级到了 2.5.2 版本,事情就解决了。

我认为这是因为 karma-jasmine 还不兼容 jasmine 3.0.0

"jasmine": "2.5.2",
"jasmine-core": "2.5.2",
"karma-jasmine": "1.1.2",

想了解更多关于这个问题的细节,请参见:

https://github.com/jasmine/jasmine/issues/1523


这个问题在jasmine核心v2.99.1中也存在。这个方法有效。 - Dream_Cap

0

我也遇到了同样的问题,但是 afterAll() 方法解决了我的问题。我的解决方案在这里

afterAll(() => {
 TestBed.resetTestingModule();
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接