Error: zone-testing.js is needed for the fakeAsync() test helper but could not be found. When running test on Angular webapp using serenityJS+Jasmine
Asked Answered
H

8

42

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.<anonymous> (C:\Users\zhenweiwong\Desktop\serenityjsframework\packages\core\testing\src\before_each.ts:26:5)
       at <Jasmine>
       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.<anonymous> (C:\Users\zhenweiwong\Desktop\serenityjsframework\jasmine-test\spec\app1.component.spec.ts:32:29)
       at <Jasmine>
Hodgkinson answered 6/5, 2021 at 8:13 Comment(2)
I have the same problem but using Jest instead of Jasmine, have you ever find a way to fix it?Derbyshire
@Derbyshire the top-voted answer at the present time works in Jest for me (importing "zone.js" and "zone.js/testing" at the very top of the setupJest.ts file). Angular 12/Jest 27/TS 4.2 environment.Bulbous
S
63

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 you place the following imports at the TOP of the file test.ts before any other import:

// Top of test.ts file
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:

  1. Deactivate onSave optimize imports.
  2. Avoid running Prettier, Beautify or other code style formatter that reorganizes the imports.
  3. Check for pre-commits hooks like Husky that format the files and reorganize the imports.

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.

Soul answered 16/8, 2021 at 4:51 Comment(4)
you might need to use import 'zone.js/dist/zone-testing'; instead of import 'zone.js/testing'; & it should be on top of all the imports (it worked for me while the other one kept generating the error Expected to be running in 'ProxyZone', but it was not found.) according to this link: github.com/angular/angular/issues/40977#issuecomment-785800709Aksoyn
Thanks buddy u saved my day, I have placed import 'zone.js/testing'; at top in test.ts and it worked for meTestamentary
Had the same issue on my side with an Angular v14 project. The test.ts file only had the 'zone.js/testing' import and moving it on top of the file fixed my issues.Peccavi
After updating to Angular 17 I had the same issue, and it was due to previously having import 'zone.js/dist/zone-testing'; So the solution worked here, and should be marked as the solution (might not be a global solution, but it was a solution for the actual problem).Yggdrasil
T
23

I got the same issue after upgraded from the Angular 10 to Angular 12. Then I fixed it by move the import 'zone.js/testing to the top of the file test.ts

enter image description here

Tranquilize answered 17/11, 2021 at 7:51 Comment(0)
O
9

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

See more

Ogre answered 13/7, 2022 at 2:54 Comment(2)
It's confusing that you "disable" the next line, but put the thing that needs to be disabled a above the disable comment. Either way though, it works. My tests started breaking after I added the organize imports option. Greatly appreciated!Turbellarian
It seems that you can just use a blank line between import 'zone.js/testing'; and all other imports, rather than this disable commandJotting
I
9

Angular 15

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:

  1. editing the 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"],
  1. the src/test.ts file can now be deleted
Improbability answered 25/12, 2022 at 14:40 Comment(0)
S
8

This issue comes into picture from Angular v11.

Solution: Move import 'zone.js/dist/zone-testing'; to the top in src/test.ts file.

Sheehy answered 21/1, 2022 at 11:32 Comment(0)
G
2

I fixed the same issue by adding import 'jest-preset-angular/setup-jest'; in setupJest.ts which is used in jest.config.js:

module.exports = {
  preset: './jest.preset.js',
  setupFilesAfterEnv: [
    "<rootDir>/setupJest.ts"
  ],
...

I am using Angular 14 with Jest@^28.1.3

Guyon answered 20/4, 2023 at 7:38 Comment(0)
F
0

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();
}));
Forth answered 31/3, 2022 at 6:20 Comment(0)
P
0

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();
});
Peck answered 8/10, 2022 at 0:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.