Skip to content

fix(@angular/build): enable code splitting for vitest browser tests - #33949

Merged
alan-agius4 merged 1 commit into
angular:mainfrom
alan-agius4:fix-build-vitest-code-splitting
Aug 27, 2026
Merged

fix(@angular/build): enable code splitting for vitest browser tests#33949
alan-agius4 merged 1 commit into
angular:mainfrom
alan-agius4:fix-build-vitest-code-splitting

Conversation

@alan-agius4

@alan-agius4 alan-agius4 commented Aug 27, 2026

Copy link
Copy Markdown
Collaborator

Enables code splitting for Vitest browser tests while keeping it disabled for Node-based tests (JSDOM/Happy-DOM).

Vitest's Node-based module loading emulation (vite-node) is not fully spec compliant and lacks live ESM bindings across chunk boundaries, which can cause uninitialized exports and break mocking. In browser tests, however, the real browser adheres to the ECMAScript spec, so code splitting can be safely enabled there.

@alan-agius4 alan-agius4 added target: patch This PR is targeted for the next patch release target: minor This PR is targeted for the next minor release and removed target: patch This PR is targeted for the next patch release labels Aug 27, 2026
@alan-agius4
alan-agius4 force-pushed the fix-build-vitest-code-splitting branch 3 times, most recently from cfaa501 to 2c03d17 Compare August 27, 2026 11:59
@alan-agius4
alan-agius4 force-pushed the fix-build-vitest-code-splitting branch from 2c03d17 to e3d55b2 Compare August 27, 2026 12:04
@alan-agius4 alan-agius4 reopened this Aug 27, 2026
@alan-agius4
alan-agius4 force-pushed the fix-build-vitest-code-splitting branch 2 times, most recently from 3dd7aa1 to c3cefba Compare August 27, 2026 12:14
@alan-agius4 alan-agius4 added the action: review The PR is still awaiting reviews from at least one requested reviewer label Aug 27, 2026
@alan-agius4
alan-agius4 requested a review from clydin August 27, 2026 12:34
@alan-agius4
alan-agius4 marked this pull request as ready for review August 27, 2026 12:34

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

Copy link
Copy Markdown

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 removes the disableCodeSplitting option and resolves ESM binding issues in Vitest/Vite SSR runs. It does this by eagerly invoking __esm initializers in chunk/bundle outputs within the Vitest plugin, and by introducing ensureTestFileEsm in the Angular compiler plugin to append export {}; to test spec files lacking export statements so they are treated as ES modules. The feedback suggests making ensureTestFileEsm idempotent by checking if the file already contains import or export statements before appending export {}; to prevent duplicate appends.

Comment thread packages/angular/build/src/tools/esbuild/angular/compiler-plugin.ts Outdated
@alan-agius4
alan-agius4 force-pushed the fix-build-vitest-code-splitting branch 3 times, most recently from 18757a9 to 15f4dfc Compare August 27, 2026 13:09
@alan-agius4 alan-agius4 changed the title fix(@angular/build): re-enable code splitting for unit tests feat(@angular/build): enable code splitting for vitest browser tests Aug 27, 2026
@angular-robot angular-robot Bot added the detected: feature PR contains a feature commit label Aug 27, 2026
@alan-agius4 alan-agius4 added target: patch This PR is targeted for the next patch release and removed target: minor This PR is targeted for the next minor release labels Aug 27, 2026
@alan-agius4 alan-agius4 added action: merge The PR is ready for merge by the caretaker target: minor This PR is targeted for the next minor release target: patch This PR is targeted for the next patch release and removed action: review The PR is still awaiting reviews from at least one requested reviewer target: patch This PR is targeted for the next patch release target: minor This PR is targeted for the next minor release labels Aug 27, 2026
Enables code splitting for Vitest browser tests while keeping it disabled
for Node-based tests (JSDOM/Happy-DOM).

Vitest's Node-based module loading emulation (vite-node) is not fully spec
compliant and lacks live ESM bindings across chunk boundaries, which can
cause uninitialized exports and break mocking. In browser tests, however,
the real browser adheres to the ECMAScript spec, so code splitting can be
safely enabled there.
@alan-agius4
alan-agius4 force-pushed the fix-build-vitest-code-splitting branch from 15f4dfc to 0635ef4 Compare August 27, 2026 13:33
@alan-agius4 alan-agius4 changed the title feat(@angular/build): enable code splitting for vitest browser tests fix(@angular/build): enable code splitting for vitest browser tests Aug 27, 2026
@alan-agius4 alan-agius4 removed the detected: feature PR contains a feature commit label Aug 27, 2026
@alan-agius4
alan-agius4 merged commit 51565d4 into angular:main Aug 27, 2026
15 checks passed
@alan-agius4
alan-agius4 deleted the fix-build-vitest-code-splitting branch August 27, 2026 13:34
@alan-agius4

Copy link
Copy Markdown
Collaborator Author

This PR was merged into the repository. The changes were merged into the following branches:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

action: merge The PR is ready for merge by the caretaker area: @angular/build target: patch This PR is targeted for the next patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants