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

后端 未结 17 2316
生来不讨喜
生来不讨喜 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 09:59

    Try if you get a more descriptive error message by running the test from the terminal, like this:

    ng test -sm=false
    

    In your test, you can replace

    it('should...')
    

    with

    fit('should...') 
    

    Now only tests preceded by fit will run. To leave the browser open after running the test, run the test like this:

    ng test -sm=false --single-run false
    

    Personally, I have encountered this error twice. Both were only triggered when calling fixture.detectChanges().

    The first time, I solved it by using string interpolation more safely in my .html file.

    Unsafe example:

    {{user.firstName}}

    Safe(r) example (note the question mark):

    {{user?.firstName}}

    The same may apply to property binding:


    The second time, I was using a DatePipe in my .html file, but the mock property that I used it on was not a date.

    .html file:

    {{startDate | date: 'dd-MM-yyyy'}}

    .ts (mock-data) file (wrong):

    let startDate = 'blablah';
    

    .ts (mock-data) file (correct):

    let startDate = '2018-01-26';
    

提交回复
热议问题