Fateh Mohamed
Fateh Mohamed

Reputation: 21367

Jasmine 3 errors in Angular 5.2 tests

I have some errors after running ng test, I got that after doing an upgrade to all my packages, here are the versions:

{
"@angular/animations": "^5.2.0",
"@angular/common": "^5.2.0",
"@angular/compiler": "^5.2.0",
"@angular/core": "^5.2.0",
"@angular/forms": "^5.2.0",
"@angular/http": "^5.2.0",
 ...
 "zone.js": "^0.8.19"
 }

and devDependencies

"jasmine-core": "~3.1.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~2.0.0",
"karma-chrome-launcher": "~2.2.0"

I got this error:

"message": "An error was thrown in afterAll\n[object ErrorEvent]", "str": "An error was thrown in afterAll\n[object ErrorEvent]"

karma screenshot

All my tests are very simple like:

import { async, TestBed  } from '@angular/core/testing';
import { UserInmailComponent } from './user-inmail.component';

describe('Component: UserInmail', () => {
it('should create an instance', () => {
  const component = new UserInmailComponent();
  expect(component).toBeTruthy();
});
});

I don't think that my test files are causing the problem.

Upvotes: 2

Views: 1564

Answers (3)

Scott Williams
Scott Williams

Reputation: 23

Based on my research, this seems like a very broad error that can come about from many developer errors. My specific error was making a call to HttpClient (HttpClientTestingModule) with a url that was undefined. The error was relatively supressed in my browser and once i fixed it, the test run passes every time now. I've spent two days on this pulling my hair out, but all i needed to do was jasmine spyOn.and.callThrough() instead of just using spyOn

Upvotes: 1

user3292546
user3292546

Reputation: 359

I also had trouble with this error message in several unit tests even with "jasmine-core v2.8.0". After some researching I was able to reproduce it and discovered that it happens when you use mocks or stubs that lack neccessary variables (in my case > observables).

So for example if you init your TestBed like this:

TestBed.configureTestingModule({
  declarations: [HeaderComponent],
  imports: [BrowserModule],
  providers: [
    {provide: AuthService, useClass: AuthServiceStub}
  ]
})

and you have an observable in AuthService which is somehow used inside your HeaderComponent but you forgot to define this observable inside your "AuthServiceStub" then it will lead to the "An error was thrown in afterAll\n[object ErrorEvent]" output.

It's a very confusing behavior of jasmine/karma because it doesn't give you any hint on what is going wrong. In my opinion it should throw a null-pointer exception because the attribute is not defined inside the stub.

Upvotes: 4

Luca Luve
Luca Luve

Reputation: 19

it's related to a bug in zone.js you have to downgrade jasmine version to 2.9 to make it work

"jasmine-core": "~2.9.0",

Upvotes: 1

Related Questions