You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 3ebcbbf
Browse filesBrowse the repository at this point in the historyBrowse files
fix(@angular/build): avoid top-level await and add zoneless option for Vitest runner
Previously, the Vitest unit-test runner used dynamic import strategies (`'dynamic'` and `'dynamic-zone'`) within the generated TestBed initialization virtual file (`createTestBedInitVirtualFile`) to load `zone.js` and `zone.js/testing`.
This logic was fundamentally flawed:
1. When Zone.js was loaded dynamically at runtime via the `'dynamic'` strategy, esbuild did not downlevel `async`/`await` because `isZonelessApp` checked the build target's `polyfills` configuration (which did not explicitly list `zone.js`). Consequently, native async/await microtasks bypassed Zone.js context tracking, breaking Zone.js at runtime.
2. If a project used a local polyfills file (e.g. `polyfills: ["src/polyfills.ts"]`), `isZonelessApp` considered the application zoneful and disabled `async-await` support in esbuild. However, esbuild cannot downlevel top-level await when async/await is downleveled, causing esbuild to reject top-level await unconditionally. Hence, the `'dynamic'` strategy never worked as intended.
3. For zoneless applications (such as `polyfills: []`), the syntactic presence of top-level `await` in the virtual file caused esbuild builds to fail when targeting older browsers or Browserslist targets that lack top-level await support, even though Zone was never present at runtime.
This commit resolves these issues by:
- Eliminating top-level `await import()` from `createTestBedInitVirtualFile` entirely.
- Inverting the polyfill strategy so that Zone.js and its testing entry-point are injected directly into `buildOptions.polyfills` before bundling.
- Introducing a new `zoneless` option for the Vitest runner to explicitly control zoneless test execution:
- `zoneless: true`: Zone.js polyfills are excluded and not loaded.
- `zoneless: false`: Zone.js and `zone.js/testing` are explicitly injected.
- Omitted (`undefined`): Existing explicit polyfills are preserved. For library targets where `polyfills` is undefined, Zone.js is injected if installed, accompanied by a deprecation warning advising users to configure the `zoneless` option.
Fixes#33324
Copy file name to clipboardExpand all lines: packages/angular/build/src/builders/unit-test/schema.json
+4Lines changed: 4 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -265,6 +265,10 @@
265
265
"description": "Specifies the path to a TypeScript file that provides an array of Angular providers for the test environment. The file must contain a default export of the provider array.",
266
266
"minLength": 1
267
267
},
268
+
"zoneless": {
269
+
"type": "boolean",
270
+
"description": "Specifies whether to execute tests in zoneless mode. When set to true, Zone.js polyfills are excluded and Zone.js is not loaded. When set to false, Zone.js and its testing support are explicitly loaded. When omitted, Zone.js is loaded based on the build target polyfills. This option is only available for the Vitest runner."
0 commit comments