Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

FYI: you can find the exact error thrown just by open DevTools Console once your tests are running.

As a quickfix you can try to run your tests without sourcemaps:

CLI v6.0.8 and above
--source-map=false

CLI v6.0.x early versions
--sourceMap=false

CLI v1.x
--sourcemaps=false

Shortcut ng test -sm=false might also work

There is an open issue on that https://github.com/angular/angular-cli/issues/7296

UPDATE: I had that issue as well, so I just migrated to the latest cli and make sure that all packages are updated in package.json also I fully reinstalled the node_modules so now the issue has gone.


If sourcemap=false doesn't help, try to 1) open your browser running the tests 2) click debug button 3) open the console

The error will be there

enter image description here


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:

<p>{{user.firstName}}</p>

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

<p>{{user?.firstName}}</p>

The same may apply to property binding:

<p [innerText]="user?.firstName"></p>

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:

<p>{{startDate | date: 'dd-MM-yyyy'}}</p>

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

let startDate = 'blablah';

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

let startDate = '2018-01-26';

This is because the jasmine framework can not handle the ErrorEvent type so it does not extract the error message and calls error.toString() on that object instead.

I just filed an issue at jasmine repo https://github.com/jasmine/jasmine/issues/1594

As long as it is not fixed, you can temporarily patch your installed jasmine package in the node_modules folder. In my case it is

node_modules/jasmine/node_modules/lib/jasmine-core/jasmine.js

and then change the implementation of the ExceptionFormatter from this

if (error.name && error.message) {
    message += error.name + ': ' + error.message;
} else {
    message += error.toString() + ' thrown';
}

to this

if (error.name && error.message) {
    message += error.name + ': ' + error.message;
} else if (error.message) {
    message += error.message;
} else {
    message += error.toString() + ' thrown';
}

It helps to identify the issue.