How do I debug a “[object ErrorEvent] thrown” error in my Karma/Jasmine tests?

后端 未结 17 2315
生来不讨喜
生来不讨喜 2020-12-04 09:51

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 t

17条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-04 10: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();
    }));
    

提交回复
热议问题