Skip to content

fix(@angular/build): use dynamic TestComponentRenderer for Vitest#33117

Open
clydin wants to merge 1 commit intoangular:mainfrom
clydin:feat/vitest-dynamic-renderer
Open

fix(@angular/build): use dynamic TestComponentRenderer for Vitest#33117
clydin wants to merge 1 commit intoangular:mainfrom
clydin:feat/vitest-dynamic-renderer

Conversation

@clydin
Copy link
Copy Markdown
Member

@clydin clydin commented May 1, 2026

This commit implements a custom TestComponentRenderer in the virtual init-testbed.js file generated for Vitest. In Vitests non-isolated mode (isolate: false) with JSDOM, Vitest creates a fresh document for each spec file but reuses the module cache. The default Angular DOMTestComponentRenderer caches the document during initialization, leading to stale references and errors like setAttribute is not a function in subsequent tests. The new DynamicDOMTestComponentRenderer looks up the document dynamically on every operation, resolving the issue without requiring a breaking change to defaults or affecting browser-based testing.

@clydin clydin marked this pull request as ready for review May 1, 2026 16:00
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a DynamicDOMTestComponentRenderer to the Vitest unit test runner to prevent stale document references when running in non-isolated mode with JSDOM. It also increases the artifact count in a Vitest E2E test and removes an unused import. Review feedback suggests enhancing the robustness of the new renderer by adding null checks for the document and its body, as well as optimizing calls to the DOM utility.

Comment thread packages/angular/build/src/builders/unit-test/runners/vitest/build-options.ts Outdated
Comment thread packages/angular/build/src/builders/unit-test/runners/vitest/build-options.ts Outdated
This commit implements a custom TestComponentRenderer in the virtual init-testbed.js file generated for Vitest. In Vitests non-isolated mode (isolate: false) with JSDOM, Vitest creates a fresh document for each spec file but reuses the module cache. The default Angular DOMTestComponentRenderer caches the document during initialization, leading to stale references and errors like setAttribute is not a function in subsequent tests. The new DynamicDOMTestComponentRenderer looks up the document dynamically on every operation, resolving the issue without requiring a breaking change to defaults or affecting browser-based testing.
@clydin clydin force-pushed the feat/vitest-dynamic-renderer branch from 65afe93 to f2c241b Compare May 1, 2026 16:15
@clydin clydin added action: review The PR is still awaiting reviews from at least one requested reviewer target: minor This PR is targeted for the next minor release labels May 1, 2026
@clydin clydin requested review from alan-agius4 May 1, 2026 17:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

action: review The PR is still awaiting reviews from at least one requested reviewer area: @angular/build target: minor This PR is targeted for the next minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant