`@angular/build:unit-test` virtual `init-testbed.js` guards `initTestEnvironment()` behind a once-per-worker symbol → stale DomAdapter under vitest ≥4.0.5 + `isolate: false`
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 55/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 冷清
- 技术栈
- angular, typescript
调研方向
从 packages/angular/build/src/builders/unit-test/runners/vitest/build-options.ts 和 plugins.ts 开始,然后使用 ng test --force、isolate: false 和 vitest 4.x 反复运行报告中的 jsdom suite。验证 stale-DomAdapter failure 不再跨 spec 文件发生,同时保留 builder 的 unit-test 设置行为;该 issue 提议采用 analogjs/analog#2244 中的 reset-and-reinitialize pattern,并指出 documentation 作为次要的后续工作。
由索引模型根据 Issue 内容生成。
描述
This is a follow-up to the now-closed/auto-locked #32754 with the missing details. That issue was dismissed as potentially analog-specific because the reporter had
@analogjs/vitest-angularin their deps. This report uses@angular/build:unit-testonly and pins the bug to specific lines in@angular/build's own source.
Which @angular/* package(s) are the source of the bug?
@angular/build
Is this a regression?
Yes — surfaces with vitest ≥4.0.5 (see vitest-dev/vitest#8944).
Description
@angular/build:unit-test's vitest runner combines two defaults that interact badly under vitest ≥4.0.5:
plugins.ts:254hardcodesisolate: falsefor the vitest pool (intentional, "align with the Karma/Jasmine experience"). Worker module graphs are therefore reused across spec files.build-options.ts:73-89— the injected virtualinit-testbed.jswrapsgetTestBed().initTestEnvironment(...)in anif (!globalThis[ANGULAR_TESTBED_SETUP])guard. The comment on line 79 explicitly says "the guard condition above ensures that the setup is only performed once". That's the anti-pattern.
The first spec file in a worker initializes a platformBrowserTesting whose DomAdapter captures the jsdom document as a closure reference. Subsequent spec files in the same worker skip the init block entirely, so the DomAdapter keeps being reused. When jsdom's document swaps between spec files — and vitest ≥4.0.5 no longer re-executes setup files between spec files under isolate: false (vitest-dev/vitest#8944) — _getDOM().getDefaultDocument().createElement(tagName) returns something that is not a real HTMLElement, and DOMTestComponentRenderer.insertRootElement crashes:
```
TypeError: rootElement.setAttribute is not a function
at DOMTestComponentRenderer.insertRootElement (@angular/platform-browser/testing)
at _TestBedImpl.createComponent
```
Different spec files fail each run; each failing spec passes in isolation. Classic test-isolation bug.
The analog project had the analogous pattern in setupTestBed() and fixed it in analogjs/analog#2244 by calling resetTestEnvironment() + initTestEnvironment() on every setup invocation instead of guarding with a once-only singleton.
Reproduction
I can put up a public minimal repro if helpful, but the bug is visible from the builder source alone — the conditions are:
- Angular 22 (or 21 with vitest ≥4.0.5) monorepo using
@angular/build:unit-testwith jsdom. runnerConfigunset, so the builder's defaultisolate: falseapplies.- Suite of ~50+ spec files to make the race frequent.
- Run
ng test --force(or equivalent) several times. A different 1–10 specs crash each run with thesetAttributetrace.
Confirmed environment:
```
Angular CLI: 22.0.0-next.6
@angular/build: 22.0.0-next.6
@angular/core: 22.0.0-next.9
vitest: 4.1.4 (via ^4.0.17)
Environment: jsdom
Runtime: Node 22 / Bun 1.x
OS: Windows 11 (also reported on Ubuntu CI via analogjs/analog#2222)
```
Exception
```
TypeError: rootElement.setAttribute is not a function
❯ DOMTestComponentRenderer.insertRootElement node_modules/@angular/platform-browser/fesm2022/testing.mjs:24
❯ _TestBedImpl.createComponent packages/core/testing/src/test_bed.ts:420
```
(The #32754 variant Cannot set base providers because it has already been called is the same root cause but a different downstream symptom — it fires when the user's own test-setup.ts re-calls initTestEnvironment. Projects that don't re-call it land on setAttribute is not a function instead.)
Proposed fix
Primary — mirror analogjs/analog#2244. Replace the if (!globalThis[ANGULAR_TESTBED_SETUP]) guard in build-options.ts:73-89 with a reset-and-reinit pattern:
```ts
getTestBed().resetTestEnvironment();
getTestBed().initTestEnvironment([BrowserTestingModule, TestModule], platformBrowserTesting(), {
errorOnUnknownElements: true,
errorOnUnknownProperties: true,
// ...
});
```
Even under isolate: false, if the setup file re-runs (or a user hook calls it), each spec file gets a fresh platform targeting the current jsdom.
Secondary (defensive) — either flip the default to isolate: true with a documented opt-out for projects that want the Karma-style speed, or make DOMTestComponentRenderer.insertRootElement throw a clearer error when rootElement.setAttribute is not callable (e.g. "TestBed's DOM adapter is referencing a document that has been torn down — check your vitest `isolate` setting"). Today the TypeError has no breadcrumb to the root cause.
Docs — the unit-test builder docs should warn that isolate: false + vitest ≥4.0.5 + jsdom silently bleeds DOM state across spec files.
Local workaround
Override to isolate: true via a project-level vitest-base.config.ts (possible because runnerConfig: true merges user config on top of the builder defaults). 10 consecutive ng test --force runs then pass deterministically. Wall-time cost is ~10–30%. This is a workaround, not a fix — the builder's guard is what should change.
References
- vitest-dev/vitest#8944 — vitest 4.0.5 regression that triggers this class of flake.
- angular/angular-cli#32754 — closed/auto-locked predecessor of this report. Same root cause, different surface symptom, closed as "insufficient information."
- analogjs/analog#2222 — same error stack downstream in analog's vitest-angular stack.
- analogjs/analog#2244 — reference fix pattern.
- 主要语言
- TypeScript
- 星标
- 27k
- 派生
- 11.8k
- 平均合并
- 16 小时 21 分钟
- 30 天内合并 PR
- 170
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
angular/angular-cli 的其他 Issue
-
area: @angular/build gemini-triaged
难度 2/5 1-3 小时 新手友好度 74/100
angular/angular-cli#33955 ·
-
area: @angular/cli gemini-triaged
难度 2/5 1-3 小时 新手友好度 72/100
angular/angular-cli#33055 · 1 条评论 · 3 个 reaction ·
-
angular/build:library area: @angular/build gemini-triaged
angular/angular-cli#34131 · 已指派 1 人 ·
-
angular/build:library area: @angular/build gemini-triaged
angular/angular-cli#34130 · 已指派 1 人 ·
-
angular/build:library area: @angular/build gemini-triaged
angular/angular-cli#34128 · 已指派 1 人 ·
查看 angular/angular-cli 的全部 Issue
相似的 Issue
-
community first-timers-only good first issue hacktoberfest help wanted low hanging fruit up-for-grabs
难度 1/5 1 小时以内 新手友好度 95/100
-
Ecosystem: ClawMetry — the Qwen Code reader is now free and open source (follow-up to #9294 / #9338) 未关闭category/integration priority/P3 scope/documentation status/ready-for-human type/feature-request
难度 1/5 1 小时以内 新手友好度 84/100
-
area:auth FE mvp P3
难度 2/5 1-3 小时 新手友好度 88/100
klasolsson81/jobbliggaren#1788 ·
-
难度 2/5 1-3 小时 新手友好度 74/100
get-convex/migrations#69 ·
-
accessibility angular bug good first issue typescript ux
难度 2/5 1-3 小时 新手友好度 88/100
apache/fineract-backoffice-ui#584 · 1 条评论 ·