Skip to content

fix(@angular/build): execute setup file hooks for each spec file when coverage is enabled - #34143

Merged
alan-agius4 merged 1 commit into
angular:mainfrom
alan-agius4:fix-coverage-setup-files
Sep 22, 2026
Merged

alan-agius4 merged 1 commit into
angular:mainfrom
alan-agius4:fix-coverage-setup-files

Conversation

@alan-agius4

@alan-agius4 alan-agius4 commented Sep 22, 2026

Copy link
Copy Markdown
Collaborator

When code coverage is enabled with the Vitest runner, test entry points are served as a virtual one-line import stub (import "./${outputPath}";) so that the test files themselves can be excluded from coverage reports after sourcemap remapping.

However, setup files configured via setupFiles were also registered in testEntryPointMappings and served as virtual stubs. Because Vitest only invalidates the top-level setup module between spec files, the imported intermediate bundle was cached in vite-node after its initial evaluation and never re-evaluated on subsequent spec files. This caused setup file hooks (e.g. beforeEach / afterEach) to silently run only for the first spec file executed by each worker.

This PR:

  1. Adds setupFiles to PluginOptions in plugins.ts and passes testSetupFiles from executor.ts.
  2. Skips generating the virtual coverage stub for setup files (setupFileSet.has(id)), allowing setup files to be directly evaluated and re-evaluated by Vitest before each spec file.
  3. Adds an integration test in setup-files_spec.ts verifying that beforeEach and afterEach hooks in setup files execute for each spec file when coverage is enabled.

Fixes #34137

@alan-agius4
alan-agius4 marked this pull request as ready for review September 22, 2026 11:34
@alan-agius4 alan-agius4 added target: patch This PR is targeted for the next patch release target: rc This PR is targeted for the next release-candidate and removed target: patch This PR is targeted for the next patch release labels Sep 22, 2026

@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 ensures that setup files are not wrapped in virtual import stubs when coverage is enabled in Vitest, allowing setup hooks to run correctly for each spec file. The feedback suggests normalizing the paths in setupFiles to absolute POSIX paths to prevent issues on Windows and with relative paths, which also allows simplifying the setup file detection logic by removing the fragile entryPoint.startsWith('setup') check.

Comment thread packages/angular/build/src/builders/unit-test/runners/vitest/plugins.ts Outdated
Comment thread packages/angular/build/src/builders/unit-test/runners/vitest/plugins.ts Outdated
@alan-agius4
alan-agius4 force-pushed the fix-coverage-setup-files branch from 0b1620a to 89e9d60 Compare September 22, 2026 11:51
@alan-agius4 alan-agius4 added the action: review The PR is still awaiting reviews from at least one requested reviewer label Sep 22, 2026
@alan-agius4
alan-agius4 requested a review from clydin September 22, 2026 12:01
… coverage is enabled

When code coverage is enabled with the Vitest runner, test entry points are served as a virtual one-line import stub (`import "./${outputPath}";`) so that the test files themselves can be excluded from coverage reports after sourcemap remapping.

However, setup files configured via `setupFiles` were also treated as test entry points and served as virtual stubs. Because Vitest only invalidates the top-level setup module between spec files, the imported intermediate bundle was cached in `vite-node` after its initial evaluation and never re-evaluated on subsequent spec files. This caused setup file hooks (e.g. `beforeEach` / `afterEach`) to silently only run for the first spec file of each worker.

This commit excludes setup files from being wrapped in the virtual coverage stub, allowing them to be directly evaluated and re-evaluated by Vitest before each spec file.

Fixes angular#34137
@alan-agius4
alan-agius4 force-pushed the fix-coverage-setup-files branch from 89e9d60 to a75ea7d Compare September 22, 2026 13:49
@alan-agius4 alan-agius4 added action: merge The PR is ready for merge by the caretaker and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Sep 22, 2026
@alan-agius4
alan-agius4 merged commit cea7feb into angular:main Sep 22, 2026
40 checks passed
@alan-agius4
alan-agius4 deleted the fix-coverage-setup-files branch September 22, 2026 15:06
@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: rc This PR is targeted for the next release-candidate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

unit-test: with --coverage, a setup file's hooks reach only the first spec file of each worker

2 participants