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?
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?
In my own case, the snippet below resolved my issue.
afterEach(() => {
fixture.destroy();
TestBed.resetTestingModule();
});
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.
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.)
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.
© 2022 - 2024 — McMap. All rights reserved.