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

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.

Leave a Comment