Title: karma builder: component imported through a barrel is undefined in TestBed when another spec has an async test (cross-chunk lazy init, karma counterpart of #33728) · Issue #34250 · angular/angular-cli · GitHub
Open Graph Title: karma builder: component imported through a barrel is undefined in TestBed when another spec has an async test (cross-chunk lazy init, karma counterpart of #33728) · Issue #34250 · angular/angular-cli
X Title: karma builder: component imported through a barrel is undefined in TestBed when another spec has an async test (cross-chunk lazy init, karma counterpart of #33728) · Issue #34250 · angular/angular-cli
Description: Which @angular/* package(s) are the source of the bug? build Is this a regression? No Description With the @angular/build:karma builder (esbuild), a standalone component imported through a barrel (export * from './x') can be undefined in...
Open Graph Description: Which @angular/* package(s) are the source of the bug? build Is this a regression? No Description With the @angular/build:karma builder (esbuild), a standalone component imported through a barrel (...
X Description: Which @angular/* package(s) are the source of the bug? build Is this a regression? No Description With the @angular/build:karma builder (esbuild), a standalone component imported through a barrel (...
Opengraph URL: https://github.com/angular/angular-cli/issues/34250
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"karma builder: component imported through a barrel is undefined in TestBed when another spec has an async test (cross-chunk lazy init, karma counterpart of #33728)","articleBody":"### Which @angular/* package(s) are the source of the bug?\n\nbuild\n\n### Is this a regression?\n\nNo\n\n### Description\n\nWith the `@angular/build:karma` builder (esbuild), a standalone component imported through a **barrel** (`export * from './x'`) can be `undefined` inside another component's `imports: [...]`. TestBed then throws `Cannot read properties of undefined (reading 'ɵcmp')`. Whether it fails depends only on **which other spec files are part of the same test bundle**. The full suite of a project can pass while a subset (`--include`) fails, or the other way around.\n\nThis looks like the karma counterpart of #33728. The mechanism is the same: shared chunks, lazy `__esm` initialisers, and a spec with an `async` test plus zone.js. #33729 fixed that one by setting `disableCodeSplitting` for the **Vitest** runner of `@angular/build:unit-test`. `src/builders/karma/application_builder.ts` on `main` still bundles with code splitting enabled, so the karma builder is still affected.\n\nWhat happens in the emitted bundle:\n\n1. A spec that contains an `async` test callback is emitted as CommonJS (`var require_x_spec = __commonJS({...}); export default require_x_spec();`). Everything it imports becomes a lazy `__esm` initialiser. Here that includes the component behind the barrel (`init_form_layout_component()`).\n2. The barrel module (`index.ts`, just `export *`) is placed in its own chunk. That chunk is what calls `init_form_layout_component()`.\n3. The chunk with the consuming component (`DialogComponent`, shared by two specs) imports the component's chunk directly, but **not** the barrel's chunk. It never calls the initialiser itself.\n4. The spec entry imports the consumer's chunk **before** the barrel's chunk. The consumer's `@Component({ imports: [FormLayoutComponent] })` decorator is evaluated while `FormLayoutComponent` is still `undefined`. The array literal keeps that `undefined` for good, even though the binding becomes defined a moment later.\n\nTrigger conditions, each toggled on its own in the reproduction below:\n\n| Condition | Without it |\n|---|---|\n| Another spec in the bundle has an `async` test and imports the component directly | removing `async` from `b/other.spec.ts`: **4/4 pass** |\n| The consumer imports the component through a barrel | importing from `../layout/form-layout.component`: passes (the consumer then calls the initialiser itself) |\n| The consumer is shared by ≥ 2 specs and the barrel by ≥ 3, so they land in different chunks | with a single consumer spec, everything is inlined and passes |\n\n### Minimal Reproduction\n\nEight small files, run with `ng test --watch=false`:\n\n**angular.json** (test target only)\n```json\n{\n \"version\": 1,\n \"projects\": {\n \"repro\": {\n \"projectType\": \"application\",\n \"root\": \"\",\n \"sourceRoot\": \"src\",\n \"architect\": {\n \"test\": {\n \"builder\": \"@angular/build:karma\",\n \"options\": {\n \"polyfills\": [\"zone.js\", \"zone.js/testing\"],\n \"tsConfig\": \"tsconfig.spec.json\",\n \"browsers\": \"ChromeHeadless\"\n }\n }\n }\n }\n }\n}\n```\n\n**tsconfig.json**\n```json\n{\n \"compilerOptions\": {\n \"strict\": true, \"skipLibCheck\": true, \"isolatedModules\": true,\n \"experimentalDecorators\": true, \"importHelpers\": true,\n \"target\": \"ES2022\", \"module\": \"preserve\"\n }\n}\n```\n`tsconfig.spec.json` extends it with `\"types\": [\"jasmine\"]` and `\"include\": [\"src/**/*.ts\"]`.\n\n**src/app/layout/form-layout.component.ts**\n```ts\nimport { Component } from '@angular/core';\n\n@Component({ selector: 'app-form-layout', template: '\u003cng-content />' })\nexport class FormLayoutComponent {}\n```\n\n**src/app/layout/index.ts**\n```ts\nexport * from './form-layout.component';\n```\n\n**src/app/a/dialog.component.ts**\n```ts\nimport { Component } from '@angular/core';\nimport { FormLayoutComponent } from '../layout';\n\n@Component({\n selector: 'app-dialog',\n imports: [FormLayoutComponent],\n template: '\u003capp-form-layout>dialog\u003c/app-form-layout>',\n})\nexport class DialogComponent {}\n```\n\n**src/app/a/dialog.component.spec.ts** and **src/app/a/dialog-again.spec.ts** (identical, two consumers of `DialogComponent`)\n```ts\nimport { TestBed } from '@angular/core/testing';\nimport { DialogComponent } from './dialog.component';\n\ndescribe('DialogComponent', () => {\n it('creates', () => {\n TestBed.configureTestingModule({ imports: [DialogComponent] });\n expect(TestBed.createComponent(DialogComponent).componentInstance).toBeTruthy();\n });\n});\n```\n\n**src/app/c/page.component.ts** (a third importer of the barrel) and its spec\n```ts\nimport { Component } from '@angular/core';\nimport { FormLayoutComponent } from '../layout';\n\n@Component({\n selector: 'app-page',\n imports: [FormLayoutComponent],\n template: '\u003capp-form-layout>page\u003c/app-form-layout>',\n})\nexport class PageComponent {}\n```\n```ts\nimport { TestBed } from '@angular/core/testing';\nimport { PageComponent } from './page.component';\n\ndescribe('PageComponent', () => {\n it('creates', () => {\n TestBed.configureTestingModule({ imports: [PageComponent] });\n expect(TestBed.createComponent(PageComponent).componentInstance).toBeTruthy();\n });\n});\n```\n\n**src/app/b/other.spec.ts** (the only spec with an `async` test; it imports the component directly)\n```ts\nimport { TestBed } from '@angular/core/testing';\nimport { FormLayoutComponent } from '../layout/form-layout.component';\n\ndescribe('Other (has an async test)', () => {\n it('creates the layout', async () => {\n TestBed.configureTestingModule({ imports: [FormLayoutComponent] });\n await Promise.resolve();\n expect(TestBed.createComponent(FormLayoutComponent).componentInstance).toBeTruthy();\n });\n});\n```\n\nResult: `TOTAL: 2 FAILED, 2 SUCCESS`. Both `DialogComponent` specs fail with the error below. Removing `async` (and the `await`) from `other.spec.ts` gives `TOTAL: 4 SUCCESS`.\n\n### Exception or Error\n\n```text\nTypeError: Cannot read properties of undefined (reading 'ɵcmp')\n at getComponentDef2\n at isStandaloneComponent\n at queueTypesFromModulesArrayRecur\n at TestBedCompiler.queueTypesFromModulesArray\n at TestBedCompiler.configureTestingModule\n at _TestBedImpl.configureTestingModule\n```\n\n### Your Environment\n\n```text\nAngular CLI: 20.3.32\nNode: 24.12.0\nPackage Manager: npm 11.6.2\nOS: darwin arm64\n\nAngular: 20.3.16\n@angular/build 20.3.16 (esbuild 0.25.9)\ntypescript 5.8.3\nzone.js 0.15.1\n```\n\n### Anything else relevant?\n\n- I reproduced it on 20.3.16. I haven't run it on 22.x, but on `main` the karma builder still doesn't set `disableCodeSplitting` (only `unit-test/runners/vitest/build-options.ts` does since #33729). The code path looks unchanged.\n- In a real project this showed up as 3–5 component specs failing only when `--include` mixed two folders, while the full suite passed. Which spec happened to call the initialiser first decided the outcome.\n- Applying the same `disableCodeSplitting: true` to the karma application build seems like the natural fix, as #33729 did for Vitest.\n","author":{"url":"https://github.com/fabiofigrod","@type":"Person","name":"fabiofigrod"},"datePublished":"2026-10-03T21:59:03Z","url":"https://github.com/angular/angular-cli/issues/34250"}
| 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:204d325d-1aa9-d6f8-1cf2-01119f258967 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E522:1D20B7:2B47AF0:36DE903:6B01E0F1 |
| html-safe-nonce | 81001a0f347656284a477127e5d68d0214fa608da794e69f515a42d12e89a75b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNTIyOjFEMjBCNzoyQjQ3QUYwOjM2REU5MDM6NkIwMUUwRjEiLCJ2aXNpdG9yX2lkIjoiODAxODk1MDc0NTEzMzAxNTI4MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | fd1952cb9b1b0f8e87d67c09691c794f39802e801427b7ff29b1a529c1728694 |
| hovercard-subject-tag | issue:5693885025 |
| 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/34250/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9432fda4dad65a7b6e4dfe66b3a591509455fa549b7d2d38b8e29728f535e371/angular/angular-cli/issues/34250 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9432fda4dad65a7b6e4dfe66b3a591509455fa549b7d2d38b8e29728f535e371/angular/angular-cli/issues/34250 |
| og:image:alt | Which @angular/* package(s) are the source of the bug? build Is this a regression? No Description With the @angular/build:karma builder (esbuild), a standalone component imported through a barrel (... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | fabiofigrod |
| 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