How do I debug a “[object ErrorEvent] thrown” erro

2019-01-10 10:22发布

I have several failing tests that only output [object ErrorEvent] thrown. I don't see anything in the console that helps me pinpoint the offending code. Is there something I need to do to track these down?

[EDIT]: I'm running Karma v1.70, Jasmine v2.7.0

14条回答
神经病院院长
2楼-- · 2019-01-10 11:11

In my case the problem was with the service, as one of the object wil be undefined during tests running.

Service code sample was something like below access to dom,

  const elem = this.document.querySelector(element) as HTMLElement;
  elem.scrollIntoView({param1: ''});

The specs referring to this service were failing with the error '[object ErrorEvent] thrown'.

I mocked my service object inside all the specs which were referring to this and the issue got resolved.

Mock service

class MockService {
serviceMethod(div) : void {
  testElement = document.querySelector('div') as HTMLElement;
  return testElement;
  } 
}

And use this mock service object in providers as below,

beforeEach(async(() => {

TestBed.configureTestingModule({
  declarations: [Component],
  providers: [
     { provide: Service, useClass: MockService },
    ],
})
  .compileComponents();
}));
查看更多
老娘就宠你
3楼-- · 2019-01-10 11:12

For me it was related to having a promise resolved in the ngOnInit of a component. I had to use async, fakeAsync and tick as well as stubbing out the async service with spyOn

beforeEach(async(
  //... initialise testbed and component
))
beforeEach(fakeAsync(
  // ... setup mocks then call:
  component.ngOnInit()
  tick()
  fixture.detectChanges()
))

Angular - How to unit test component with asynchronous service call

查看更多
登录 后发表回答