Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"An error was thrown in afterAll\n[object ErrorEvent] thrown" - Angular 4 Unit tests

While I was working on Angular 4 unit tests, one of the pages which uses google maps and agm package showed an error:

An error was thrown in afterAll\n[object ErrorEvent] thrown

Does anyone know what the issue could be?

Console Screenshot

like image 361
Tetali pratyush Avatar asked May 21 '18 15:05

Tetali pratyush


People also ask

What is HttpClientTestingModule?

Using the HttpClientTestingModule and HttpTestingController provided by Angular makes mocking out results and testing http requests simple by providing many useful methods for checking http requests and providing mock responses for each request.

What is Waitforasync?

waitForAsynclinkWraps a test function in an asynchronous test zone. The test will automatically complete when all asynchronous calls within this zone are done. Can be used to wrap an inject call.


3 Answers

In my own case, the snippet below resolved my issue.

  afterEach(() => {
    fixture.destroy();
    TestBed.resetTestingModule();
  });
like image 167
Ifesinachi Bryan Avatar answered Sep 16 '22 13:09

Ifesinachi Bryan


For me, swapping this:

beforeEach(async(() => { //...

for this:

beforeEach(() => { //...

in a test that ran before the one which was apparently failing, helped me to identify the real culprit.

Details: I was getting this error for tests that failed in the entire suite, but passed when run individually. It turned out that it was an earlier test, which was being run asynchronously, that was actually failing.

(That test had a template that had an error in the code, solved with the help of this Stack Overflow answer.)

like image 22
jamesthe500 Avatar answered Sep 18 '22 13:09

jamesthe500


I was also looking for a solution to this issue and found that if I run my test as usual via terminal (ng test), then click debug in the Karma runner Chrome window, then open my dev console, I could see the real output of the error.

I saw a suggestion to run the unit tests with an additional flag; this didn't work for me, it may be worth a try however:

  • CLI v6.x --sourceMap=false
  • CLI v1.x --sourcemaps=false

Source: https://stackoverflow.com/a/46840229

In my case I was mocking a third party package class, and I'd forgotten to mock one of the function calls in the mocked class in my unit tests; it was throwing an error which wasn't bubbling to the terminal.

Apologies that this doesn't solve the error in itself, but perhaps it will get you closer to debugging the crux of the issue and finding a solution as it did for me.

like image 37
Paul Acheson Avatar answered Sep 19 '22 13:09

Paul Acheson