TestBed should be zoneless compatible

This issue has been created since 2022-11-23.

Which @angular/* package(s) are relevant/related to the feature request?

core

Description

TL;DR: TestBed should provide a way to run zoneless and avoid importing zone.js when we don't need it.
And I'd love to help with a PR?

TestBed assumes that zone.js is available while there are contexts when this is not necessary and here are some examples:

  • zoneless apps & libs
  • tests that do not depend on change detection: service tests, pipe tests, "isolated" component testing etc...
  • tests where CD is triggered manually using fixture.detectChanges()

These tests must load zone.js when they don't need it.
While this might sound like a really minor issue, loading zone.js can cause trouble in some situations.

For instance, integrating TestBed and zone.js with vitest causes the following error Method Promise.prototype.then called on incompatible receiver which is probably due to zone.js patching promises and breaking Node.js's safe promises when importing ESMs dynamically or something like that. This is similar to the issue described here #47872.

Proposed solution

A first quick win is replacing this:

const ngZone = new NgZone({enableLongStackTrace: true});
const providers: Provider[] = [
{provide: NgZone, useValue: ngZone},

with:

{provide: NgZone, useFactory: () => new NgZone({enableLongStackTrace: true}); }

this would then allow us to go zoneless with this configuration:

TestBed.configureTestingModule({
  providers: [
    {
      provide: NgZone,
      useClass: ɵNoopNgZone,
    },
  ],
});

The main problem with this approach is the boilerplate and having to use the ɵNoopNgZone implementation detail.

A second step would be adding the bootstrap's ngZone option to initTestEnvironment:

getTestBed().initTestEnvironment(
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting(),
  {ngZone: 'noop'}
);

Alternatives considered

Disable promises monkey patching using globalThis.__Zone_disable_ZoneAwarePromise = true
but this causes the following issue #37349

AndrewKushnir wrote this answer on 2022-11-23

@yjaaidi thanks for submitting this ticket.

The first step of making the new NgZone creation lazy makes sense, please let us know if you are interested in submitting a PR. As you mentioned, that should provide an ability to replace ZoneJS implementation and experiment with vitest integration.

The second step of exposing a config option would require some discussions within the team. I'll share an update once I have it.

Thank you.

More Details About Repo
Owner Name angular
Repo Name angular
Full Name angular/angular
Language TypeScript
Created Date 2014-09-18
Updated Date 2022-12-01
Star Count 85220
Watcher Count 3042
Fork Count 22607
Issue Count 1316

YOU MAY BE INTERESTED

Issue Title Created Date Comment Count Updated Date
Cannot jump to exact coordinates 1 2021-04-07 2022-05-11
Fix empty fields in /v1/screst getWriteMarkers 3 2022-09-12 2022-10-13
Add predefined data attributes to all the components like in radix primitives 0 2022-05-21 2022-11-27
Don't include the whole license text in the 'license' field 2 2022-04-17 2022-11-27
Use correct Icon for Windows shortcuts instead of generic one 1 2021-02-27 2022-11-18
How to get Google Sheets to JSON data 0 2021-05-10 2022-10-15
Weighted policy for capacity type 1 2022-01-20 2022-11-21
FileNotFoundException 1 2020-10-29 2022-10-06
rating 组件 item 子组件多次重复点击不会 re-render 2 2021-10-28 2022-10-21
Position:Absolute not being proccesed 6 2017-08-08 2022-11-22
after updating the version of rails and ruby, I am getting error 2 2021-02-18 2022-11-08
Potential deadlock in ConsensusAlgorithm::NBX on some systems 8 2022-11-09 2022-11-23
TypeError: 'NoneType' object is not subscriptable 2 2022-04-01 2022-08-26
Rust: Improve compile times 1 2021-10-27 2022-11-11
Improve Node.js custom build phase section 0 2021-08-18 2022-10-22
Deprecated `CC_NODE_START_GOAL` env variable 0 2021-08-18 2022-11-07
Workflow example for Github & Gitlab 0 2021-08-11 2022-09-25
创建新的题目上传测试用例之后里面的.in和.out文件不自动解压出来 1 2021-12-02 2022-11-29
v4.0 bug report - category search not working 7 2022-06-28 2022-11-11
Bot. Remove locations in groups. 0 2022-11-19 2022-11-22
Maskable & Monochrome Icons 0 2022-09-29 2022-11-18
New functions for CGRect aspect resize/center 7 2013-05-29 2022-11-15
Table Names / Sections are not visible on main dashboard 1 2021-06-09 2022-10-19
Improve reliability of import CSV feature 0 2021-12-21 2022-10-27
国家药品监督管理局药品审评中心 指导原则专栏 0 2022-07-05 2022-08-08
can not work with the windows ad 2 2021-09-12 2022-11-22
Adding n-n navigation via abp suite there is typehead without select option with add button, only adds typehead input with add button. 2 2022-04-22 2022-10-08
If keep pressed and re-enter the window when using the Eyedropper, the preview will not display 1 2022-04-24 2022-11-08
Accessibility: Screen readers no longer accurately report current ctrl+l result 10 2021-12-21 2022-08-17
Build failing on INSTALL_PODS step 5 2022-02-01 2022-10-10
setting path in fig doesn't recognize $HOME 4 2022-06-28 2022-11-10
convert uploaded image to base64 11 2020-05-12 2022-11-20
Refactor generation of missing tsconfig.json when analysing TypeScript files in SonarLint 0 2022-11-18 2022-11-16
Add new endpoint to eslint-bridge server to create tsconfig.json files 0 2022-11-18 2022-11-16
server segfaults when remain-on-exit is enabled and in control mode when the processes die (or do not start) 4 2021-08-17 2022-09-30
[Bug] Xamarin.Forms is not able to Archive the Android project - multiple (random) AOT compile errors 2 2022-03-24 2022-09-22
[Bug] Wrong Route Matcher Generated when a Route Method Takes No Parameters and Has Path Parameters 4 2022-07-13 2022-11-15
连接pulse secure访问不了ipv6 7 2020-02-12 2022-11-25
Nightly Testing Report 4 2022-05-15 2022-11-12
Windows binary jobs failing with CUDA 11.5 0 2022-02-06 2022-11-06
SYLK shared formulae 2 2021-08-10 2022-11-15
Sharing a board in Business OneDrive - Recipient not finding the shared folder 6 2022-01-12 2022-10-02
Can't connect to server + Crash on 'SSL / Authentication' option click 1 2019-02-14 2022-11-09
Severe memory leak on failed connection 18 2021-02-27 2022-11-25
[Bug-FE][UI Next][V1.0.0-Alpha]The set runtime and cron rules show inconsistencies with the next five execution times 4 2022-03-25 2022-09-19
All Desktop samples have a major content shifting issue after resizing a window 4 2021-03-30 2022-11-23
Extend search field | username, hostname 2 2022-06-14 2022-11-23
Q&A w/ @ecowden on Individual Membership Candidacy 7 2016-01-18 2022-11-18
With gin v1.8.0, gin.Context is canceled within goroutine 13 2022-05-31 2022-10-02
Version 1.1.0 taged is wrong 2 2021-12-08 2022-11-30