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

4

11

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

Tottering answered 21/5, 2018 at 15:22 Comment(3)
Can you add the test file and the test classErnestoernestus
I am having the same issue, hopefully you fin an answerHorick
Have you been able to fix this? having the same issueSupposal
P
7

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

  afterEach(() => {
    fixture.destroy();
    TestBed.resetTestingModule();
  });
Pulverable answered 7/7, 2020 at 10:24 Comment(1)
@Donovant, I am happy it helped.Pulverable
S
5

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.

Saltatorial answered 3/7, 2018 at 11:4 Comment(0)
I
5

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.)

Ingvar answered 1/2, 2019 at 20:25 Comment(0)
A
1

My solution to this is to add the following to my Karma test.ts file:

getTestBed().initTestEnvironment(
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting(),
  {
    teardown: { destroyAfterEach: true },
  }
);

This is explained in more detail on this dev.to article

This avoids having to write afterEach(() => {...}); in all of my hundreds of tests.

Ammoniate answered 21/6, 2022 at 8:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.