I am trying to run component testing on Angular web app using serenityJS + Jasmine but encountered the error below. Any idea how I can resolve this issue?
Message:
Error: zone-testing.js is needed for the fakeAsync() test helper but could not be found.
Please make sure that your environment includes zone.js/dist/zone-testing.js
Stack: Error: zone-testing.js is needed for the fakeAsync() test helper but could not be found. Please make sure that your environment includes zone.js/dist/zone-testing.js at resetFakeAsyncZone (C:\Users\zhenweiwong\Desktop\serenityjsframework\packages\core\testing\src\fake_async.ts:25:9) at UserContext. (C:\Users\zhenweiwong\Desktop\serenityjsframework\packages\core\testing\src\before_each.ts:26:5) at at processImmediate (internal/timers.js:439:21) at process.topLevelDomainCallback (domain.js:131:23) Message: Error: In this configuration Angular requires Zone.js Stack: Error: In this configuration Angular requires Zone.js at new NgZone (C:\Users\zhenweiwong\Desktop\serenityjsframework\packages\core\src\zone\ng_zone.ts:129:13) at TestBedViewEngine._initIfNeeded (C:\Users\zhenweiwong\Desktop\serenityjsframework\packages\core\testing\src\test_bed.ts:409:9) at TestBedViewEngine.createComponent (C:\Users\zhenweiwong\Desktop\serenityjsframework\packages\core\testing\src\test_bed.ts:599:10) at Function.TestBedViewEngine.createComponent (C:\Users\zhenweiwong\Desktop\serenityjsframework\packages\core\testing\src\test_bed.ts:245:36)
at UserContext. (C:\Users\zhenweiwong\Desktop\serenityjsframework\jasmine-test\spec\app1.component.spec.ts:32:29) at
Hello little bit late to answer but I leave the comment here for future reference:
ERROR ORIGIN
In my case, the error was found in the test.ts file. This file sets the TestBed config that is used by default in Angular spec files when created with the command ng generate component my-component .
SOLUTION
The fix is to make sure that the following imports are at the top of the file test.ts before any other import:
import 'zone.js';
import 'zone.js/testing';
Be careful with your configurations that might alter the order of those imports as it may lead to break the tests again. That's why you might, over the test.ts file:
FURTHER TROUBLESHOTING
You might also want to look if the following imports are present:
import 'zone.js';
import 'zone.js/dist/async-test.js';
import 'zone.js/dist/proxy.js';
import 'zone.js/dist/sync-test';
import 'zone.js/dist/jasmine-patch';
Same issue was solved with other approaches in this thread .
This issue comes into picture from Angular v11.
Solution: Move import 'zone.js/dist/zone-testing';
to the top in src/test.ts
file.
I solved this problem by using Angular waitForAsync
.
Instead of:
beforeEach(async () => {
await TestBed.configureTestingModule({declarations: [BannerComponent]}).compileComponents();
});
Try to use the following:
import { waitForAsync } from '@angular/core/testing';
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({declarations: [BannerComponent]}).compileComponents();
}));
In Vscode open file test.ts .
Add line down on top file, first line .
import 'zone.js/testing';
OBS: if your vscode organize your imports there file, use o organize-imports-disable-next-line .
Example:
import 'zone.js/testing';
// organize-imports-disable-next-line
For me this error because of syntax,
From
beforeEach(async(async () => {
await TestBed.configureTestingModule({declarations:
[ListComponent]}).compileComponents();
}));
To
beforeEach(async () => {
await TestBed.configureTestingModule({declarations:
[ListComponent]}).compileComponents();
});
I ended up here after getting a similar issue in my app after upgrading from Angular 14 to Angular 15. I solved it this way by:
angular.json
file and in the project "test"
changing its "options"
by removing "main": "src/test.ts",
and adding this instead:"polyfills": ["zone.js", "zone.js/testing"],
src/test.ts
file can now be deleted
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.