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

后端 未结 17 2322
生来不讨喜
生来不讨喜 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:56

    TL;DR: It may be related to testing routing.

    I'm getting [object ErrorEvent] thrown too. An hour later, traced it to one line of code.

    this.username = this.userService.getUser(this.route.snapshot.paramMap.get('id'))[0];
    

    The problem lies with the test environment attempting to evaluate this.route.snapshot.paramMap.get('id').

    If I replace it with 0, [object ErrorEvent] thrown goes away.

    My userService has a user like so:

    public users = [ ["admin", "First name", "Surname", etc... ] ].
    

    So 0 just gets this user, at index 0.

    Otherwise when normally running my app, this.route.snapshot.paramMap.get('id') is evaluated when the user selects a user to edit from my table of users.

    So in my HTML, *ngFor="let user of users; index as i" loops to display all the users then routerLink="/edit/{{i}}" so you can click on edit buttons for each user, which when clicked go to e.g. http://localhost:4200/edit/0 to edit the aforementioned admin user's details.

提交回复
热议问题