Title: `@angular/build:unit-test`: with the default jsdom runner and zone.js, an export from a shared chunk is `undefined` when read during class-field initialisation from a spec with an async test · Issue #33728 · angular/angular-cli · GitHub
Open Graph Title: `@angular/build:unit-test`: with the default jsdom runner and zone.js, an export from a shared chunk is `undefined` when read during class-field initialisation from a spec with an async test · Issue #33728 · angular/angular-cli
X Title: `@angular/build:unit-test`: with the default jsdom runner and zone.js, an export from a shared chunk is `undefined` when read during class-field initialisation from a spec with an async test · Issue #33728 · angular/angular-cli
Description: Command test Is this a regression? Not known to be — this is the @angular/build:unit-test builder's default (jsdom) runner; I have no earlier version where this worked. Description Under the unit-test builder's default jsdom runner, a mo...
Open Graph Description: Command test Is this a regression? Not known to be — this is the @angular/build:unit-test builder's default (jsdom) runner; I have no earlier version where this worked. Description Under the unit-t...
X Description: Command test Is this a regression? Not known to be — this is the @angular/build:unit-test builder's default (jsdom) runner; I have no earlier version where this worked. Description Under the un...
Opengraph URL: https://github.com/angular/angular-cli/issues/33728
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`@angular/build:unit-test`: with the default jsdom runner and zone.js, an export from a shared chunk is `undefined` when read during class-field initialisation from a spec with an async test","articleBody":"### Command\n\ntest\n\n### Is this a regression?\n\nNot known to be — this is the `@angular/build:unit-test` builder's default (jsdom) runner; I have no earlier version where this worked.\n\n### Description\n\nUnder the `unit-test` builder's default jsdom runner, a module that esbuild places in a chunk shared between spec entry points is wrapped in a lazy `__esm` initialiser, and a consumer in a *different* chunk reads its exports as `undefined` during class-field initialisation. The same specs pass when the runner is switched to browser mode with the same bundles, so this looks like the Node-side module evaluation path rather than a bundling fault.\n\nThe distinction that makes it precise:\n\n- a service reads the shared `const` **inside a method** → always correct;\n- a component reads the same `const` in a **class-field initialiser** → `undefined`.\n\nThe binding is live — replacing the fields with getters (reading the same import *later*) makes the failing test pass with no other change. So the value arrives; at construction time it is not yet visible to the importer, even though the emitted code calls `init_env_config()` earlier in the same module body and construction happens later still.\n\n**Trigger conditions** — each toggled on its own with everything else constant; all four are required:\n\n| # | Required condition | Evidence |\n|---|---|---|\n| 1 | ≥ 2 spec entries import the module (shared chunk, lazy `__esm` wrappers) | a single spec file inlines everything and passes |\n| 2 | a consumer in another chunk reads the export during class-field initialisation | getters pass |\n| 3 | the consuming spec file contains an **async test callback** | making that one test synchronous passes; `async` with **no** `await` still fails |\n| 4 | **zone.js** is in the unit-test polyfills | deleting `\"polyfills\": [\"zone.js\"]` from the unit-test build configuration makes the same suite pass; restoring it fails again |\n\nNot required (removed with the failure surviving): `TestBed.configureTestingModule()` / `compileComponents()` (a bare `TestBed.createComponent()` inside an async test fails), provider overrides, barrel files, `@angular/core` patch level (fails on 21.2.18 and 21.2.19).\n\nConditions 3 and 4 are one mechanism: with zone.js present the builder downlevels `async`/`await`, so a spec containing any async function imports the `__async` helper — and esbuild then emits that spec entry **CommonJS-wrapped**, with its entry-local ESM dependencies (the component module) as lazy `__esm` initialisers:\n\n```js\n// spec-…-lead-generator.container.js (dumped with ng test --dump-virtual-files)\nimport { DEALERS, LeadService, init_env_config, init_lead_service } from \"./chunk-N64URYIK.js\";\nimport { __async, __commonJS, __esm } from \"./chunk-EFV3TZQO.js\";\n\nvar LeadGeneratorContainer;\nvar init_lead_generator_container = __esm({\n \"…/lead-generator.container.ts\"() {\n init_services();\n init_env_config(); // runs before the class statement below\n LeadGeneratorContainer = class _LeadGeneratorContainer {\n leadService = inject(LeadService);\n dealers = DEALERS; // undefined at construction time\n dealerOptions = this.dealers.map(…); // throws\n };\n }\n});\n\nvar require_lead_generator_container_spec = __commonJS({\n \"…/lead-generator.container.spec.ts\"(exports) {\n init_lead_generator_container();\n describe(\"LeadGeneratorContainer\", () => { … });\n }\n});\nexport default require_lead_generator_container_spec();\n```\n\nThe shared chunk assigns `DEALERS` inside `init_env_config()`'s `__esm` wrapper and exports the binding; the emitted code is valid ESM and runs correctly in browser mode.\n\nOne caveat for anyone building a paraphrased fixture: the failure is sensitive to inert content — adding a top-level `console.log` to either the shared or the importing module defuses it, and one hand-written structural clone of the same shape passes. The linked reproduction is the exact shape that fails deterministically.\n\n### Minimal Reproduction\n\n**https://github.com/jonmarozick/ng-shared-chunk-repro**\n\n```\nnpm install\nnpm test # ng test --no-watch\n```\n\nExpected: 2 passed. Actual: `Test Files 1 failed | 1 passed (2)` — `lead-generator.container.spec.ts` fails, `lead.service.spec.ts` (the control reading the same const in a method) passes.\n\nNine source files, dependencies are only Angular 21.2.19 + Vitest 4 + jsdom, `package-lock.json` committed. The module graph:\n\n```\nsrc/environments/env-config.ts exports `const DEALERS`\n ├── lead.service.ts reads DEALERS in a method\n │ ├── lead.service.spec.ts ← spec entry 1 (passes)\n │ └── lead-generator.container.ts\n └── lead-generator.container.ts reads DEALERS in a field initialiser\n └── lead-generator.container.spec.ts ← spec entry 2 (fails)\n```\n\nAlso reproduced by copying these source files unchanged into a fresh `ng new` workspace on CLI 21.2.19 (`@angular/core` 21.2.19) with `\"polyfills\": [\"zone.js\"]` added to the unit-test build configuration — so nothing about it depends on the original project. The emitted bundles can be inspected via `ng test --dump-virtual-files` (they land in `.angular/cache/\u003cversion>/\u003cproject>/unit-test/output-files/`).\n\n### Exception or Error\n\n```text\nTypeError: Cannot read properties of undefined (reading 'map')\n ❯ _LeadGeneratorContainer.\u003cinstance_members_initializer> src/app/features/lead-generator/lead-generator.container.ts:291:43\n ❯ new _LeadGeneratorContainer src/app/features/lead-generator/lead-generator.container.ts:336:5\n ❯ NodeInjectorFactory.LeadGeneratorContainer_Factory [as factory] spec-app-features-lead-generator-lead-generator.container.js:1138:16\n ❯ getNodeInjectable packages/core/src/render3/di.ts:785:37\n ❯ instantiateAllDirectives packages/core/src/render3/instructions/shared.ts:122:2\n ❯ createComponentRef packages/core/src/render3/component_ref.ts:301:20\n ❯ create packages/core/testing/src/test_bed.ts:695:44\n```\n\n### Your Environment\n\n```text\nAngular CLI: 21.2.19\nNode: 24.18.0\nPackage Manager: npm 11.16.0\nOS: win32 x64\n\n@angular/build 21.2.19\n@angular/cli 21.2.19\n@angular/core 21.2.18\n@angular/compiler-cli 21.2.18\ntypescript 5.9.3\nvitest 4.1.10\nzone.js 0.16.2\njsdom (builder default)\n```\n\nAlso reproduced on Linux CI (ubuntu-latest, Node 24) and in a fresh `ng new` workspace with `@angular/core` 21.2.19.\n\n### Anything else relevant?\n\n- Not avoidable through runner configuration: `isolate` true/false, `pool` forks/threads/vmThreads, `fileParallelism: false`, and dropping `runnerConfig` entirely all still fail. Removing zone.js avoids it but is not available to a zone-based app, and \"don't write async tests\" is not a workaround.\n- The impact can be silent: in a sibling project on the same setup, jsdom reported 108 of 216 tests (three spec files never loaded) in half the wall-clock of browser mode — a faster jsdom run can mean less of the suite ran.\n- Not tested on 22.x (project fleet is pinned to v21 LTS); the two vitest-related 22.x changelog entries don't touch the module-interop path.\n- Possibly related open issues, none the same: #32252, #33047, #33570.\n- I have a verified fix (disabling code splitting for the unit-test build only, mirroring the polyfills-bundle precedent) and will open a PR referencing this issue.\n","author":{"url":"https://github.com/jonmarozick","@type":"Person","name":"jonmarozick"},"datePublished":"2026-08-02T19:07:23Z","url":"https://github.com/angular/angular-cli/issues/33728"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:925fe19b-80b2-f21c-f258-b5ca8736a6b3 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AB16:16EED3:3479C6E:42E49A6:6B01E435 |
| html-safe-nonce | 29fb0f3cc7c526a469be6a846a94a54e1dd79d1870b3f8f48a46451677d605d1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQjE2OjE2RUVEMzozNDc5QzZFOjQyRTQ5QTY6NkIwMUU0MzUiLCJ2aXNpdG9yX2lkIjoiNzMwNTA3Njc4NzY4NjUzMjE0OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 812677d976baeba4dcbe9e50e05c69b4d259a07792ee73d5c5e950c99fa22689 |
| hovercard-subject-tag | issue:5044032510 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular-cli/33728/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d3270ff76c82e14e8b7fc26587c6833e4644faf3af4c9b888eb5357148ff0aa9/angular/angular-cli/issues/33728 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d3270ff76c82e14e8b7fc26587c6833e4644faf3af4c9b888eb5357148ff0aa9/angular/angular-cli/issues/33728 |
| og:image:alt | Command test Is this a regression? Not known to be — this is the @angular/build:unit-test builder's default (jsdom) runner; I have no earlier version where this worked. Description Under the unit-t... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jonmarozick |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular-cli git https://github.com/angular/angular-cli.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 36891867 |
| octolytics-dimension-repository_nwo | angular/angular-cli |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 36891867 |
| octolytics-dimension-repository_network_root_nwo | angular/angular-cli |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width