question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NX 11 -> 12 Migration issue: zone-testing.js is needed for the fakeAsync()

See original GitHub issue

Current Behavior

After migrating a NX 11 (exclusively Angular) repo to 12, I’m getting the following error when running the Jest unit tests:

    zone-testing.js is needed for the fakeAsync() test helper but could not be found.
            Please make sure that your environment includes zone.js/testing

I noticed that polyfills.ts was updated during migration, changing the zone.js import statement. So, I changed back that line and tried as well. But the same error persisted. I also tried adding import "zone.js/tsting" without any luck.

Expected Behavior

Unit tests to execute without an error.

Steps to Reproduce

Migrate a NX 11 repo with nx migrate latest.

Failure Logs


    zone-testing.js is needed for the fakeAsync() test helper but could not be found.
            Please make sure that your environment includes zone.js/testing

      at resetFakeAsyncZone (../../../packages/core/testing/src/fake_async.ts:25:9)
      at Object.<anonymous> (../../../packages/core/testing/src/test_hooks.ts:39:7)

Environment

NX Report complete - copy this into the issue template

Node : 12.14.1 OS : darwin x64 npm : 7.19.1

nx : 11.1.5 @nrwl/angular : 12.5.8 @nrwl/cli : 12.5.8 @nrwl/cypress : 12.5.8 @nrwl/devkit : 12.5.8 @nrwl/eslint-plugin-nx : 12.5.8 @nrwl/express : Not Found @nrwl/jest : 12.5.8 @nrwl/linter : 12.5.8 @nrwl/nest : Not Found @nrwl/next : Not Found @nrwl/node : Not Found @nrwl/nx-cloud : 12.2.7 @nrwl/react : Not Found @nrwl/schematics : Not Found @nrwl/tao : 12.5.8 @nrwl/web : Not Found @nrwl/workspace : 12.5.8 @nrwl/storybook : 12.5.8 @nrwl/gatsby : Not Found typescript : 4.2.4

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
bridziuscommented, Dec 15, 2021

For people who actually need the environment teardown override - import 'jest-preset-angular/setup-jest'; has to be imported before the other @angular imports for the setup to happen correctly. So:

import 'jest-preset-angular/setup-jest';
import { getTestBed } from '@angular/core/testing';
import {
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting,
} from '@angular/platform-browser-dynamic/testing';

getTestBed().resetTestEnvironment();
getTestBed().initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting(), {
  teardown: { destroyAfterEach: false },
});
1reaction
antoineviscardicommented, Apr 19, 2022

Any tipps how this workaround works with organize imports feature of vscode?

Assuming you use Prettier, the best solution we found in our repo is to rely on Trivago’s plugin. It is quite a bit more configurable than the VSCode feature. It also has the advantage of working regardless of the IDE.

The plugin allows you to set an import order based on regex patterns in your .prettierrc file. Somthing like the following would insure the jest-preset-angular/setup-jest import is always before any other third party module.

  "importOrder": [
    "reflect-metadata",
    "dotenv/config",
    "jest-preset-angular/setup-jest",
    "<THIRD_PARTY_MODULES>",
    "^@MyCompany/shared/(.*)$",
    "^@MyCompany/(.*)$",
    "^[./]"
  ],
Read more comments on GitHub >

github_iconTop Results From Across the Web

Error: zone-testing.js is needed for the fakeAsync() test helper ...
This issue comes into picture from Angular v11. Solution: Move import 'zone.js/dist/zone-testing'; to the top in src/test.ts file.
Read more >
Error: zone-testing.js is needed for the fakeAsync() test helper ...
Current Behavior. After migrating a NX 11 (exclusively Angular) repo to 12, I'm getting the following error when running the Jest unit tests:....
Read more >
Controlling Time with Zone.js and FakeAsync | by Victor Savkin
First, the test is flat: no setTimeout calls required. Flat tests are easier to follow, refactor, and debug. Second, if one of the...
Read more >
fakeAsync - Angular
Wraps a function to be executed in the fakeAsync zone: Microtasks are manually executed by calling flushMicrotasks() . Timers are synchronous; tick() simulates ......
Read more >
license_scanning (#10019) · Jobs · osm / NG-UI · GitLab
Panning and zooming are widely used in web-based mapping, ... which defaults to:\n\n```js\nfunction touchable() {\n return navigator.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found